3
4

More than 1 year has passed since last update.

RustをMacで公式ドキュメントにしたがって始める

Last updated at Posted at 2021-12-12

この記事は Rust Advent Calendar 2021 の6日目の記事です。
昨日の記事は @hkford さんの AWS SDK for Rust (Developer Preview) を触ってみた でした。

はじめに

  • Advent Calendar を見ててRustの人気が凄まじいのでゼロ知識からMacで動かすところまでやってみる。

Rustのインストール

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

コマンドが完了したら To get started you may need to restart your current shell. と書いてある通りにターミナルを再起動するとPATHが通って各種コマンドが使えるようになっている。

$ rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)

$ rustup --version
rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`

$ cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)

無事にインストールされている。

Rustの開発環境において、全てのツールは~/.cargo/binディレクトリにインストールされ、ここにrustc、cargo、rustupを含むRustのツールチェーンが置かれます。
よって、このディレクトリをPATH環境変数に含めるのが、Rustの開発者にとっての通例となっています。

さらにPATHを通すのが通例とのことなので ~/.zshrc に通す。

export PATH=$HOME/.cargo/bin:$PATH

これでインストール自体は終わり、とても簡単でした。

アンインストールの手順も書いてあるのは好印象、しかもコマンド一発。

$ rustup self uninstall

rustc

適当なフォルダで準備。

$ mkdir hello_world
$ cd hello_world
$ vim main.rs
main.rs
fn main() {
    println!("Hello, world!");
}

コンパイルして実行する。

$ rustc main.rs
$ ./main
Hello, world!

実行できた。
ここらへんの流れはC言語と似ていますね。

cargo

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

$ cd hello_cargo

$ tree
tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    ├── debug
    │   ├── build
    │   ├── deps
    │   │   ├── hello_cargo-ab36b8e9f033d41f
    │   │   ├── hello_cargo-ab36b8e9f033d41f.32bbbek9y7gr0w9k.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.3b8wql4gwh4zam8l.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.3k22ssq0k840ctbj.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.41gv44lony1efmm2.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.46b3yhpyv9nuz59j.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.4ufk194usjl2kma2.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.4wqtzf49c3b4dqri.rcgu.o
    │   │   ├── hello_cargo-ab36b8e9f033d41f.d
    │   │   ├── hello_cargo-ab36b8e9f033d41f.fmqw2rqs6hrxzbr.rcgu.o
    │   │   ├── hello_cargo-bd1585ca0fd9dde7.d
    │   │   └── libhello_cargo-bd1585ca0fd9dde7.rmeta
    │   ├── examples
    │   ├── hello_cargo
    │   ├── hello_cargo.d
    │   └── incremental
    │       ├── hello_cargo-2e5hmus6kw5k4
    │       │   ├── s-g52zen6r7p-fhottn-35p4tiguqfdzv
    │       │   │   ├── 32bbbek9y7gr0w9k.o
    │       │   │   ├── 3b8wql4gwh4zam8l.o
    │       │   │   ├── 3k22ssq0k840ctbj.o
    │       │   │   ├── 41gv44lony1efmm2.o
    │       │   │   ├── 46b3yhpyv9nuz59j.o
    │       │   │   ├── 4ufk194usjl2kma2.o
    │       │   │   ├── 4wqtzf49c3b4dqri.o
    │       │   │   ├── dep-graph.bin
    │       │   │   ├── fmqw2rqs6hrxzbr.o
    │       │   │   ├── query-cache.bin
    │       │   │   └── work-products.bin
    │       │   └── s-g52zen6r7p-fhottn.lock
    │       └── hello_cargo-3d56908pam683
    │           ├── s-g52zfg82fu-1lhcfv2-1wto432sjx43u
    │           │   ├── dep-graph.bin
    │           │   ├── query-cache.bin
    │           │   └── work-products.bin
    │           └── s-g52zfg82fu-1lhcfv2.lock
    └── release
        ├── build
        ├── deps
        │   ├── hello_cargo-2e834568fcba3635
        │   └── hello_cargo-2e834568fcba3635.d
        ├── examples
        ├── hello_cargo
        ├── hello_cargo.d
        └── incremental

16 directories, 38 files
src/main.rs
fn main() {
    println!("Hello, world!");
}
Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

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

[dependencies]

色々できている。

ビルドして実行してみる。

$ cargo build
   Compiling hello_cargo v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 1.80s

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_cargo`
Hello, world!

とても簡単にできた。

まとめ

  • Rustはドキュメントがシンプルにまとまっていてとてもスムーズに導入できた。
  • とりあえず動かしたい人にとっては公式がブラウザ上の実行環境を用意しているのも嬉しい。
  • まだRustの入り口に立ったところだが、Rustが開発者フレンドリーであることは公式ドキュメントからとても感じられた。
3
4
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
3
4