カテゴリー
Linux

Mac に Rust 言語を本家のやり方に素直に従ってインストールと getting started した記録

環境等

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.5
BuildVersion:   18F132
$

インストール方法

インストール記録

  • 一度入力する必要があった。お任せインストールの "1" を入力した。
$ curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  /Users/oki2a24/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:

  /Users/oki2a24/.profile
  /Users/oki2a24/.bash_profile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

   default host triple: x86_64-apple-darwin
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2019-05-23, rust version 1.35.0 (3c235d560 2019-05-20)
info: downloading component 'rustc'
 81.6 MiB /  81.6 MiB (100 %)   2.0 MiB/s in 42s ETA:  0s
info: downloading component 'rust-std'
 53.5 MiB /  53.5 MiB (100 %)   1.9 MiB/s in 31s ETA:  0s
info: downloading component 'cargo'
  3.5 MiB /   3.5 MiB (100 %)   2.3 MiB/s in  2s ETA:  0s
info: downloading component 'rust-docs'
 10.4 MiB /  10.4 MiB (100 %)   1.1 MiB/s in  9s ETA:  0s
info: installing component 'rustc'
 81.6 MiB /  81.6 MiB (100 %)  17.8 MiB/s in  4s ETA:  0s
info: installing component 'rust-std'
 53.5 MiB /  53.5 MiB (100 %)  21.2 MiB/s in  2s ETA:  0s
info: installing component 'cargo'
info: installing component 'rust-docs'
 10.4 MiB /  10.4 MiB (100 %)   2.9 MiB/s in  3s ETA:  0s
info: default toolchain set to 'stable'

  stable installed - rustc 1.35.0 (3c235d560 2019-05-20)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env
$

差分

~/.bash_profile, ~/.profile の末尾に次が追加された。


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

PATH に追加されたものを紐解く

To configure your current shell run source $HOME/.cargo/env

とあり、一方で、 bash_profile などには export PATH="$HOME/.cargo/bin:$PATH" が追加され、微妙に内容が異なります。そこで調べたところ、次のようになりました。

  • ~/.cargo/env は export PATH="$HOME/.cargo/bin:$PATH" と書かれており、 ~/.cargo/env などの内容と同じ。つまり、結局は PATH に $HOME/.cargo/bin を追加しているだけ。
  • インストールスクリプトの行なっていることは、 ~/.cargo/bin/ にバイナリを配置しているだけのような気がする。
$ ls -al ~/.cargo/
total 8
drwxr-xr-x   4 oki2a24  staff   128  6 23 15:48 ./
drwxr-xr-x+ 57 oki2a24  staff  1824  6 23 15:46 ../
drwxr-xr-x  14 oki2a24  staff   448  6 23 15:46 bin/
-rw-r--r--   1 oki2a24  staff    37  6 23 15:48 env
$ ls -al ~/.cargo/bin/
total 161184
drwxr-xr-x  14 oki2a24  staff      448  6 23 15:46 ./
drwxr-xr-x   4 oki2a24  staff      128  6 23 15:48 ../
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 cargo*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 cargo-clippy*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 cargo-fmt*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 cargo-miri*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 clippy-driver*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rls*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rust-gdb*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rust-lldb*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rustc*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rustdoc*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rustfmt*
-rwxr-xr-x  12 oki2a24  staff  6875720  6 23 15:46 rustup*
$ cat ~/.cargo/env
export PATH="$HOME/.cargo/bin:$PATH"
$

インストール確認

インストール直後に試したかったので、インストール時に出力されていたコマンドを実行して PATH を追加して試しました。

$ source $HOME/.cargo/env

Install Rust – Rust Programming Language ページを読んでインストール確認できそうなコマンドは次でした。良さそうです。

$ rustc --version
rustc 1.35.0 (3c235d560 2019-05-20)
$

このページを見ると、 さらに Getting started – Rust Programming Language を試してみなよ! というのでやってみます。

Cargo: the Rust build tool and package manager

$ cargo --version
cargo 1.35.0 (6f3e9c367 2019-04-04)
$

Generating a new project

  • プロジェクトを作成すると、 git init 済みの状態となっている。
$ cd ~
$ cargo new hello-rust
     Created binary (application) `hello-rust` package
$
$ tree hello-rust/
hello-rust/
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
$
$ ls -al hello-rust/
total 16
drwxr-xr-x   6 oki2a24  staff   192  6 23 16:14 ./
drwxr-xr-x+ 58 oki2a24  staff  1856  6 23 16:14 ../
drwxr-xr-x   9 oki2a24  staff   288  6 23 16:14 .git/
-rw-r--r--   1 oki2a24  staff    19  6 23 16:14 .gitignore
-rw-r--r--   1 oki2a24  staff   123  6 23 16:14 Cargo.toml
drwxr-xr-x   3 oki2a24  staff    96  6 23 16:14 src/
$ ls -al hello-rust/src/
total 8
drwxr-xr-x  3 oki2a24  staff   96  6 23 16:14 ./
drwxr-xr-x  6 oki2a24  staff  192  6 23 16:14 ../
-rw-r--r--  1 oki2a24  staff   45  6 23 16:14 main.rs
$
$ cd hello-rust/
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        Cargo.toml
        src/

nothing added to commit but untracked files present (use "git add" to track)
$
$ git add -A
$ git commit -m "initial commit"
[master (root-commit) ff405b4] initial commit
 3 files changed, 12 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Cargo.toml
 create mode 100644 src/main.rs
$

プロジェクトをコンパイルして実行

