Open6

Rust さわる

sterashima78sterashima78

Rust をインストール

https://www.rust-lang.org/ja/tools/install

環境は Ubuntu

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/sterashima/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /home/sterashima/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/sterashima/.cargo/bin

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

  /home/sterashima/.profile
  /home/sterashima/.bashrc

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

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

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

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2021-07-29, rust version 1.54.0 (a178d0322 2021-07-26)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 16.7 MiB /  16.7 MiB (100 %)  12.8 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 21.9 MiB /  21.9 MiB (100 %)  14.8 MiB/s in  1s ETA:  0s
info: downloading component 'rustc'
 50.1 MiB /  50.1 MiB (100 %)  18.2 MiB/s in  3s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
  6.0 MiB /   6.0 MiB (100 %)   5.9 MiB/s in  1s ETA:  0s
info: installing component 'clippy'
info: installing component 'rust-docs'
 16.7 MiB /  16.7 MiB (100 %)   1.7 MiB/s in 13s ETA:  0s
info: installing component 'rust-std'
 21.9 MiB /  21.9 MiB (100 %)   5.0 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 50.1 MiB /  50.1 MiB (100 %)   5.3 MiB/s in 10s ETA:  0s
info: installing component 'rustfmt'
  3.7 MiB /   3.7 MiB (100 %)   3.4 MiB/s in  1s ETA:  0s
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.54.0 (a178d0322 2021-07-26)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

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

環境変数読み込む。

$ source $HOME/.cargo/env

インストールスクリプトで . "$HOME/.cargo/env"$HOME/.profile$HOME/.bashrc に書いてくれる。
fish だとこれを読んでくれないので、以下で設定しておく

set -U fish_user_paths $fish_user_paths $HOME/.cargo/bin

以下のように各種コマンドが使えるようになる

$ rustc --version
rustc 1.54.0 (a178d0322 2021-07-26)

$ 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.54.0 (a178d0322 2021-07-26)`

$ rustdoc --version
rustdoc 1.54.0 (a178d0322 2021-07-26)     

$ rustfmt --version
rustfmt 1.4.37-stable (a178d03 2021-07-26)

$ cargo --version
cargo 1.54.0 (5ae8d74b3 2021-06-22)
sterashima78sterashima78

プロジェクト作る

$ cargo new hello
     Created binary (application) `hello` package
$ tree hello/
hello/
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
sterashima78sterashima78

実行する

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

素振りのネタは AtCoder にする

https://doc.rust-jp.rs/atcoder-rust-resources/installation/cargo-generate.html

$ cargo install cargo-generate
$ cargo generate --version
cargo-generate 0.8.0

例えばabc210 の問題を解こうと思うと以下のようにするらしい。

$ cargo generate --name abc210 --git https://github.com/rust-lang-ja/atcoder-rust-base --branch ja
🔧   Creating project called `abc210`...
[1/8]   Done: .gitignore
[2/8]   Done: Cargo.lock
[3/8]   Done: Cargo.toml
[4/8]   Done: LICENSE
[5/8]   Done: README.md
[6/8]   Done: rust-toolchain
[7/8]   Done: src/main.rs
[8/8]   Done: tests/sample_inputs.rs
✨   Done! New project created abc210
$ tree abc210/
abc210/
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── rust-toolchain
├── src
│   └── main.rs
└── tests
    └── sample_inputs.rs

2 directories, 7 files

ABC086Cの解答例が入った状態で作られる。
これ問題ごとに作らないとだめな気がするな。

rust-toolchain が入ってるのでそのバージョンで動く (手元になければ自動で入れてくれる)

$ rustc --version
rustc 1.42.0 (b8cedc004 2020-03-09)

賢くて良い。