Rust環境のセットアップ

Rustの環境をセットアップする手順です。
Rustについてはこちらを参照してください。

実行環境

PC自体は、Macですが再インストールが面倒だったのでDocker上で実行しています。

Docker version 19.03.5, build 633a0ea
image: ubuntu 19.04

rustupのインストール

公式のインストール方法に従ってインストールしていきます。\
公式サイトに行って、説明を読みながらインストールしてもらってもいいですが、面倒な方は
curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh
をターミナル上で実行してもらえばインストールができます。インストール時は下記のようになります。

0a0cc89c09eb# 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.

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

  /root/.cargo/bin

This can be modified with the CARGO_HOME environment variable.

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

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

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

  /root/.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-unknown-linux-gnu
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1  #<- ここは1ではなくてもいいですが、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 2020-03-12, rust version 1.42.0 (b8cedc004 2020-03-09)
info: downloading component 'cargo'
  4.8 MiB /   4.8 MiB (100 %)   1.3 MiB/s in  3s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 12.0 MiB /  12.0 MiB (100 %)   3.0 MiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
 17.1 MiB /  17.1 MiB (100 %)   3.0 MiB/s in  5s ETA:  0s
info: downloading component 'rustc'
 58.6 MiB /  58.6 MiB (100 %)   5.0 MiB/s in 13s ETA:  0s^[[B
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.0 MiB /  12.0 MiB (100 %)   4.3 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
 17.1 MiB /  17.1 MiB (100 %)  10.6 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 58.6 MiB /  58.6 MiB (100 %)   8.4 MiB/s in  7s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.42.0 (b8cedc004 2020-03-09)


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

ログインシェルでパスを通したい方は最後にあるとおり、
source $HOME/.cargo/env
を実行してください。ログインしなおせばパスは通りますので必須ではないです。

Windows環境の方

上のインストール方法はLinux/Unix上でのインストール方法になります。こちらをクリックして、インストーラーを起動していただければ問題なくインストール出来るかと思います。最近はWSLでwindows上でもlinuxコマンドが使えるようになってきているようなので、WSL上でもしかすると実行できるかもしれません。不明点があればコメントでもメールでも構いませんので連絡ください。

Rustでhello world!

実行環境が整ったので、実際にプログラムを書いていきましょう。

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

エディタはなんでもいいので、上のコードを保存し名前をつけて保存しましょう。\
この時保存先が必要になるので、メモかコピーするかしておいてください。
保存したら、ターミナルを開いて事項してみましょう。

0a0cc89c09eb# ls
hello_world.rs
0a0cc89c09eb# rustc hello_world.rs
0a0cc89c09eb# ls
hello_world  hello_world.rs
0a0cc89c09eb# ./hello_world
hello, world

この例では、rustcを使ってコンパイルをしていきましたが、本来なら下記の方法がメジャーです。

0a0cc89c09eb# cargo new --bin hello_world
     Created binary (application) `hello_world` package
0a0cc89c09eb# cd hello_world && tree .
.
|-- Cargo.toml
`-- src
    `-- main.rs

1 directory, 2 files
0a0cc89c09eb# cargo run
   Compiling hello_world v0.1.0 (/root/test/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34s
     Running `target/debug/hello_world`
Hello, world!

この場合はmain.rsが元から存在していて中身が

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

こんな感じで元から、hello worldが書いてあります。
最初に勉強しているうちはCargoを使うまでもないコードが大半なのですが、最終的にはCargoを使うことになるので、最初のうちからこれに慣れていた方がいいかもしれません。

参考

https://forge.rust-lang.org/infra/other-installation-methods.html
https://rust-lang.org/learn/get-started

广告
将在 10 秒后关闭
bannerAds