$ cargo run
   Compiling hello-rust v0.1.0 (/Users/oki2a24/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.56s
     Running `target/debug/hello-rust`
Hello, world!
$

Adding dependencies

依存しているパッケージをダウンロードして、コンパイルするようです。

$ vim Cargo.toml
$ git diff
diff --git a/Cargo.toml b/Cargo.toml
index 2791952..8a4372a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,3 +5,4 @@ authors = ["oki2a24 <oki2a24@gmail.com>"]
 edition = "2018"

 [dependencies]
+ferris-says = "0.1"
$
$
$
$ cargo build
    Updating crates.io index
  Downloaded ferris-says v0.1.1
  Downloaded clap v2.33.0
  Downloaded ansi_term v0.11.0
  Downloaded atty v0.2.11
  Downloaded smallvec v0.4.5
  Downloaded bitflags v1.1.0
  Downloaded strsim v0.8.0
  Downloaded unicode-width v0.1.5
  Downloaded error-chain v0.10.0
  Downloaded textwrap v0.11.0
  Downloaded vec_map v0.8.1
  Downloaded libc v0.2.58
  Downloaded backtrace v0.3.30
  Downloaded backtrace-sys v0.1.28
  Downloaded cfg-if v0.1.9
  Downloaded rustc-demangle v0.1.15
  Downloaded autocfg v0.1.4
  Downloaded cc v1.0.37
   Compiling cc v1.0.37
   Compiling libc v0.2.58
   Compiling autocfg v0.1.4
   Compiling bitflags v1.1.0
   Compiling rustc-demangle v0.1.15
   Compiling cfg-if v0.1.9
   Compiling unicode-width v0.1.5
   Compiling strsim v0.8.0
   Compiling vec_map v0.8.1
   Compiling ansi_term v0.11.0
   Compiling smallvec v0.4.5
   Compiling textwrap v0.11.0
   Compiling backtrace v0.3.30
   Compiling atty v0.2.11
   Compiling clap v2.33.0
   Compiling backtrace-sys v0.1.28
   Compiling error-chain v0.10.0
   Compiling ferris-says v0.1.1
   Compiling hello-rust v0.1.0 (/Users/oki2a24/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 18s
$

この操作で作成された Cargo.lock を見てみます。 Node.js の package-lock.json や、 Composer の composer.lock に相当するもののようです。 つまり、依存パッケージは Cargo.toml に記述しますけれども、それがさらにどのパッケージに依存しているのか、そしてすべてのパッケージの詳細なバージョンは何であるのか、を記録するのが、 Cargo.lock のようです。

$ cat Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "atty"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
 "termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "autocfg"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "backtrace"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
 "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
 "rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "backtrace-sys"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "bitflags"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "cc"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "cfg-if"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "error-chain"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "backtrace 0.3.30 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "ferris-says"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "smallvec 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "hello-rust"
version = "0.1.0"
dependencies = [
 "ferris-says 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "libc"
version = "0.2.58"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "redox_syscall"
version = "0.1.54"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "rustc-demangle"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "smallvec"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "termion"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
 "numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)",
 "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "unicode-width"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "winapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf"
"checksum backtrace 0.3.30 (registry+https://github.com/rust-lang/crates.io-index)" = "ada4c783bb7e7443c14e0480f429ae2cc99da95065aeab7ee1b81ada0419404f"
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "39f75544d7bbaf57560d2168f28fd649ff9c76153874db88bdbdfd839b1a7e7d"
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8"
"checksum ferris-says 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "07a21339b4ddc37834c200a769f0fa58919bfda64950abcc1b6438e8b50b32fd"
"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319"
"checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
"checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af"
"checksum smallvec 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f90c5e5fe535e48807ab94fc611d323935f39d4660c52b26b96446a7b33aef10"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
$

次のステップのために、このパッケージを使う準備をしておきます。

$ git add -A
$ git commit -m "ferris-says を依存パッケージに追加する"
[master a1a0a86] ferris-says を依存パッケージに追加する
 2 files changed, 213 insertions(+)
 create mode 100644 Cargo.lock
$
$ vim src/main.rs
$ git diff
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..18308f8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1 @@
-fn main() {
-    println!("Hello, world!");
-}
+use ferris_says::say;
$ git commit -am "small Rust application の準備をする"
[master c1a4312] small Rust application の準備をする
 1 file changed, 1 insertion(+), 3 deletions(-)
$

A small Rust application

お手本ページの通りにファイルに修正を加えました。

$ vim src/main.rs
$ git diff
diff --git a/src/main.rs b/src/main.rs
index 18308f8..8b405ec 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1 +1,11 @@
 use ferris_says::say;
+use std::io::{stdout, BufWriter};
+
+fn main() {
+    let stdout = stdout();
+    let out = b"Hello fellow Rustaceans!";
+    let width = 24;
+
+    let mut writer = BufWriter::new(stdout.lock());
+    say(out, width, &mut writer).unwrap();
+}
$

あとは cargo run してみますと、成功です!

$ cargo run
   Compiling hello-rust v0.1.0 (/Users/oki2a24/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.61s
     Running `target/debug/hello-rust`
----------------------------
| Hello fellow Rustaceans! |
----------------------------
              \
               \
                  _~^~^~_
              \) /  o o  \ (/
                '_   -   _'
                / '-----' \
$

最後に、やらなくても良いですけれども、コミットしておしまいにしましょう。

$ git commit -am "完成した small Rust application を追加する!"
[master a120f59] 完成した small Rust application を追加する!
 1 file changed, 10 insertions(+)
$

おわりに

ほんの少し Rust 言語を体験しました。ページの分量は多くなってしまいましたけれども、行なったことの内容は多くはありません。

Vim でファイルを編集しましたけれども、シンタックスハイライトもされており、とっつきにくくはなさそうです。 Vim 用のプラグインや、 LS もあるようですので、試してみたいと思います。

以上です。

コメントを残す