1.安装
$ curl https://sh.rustup.rs -sSf | sh
2.更新
$ rustup update
3.卸载
$ rustup update
4.设置环境变量
$ export PATH=$PATH:/home/opencl/.cargo/bin
或将
export PATH=$PATH:/home/opencl/.cargo/bin
添加为 /etc/profile or ~/.profile 最后一行,并source 之
5.看本地文档
$ rustup doc
6.示例编译
$ cat hello_world.rs
fn main(){
println!("Hello world!");
}
$ rustc ./hello_world.rs
$ ls
hello_world hello_world.rs
$ ./hello_world
Hello world!
$
7.测试cargo
$ cargo --version
cargo 1.60.0 (d1fd9fe 2022-03-01)
8.用 cargo 创建 rust 项目
$ cargo new hello_cargo
Created binary (application) `hello_cargo` package
$ ll
total 12
drwxrwxr-x 3 opencl opencl 4096 5月 18 11:23 ./
drwxrwxr-x 4 opencl opencl 4096 5月 18 11:22 ../
drwxrwxr-x 4 opencl opencl 4096 5月 18 11:23 hello_cargo/
$ ls ./hello_cargo/
Cargo.toml src
$ ls ./hello_cargo/src/
main.rs
$ cat ./hello_cargo/src/main.rs
fn main() {
println!("Hello, world!");
}
$
9. 运行cargo项目
$ cd hello_cargo/
$ cargo run
Compiling hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.38s
Running `target/debug/hello_cargo`
Hello, world!
$
10.编写程序时检查语法
$ cargo check
Checking hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
11.编译发布版本
$ cargo build --release
Compiling hello_cargo v0.1.0 (/home/opencl/ex/rustEx/helloCargo/hello_cargo)
Finished release [optimized] target(s) in 0.12s
$ ls
Cargo.lock Cargo.toml src target
$ ls target/
CACHEDIR.TAG debug/ release/ .rustc_info.json
$ ls target/release/
build deps examples hello_cargo hello_cargo.d incremental
$
$ cd ./target/release/
$ ./hello_cargo
Hello, world!
$
12.小案例
$ cargo update
Updating crates.io index
$ cargo run