安装,官方源国内一般连不上,需要使用镜像安装,有三个安装选项,默认就好。
会安装到当前用户下,不需要root用户或者sudo权限。
https://rustup.rs/#
[postgres@pg12 ~]$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
[postgres@pg12 ~]$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
[postgres@pg12 ~]$ export PATH="$HOME/.cargo/bin:$PATH"
[postgres@pg12 ~]$ curl https://sh.rustup.rs -sSf | sh
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"
安装后,会自动添加环境变量,不需要手动添加
~/.bash_profile
. "$HOME/.cargo/env"
当前shell会话可以手动设置环境变量,或者重新登陆shell
source "$HOME/.cargo/env"
配置国内仓库,类似于Go 的 https://goproxy.cn
[postgres@pg12 ~]$ cat ~/.cargo/config
[source.crates-io]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
[postgres@pg12 ~]$ rustc --version
rustc 1.73.0 (cc66ad468 2023-10-03)
[postgres@pg12 ~]$ cargo --version
cargo 1.73.0 (9c4383fb5 2023-08-26)
[postgres@pg12 ~]$ rustup --version
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.73.0 (cc66ad468 2023-10-03)`
卸载的方法
rustup self uninstall
hello world
[postgres@pg12 ~]$ mkdir rustlearn
[postgres@pg12 ~]$ cd rustlearn/
/home/postgres/rustlearn
[postgres@pg12 rustlearn]$ cargo new --bin hello
Created binary (application) `hello` package
编译运行
[postgres@pg12 hello]$ cargo run
Compiling hello v0.1.0 (/home/postgres/rustlearn/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.46s
Running `target/debug/hello`
Hello, world!
[postgres@pg12 hello]$ ./target/debug/hello
Hello, world!
清理
[postgres@pg12 hello]$ cargo clean
目录结构
[postgres@pg12 rustlearn]$ ll -a hello
total 8
drwxrwxr-x 4 postgres postgres 65 Oct 13 06:38 .
drwxrwxr-x 3 postgres postgres 19 Oct 13 06:38 ..
-rw-rw-r-- 1 postgres postgres 174 Oct 13 06:38 Cargo.toml
drwxrwxr-x 6 postgres postgres 103 Oct 13 06:38 .git
-rw-rw-r-- 1 postgres postgres 8 Oct 13 06:38 .gitignore
drwxrwxr-x 2 postgres postgres 21 Oct 13 06:38 src
[postgres@pg12 rustlearn]$ tree hello
hello
├── Cargo.toml
└── src
└── main.rs
初始化文件
[postgres@pg12 rustlearn]$ cat hello/Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[postgres@pg12 rustlearn]$ cat hello/src/main.rs
fn main() {
println!("Hello, world!");
}
cargo会自动初始化git,相当于
git init
[postgres@pg12 rustlearn]$ cat hello/.git/config
[core]
bare = false
repositoryformatversion = 0
filemode = true
logallrefupdates = true
[postgres@pg12 rustlearn]$ cd hello/
[postgres@pg12 hello]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# Cargo.toml
# src/
nothing added to commit but untracked files present (use "git add" to track)
参考文献
Rust安装及国内加速
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。