Rust安装程序和版本管理工具
RUSTUP_HOME
Rustup元数据和工具链将被安装到Rustup主目录中
默认: %USERPROFILE%\.rustup
Rust构建工具和包管理器
CARGO_HOME
Cargo主目录
cargo、rustc、rustup等命令将被添加到Cargo的bin
目录,位于:
%USERPROFILE%\.cargo\bin
cargo [+toolchain] [OPTIONS] [SUBCOMMAND]
编译当前包
运行项目
运行测试
构建这个包及其依赖项的文档
将库发布到crates.io
Install a Rust binary. Default location is $HOME/.cargo/bin
为了测试已经安装了Rust和Cargo,可以在终端上运行:
cargo --version
#cargo 1.65.0 (4bc8f24d3 2022-10-20)
为我们生成“Hello, world!”项目!我们可以通过移动到我们创建的新目录并在终端中运行这个程序: cargo run
init : 在现有目录中创建一个新的 cargo 包
Add dependencies to a manifest file
首先,我们将使用Cargo为我们创建一个新项目
cargo new hello-rust
#Created binary (application) `hello-rust` package
这将生成一个名为hello-rust的新目录,包含以下文件:
hello-rust
|- Cargo.toml
|- src|- main.rs
Cargo.toml
是Rust的清单文件。它是保存项目元数据以及依赖项的地方
src/main.rs
是我们编写应用程序代码的地方
你可以在 crates.io
(Rust的包注册中心)上找到各种各样的库。 在Rust中,我们经常将包称为“crate”。
在Cargo.toml文件中,添加以下信息(可从crate 页面):
[dependencies]
ferris-says = "0.2"
Now we can run:
cargo build
Cargo将为我们安装依赖项。
您将看到运行此命令为我们创建了一个新文件Cargo.lock。该文件是本地使用的依赖项的确切版本的日志。
在main.rs中
use ferris_says::say;
use std::io::{stdout, BufWriter};fn main() {let stdout = stdout();let message = String::from("Hello fellow Rustaceans!");let width = message.chars().count();let mut writer = BufWriter::new(stdout.lock());say(message.as_bytes(), width, &mut writer).unwrap();
}