Here are the steps to set up your Rust environment. For more information on Rust, see here (https://www.rust-lang.org).
The PC itself is a Mac, but I'm running it on Docker because it was troublesome to reinstall.
Execution environment
Docker version 19.03.5, build 633a0ea
image: ubuntu 19.04
Follow the Official Installation Method to install. \
You can go to the official website and have it installed while reading the explanation, but if you are troublesome
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
You can install it by running on the terminal. At the time of installation, it will be as follows.
Installation
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 #<-It doesn't have to be 1 here, but it's safer to set it to 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
If you want to pass the path with the login shell, as you can see at the end
source $HOME/.cargo/env
Please run the. If you log in again, the pass will pass, so it is not required.
The above installation method is the installation method on Linux / Unix. Click here to start the installer and you can install without any problem. I think. Recently, it seems that linux commands can be used on windows with WSL, so it may be possible to execute it on WSL. If you have any questions, please feel free to comment or email.
Now that the execution environment is ready, let's actually write the program.
hello_world.rs
fn main(){
println!("hello, world");
}
The editor can be anything, so save the above code and save it with a name. \
At this time, you will need the save destination, so make a note or copy it.
After saving, open the terminal and try the matter.
Execution result 1
0a0cc89c09eb# ls
hello_world.rs
0a0cc89c09eb# rustc hello_world.rs
0a0cc89c09eb# ls
hello_world hello_world.rs
0a0cc89c09eb# ./hello_world
hello, world
In this example, I used rustc
to compile, but the following method is the major method.
Execution result 2
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!
In this case, main.rs already exists and the contents are
rust:./src/main.rs
fn main() {
println!("Hello, world!");
}
From the beginning, hello world is written like this. Most of the code doesn't need to use Cargo while studying for the first time, but in the end you will use Cargo, so you may want to get used to it from the beginning.
https://forge.rust-lang.org/infra/other-installation-methods.html https://rust-lang.org/learn/get-started
Recommended Posts