Rust 基本
title: Rust 基本 id: 47f43ff5bad7f8fcaf6d4d3e99d398e3 tags: [] date: 2000/01/01 00:00:00 updated: 2023/05/04 17:14:12 isPublic: true --#|[分隔]|#--
Rust 基本
官方网站:https://www.rust-lang.org/
官方文档:https://doc.rust-lang.org/book/(英文)
官方文档的社区翻译:https://kaisery.github.io/trpl-zh-cn/ (中文)
依赖的托管平台:https://crates.io/
社区翻译的源码(文档的社区翻译的源码):https://github.com/KaiserY/trpl-zh-cn/tree/main (clone后可本地启动,来查看最新文档)
Rust使用的依赖包的统一分发网:https://crates.io。
和上面配套的依赖包的文档网:https://docs.rs。
安装
可在官网安装页面按照说明安装:https://www.rust-lang.org/tools/install。
或者直接使用下面这个指令,这个指令就是从官网抄来的:
可能会报错,因为是使用了低版本curl不支持的配置,但降低配置设定,或者不使用那个工具就行。
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
# 可能会报错
# curl: (4) A requested feature, protocol or option was not found built-in in this libcurl due to a build-time decision.
# 那就用这个
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 如果还不行,就是下面这个
curl https://sh.rustup.rs -sSf | sh
完成安装后,可通过一下两个指令查看是否安装完成:
rustc --version
# 输出
rustc 1.66.1 (90743e729 2023-01-10)
cargo --version
# 输出
cargo 1.66.1 (ad779e08b 2023-01-10)
rustc 就是 Rust,cargo 是 Rust 的包管理工具。
相当于 Node 和 npm。
更新
rustup update
卸载
rustup self uninstall
最基本的编译和运行
编写一个名为 main.rs 的文件,内容:
fn main() {
println!("Hello, world!");
}
保存后,在终端中使用 rustc 指令执行此文件:
rustc main.rs
然后会发现,main.rs 的同级文件夹中,出现了一个名为 main 的无后缀名文件,这是把 main.rs 编译后生成的二进制文件,可以使用终端执行这个文件:
./main
# 打印
Hello, world!
以上就是最基本的 rust 使用,但没人这么用。
使用 cargo 创建Rust项目
使用 cargo 创建项目
使用终端在某目录下执行:
cargo new rust-study
此目录下会出现 rust-study 这个文件夹,这就是 cargo 创建的 Rust 项目。
里面只有最基本的三个文件 Cargo.toml、.gitignore、src 文件夹。
其中文件 src/main.rs 是整个项目的入口主文件,编译时会默认编译此文件。
且此项目下已经自动初始化了一个 git 仓库。
使用 cargo 在某文件夹下初始化项目
使用终端进入某文件夹,然后执行:
cargo init
此文件夹会自动创建上面的那三个文件,此文件夹会自动变为成为 Rust 项目。
如果此文件夹不存在 git 仓库,也会自动初始化 git 存库,否则就不会。
cargo下载依赖时使用国内镜像源
在 Cargo.toml 从 crates.io 引入新的依赖时,默认的下载位置是国外,导致下载慢甚至直接下载超时而失败。
可以修改使用国内源,在 ~/.cargo/ 下新建 config 文件:
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 指定镜像(下面几个里选一个)
replace-with = 'tuna'
# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# rustcc社区
[source.rustcc0]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
[source.rustcc1]
registry="git://crates.rustcc.cn/crates.io-index"
[source.rustcc2]
registry="git://crates.rustcc.com/crates.io-index"
这段配置是配置了多个国内源,可以通过配置上面 replace-with 字段来指定使用哪一个。
cargo 编译项目
某个 Rust 项目文件夹名称为 rust-study,使用终端在此项目中运行:
cargo build
会编译项目,生成文件 Cargo.lock 和文件夹 target。
其中文件 target/debug/rust-study 是项目编译后的二进制文件,可以使用一下命令直接运行:
./target/debug/rust-study
注意:上面的编译指令只是针对开发时,如果项目最终准备好发布时,使用以下命令:
cargo build --release
这个指令会优化编译,时间更长,但编译后的二进制文件的执行效率会更高。
此命令将在 target/release 而不是 target/debug 中创建可执行文件。
cargo 运行项目
在某个 Rust 项目文件夹中运行:
cargo run
会自动进行编译并运行,一般开发都会使用此命令。
cargo 检查项目
在某个 Rust 项目文件夹中运行:
cargo check
会检查项目代码是否合法,但不会真的去编译,更快速。
Last updated
Was this helpful?