2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rustを極めたい - 0

Last updated at Posted at 2023-06-05

Rustとは

2015年に正式リリースされた比較的新しめのプログラミング言語です。WEBブラウザのFirefoxを手掛けるMozilla社によって開発が進められています。直近だと2023年6月1日にバージョン1.70がリリースされました。

Rustには下記のような特徴があります。

  • 仮想マシン・ガベージコレクションを持たないため、C/C++と同等の実行速度を実現できる。
  • モダンな文法で書くことができる。
  • OSなどのシステム開発からWEBアプリケーション開発まで幅広く実装することができる。
  • コンパイルエラーが非常に親切でわかりやすい。
  • 「所有権」「借用」「ライフタイム」などの言語仕様により、メモリセーフなコーディングができる。
  • PythonやGoなどと比べると、若干学習コストが高い。
  • 言語レベルでの制約が厳しい分、誰が書いても比較的読みやすいコードになる。
  • 公式のドキュメントが充実している。

とにかく始めてみよう

公式サイトに従い、Rustと周辺ツールをインストールします。

ターミナルで下記コマンドを実行して、インストール出来ているか確認します。

$ rustup --version
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.70.0 (90c541806 2023-05-31)`


$ cargo --version
cargo 1.70.0 (ec8a8a0ca 2023-04-25)

それぞれバージョンが表示されれば成功です。

rustupって何?

Mozilla社が出しているRustの公式インストーラ兼バージョン管理ツール。rustup updateで最新のRustをインストールしたり、toolchainと呼ばれる、コンパイラ・関連ツール・標準ライブラリ一式を管理することができます。

cargoって何?

こちらもRustインストール時に付属する公式のビルドツール兼パッケージマネージャー。cargo new project_nameで新規プロジェクトを作成したり、cargo runで、作成したプロジェクトを実行することができます。

RustでHello World!プロジェクトを作ってみよう

cargoを使って、新しいプロジェクトを作成します。プロジェクト名はなんでも良いですが、全て小文字・単語間はアンダーバーで繋げるスネークケースにします。コマンドが成功すると、カレントディレクトリにプロジェクト名の付いたフォルダが生成されます。

$ cargo new hello_world
     Created binary (application) `hello_world` package

$ ls
hello_world

プロジェクトフォルダの中身は、下記のようになっています。

$ cd hello_world
$ tree
.
├── Cargo.toml
└── src
    └── main.rs

Cargo.tomlというファイルは、プロジェクトの設定ファイルになります。ここには、プロジェクトの情報・ライブラリの依存関係などが記載されます。Javascriptのpackage.jsonファイルのようなものです。

[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# ここにライブラリなどの依存関係を書いていく

srcディレクトリの配下に、実際のRustコードを書いていきます。cargoを使ってプロジェクトを作成すると、最初から下記のようなコードが書かれています。

// src/main.rs

fn main() {
    println!("Hello, world!");
}

プロジェクトを実行して、結果を見てみましょう。

$ cargo run
   Compiling hello_world v0.1.0 (/path/to/project/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.18s
     Running `target/debug/hello_world`
Hello, world!

hello worldプロジェクトで色々試してみよう

ソースコードを書き換えて、出力がどのように変わるか確認してみましょう。

println!()で複数行出力

// src/main.rs

fn main() {
    println!("Hello,");
    println!("World!!");
}
$ cargo run
   Compiling hello_world v0.1.0 (/path/to/project/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.45s
     Running `target/debug/hello_world`
Hello,
World!!

forifで繰り返しと条件分岐

// src/main.rs

fn main() {
    for i in 0..10{
        if i % 2 == 0 {
            println!("hello?");
        }else{
            println!("world!!");
        }
    }
}
$ cargo run
   Compiling hello_world v0.1.0 (/path/to/project/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.58s
     Running `target/debug/hello_world`
hello?
world!!
hello?
world!!
hello?
world!!
hello?
world!!
hello?
world!!

変数と繰り返しで縦書きに

// src/main.rs

fn main() {
    let message: String = String::from("hello world!");
    for i in message.chars(){
        println!("{}",i);
    }
}
$ cargo run
   Compiling hello_world v0.1.0 (/path/to/project/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.42s
     Running `target/debug/hello_world`
h
e
l
l
o
 
w
o
r
l
d
!

終わりに

次回は、変数・定数・データ型・関数などについて書いていこうと思います。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?