简介
和C语言过日子太苦了,找了个Go来作伴,现在来体验下Rust这个小三。
Rust连续多年被评为“最受喜爱的编程语言”,好吧,让我看下这小三有什么魅力这么招人喜欢。
安装
执行如下命令安装rust环境和cargo命令,过程中按回车选择默认安装方式即可。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
[xiaofeng@localhost Rust]$ 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/xiaofeng/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/home/xiaofeng/.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/xiaofeng/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/xiaofeng/.profile
/home/xiaofeng/.bash_profile
/home/xiaofeng/.bashrc
/home/xiaofeng/.zshenv
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 standard installation (default - just press enter)
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'
712.1 KiB / 712.1 KiB (100 %) 25.5 KiB/s in 30s ETA: 0s
info: latest update on 2024-02-08, rust version 1.76.0 (07dca489a 2024-02-04)
info: downloading component 'cargo'
8.5 MiB / 8.5 MiB (100 %) 559.7 KiB/s in 4m 11s ETA: 0s
info: downloading component 'clippy'
2.1 MiB / 2.1 MiB (100 %) 601.8 KiB/s in 3s ETA: 0s
info: downloading component 'rust-docs'
14.7 MiB / 14.7 MiB (100 %) 2.1 MiB/s in 8s ETA: 0s
info: downloading component 'rust-std'
23.9 MiB / 23.9 MiB (100 %) 1.9 MiB/s in 11s ETA: 0s
info: downloading component 'rustc'
62.3 MiB / 62.3 MiB (100 %) 5.5 MiB/s in 30s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
14.7 MiB / 14.7 MiB (100 %) 6.9 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
23.9 MiB / 23.9 MiB (100 %) 9.6 MiB/s in 2s ETA: 0s
info: installing component 'rustc'
62.3 MiB / 62.3 MiB (100 %) 10.9 MiB/s in 5s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.76.0 (07dca489a 2024-02-04)
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, you need to source
the corresponding env file under $HOME/.cargo.
This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish" # For fish
[xiaofeng@localhost Rust]$
示例
源码
编写代码main.rs
fn main(){
println!("hello");
}
编译
使用rustc命令进行编译
rustc main.rs
运行
运行编译后的main程序
./main
[xiaofeng@localhost hello]$ ./main
hello
[xiaofeng@localhost hello]$