Cargo 使用

安装

使用包管理器

sudo apt install cargo  # Ubuntu
brew install rust       # macOS

将下面的命令加入 ~/.bashrc

export PATH="$HOME/.cargo/bin:$PATH"

使用官方脚本

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

卸载:

rustup self uninstall

参考:

使用

Cargo 是 Rust 的构建工具和包管理器。

cargo init               # 在当前目录初始化 Rust 项目
cargo build              # 编译项目
cargo run                # 编译并运行项目
cargo test               # 运行项目中的测试用例
cargo clean              # 清除构建产物
cargo doc                # 生成项目文档
cargo update             # 更新依赖到最新的兼容版本
cargo install <crate>    # 全局安装一个可执行的 Rust 包
cargo uninstall <crate>  # 卸载已安装的全局包
cargo publish            # 发布包到 crates.io

Cargo 中的软件包叫 crates

Hello World

main.rs:

fn main() {
    println!("Hello, World!");
}
rustc main.rs  # 编译
./main         # 运行

参见:

换源

编辑 ~/.cargo/config.toml,添加以下内容:

[source.crates-io]
replace-with = 'mirror'

[source.mirror]
registry = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"

[registries.mirror]
index = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"

参考:

posted @ 2024-07-24 01:28  Undefined443  阅读(35)  评论(0)    收藏  举报