官方文档
https://www.rust-lang.org/zh-CN/ https://www.rust-lang.org/zh-CN/learn
软件包仓库
crates.io,即 Rust 包的仓库中找到所有类别的库
是否安装成功
cargo --version
cargo 1.58.0 (7f08ace4f 2021-11-24)
基础命令
cargo check在不生成二进制文件的情况下构建项目来检查错误cargo build可以构建项目(编译)cargo run可以运行项目cargo test可以测试项目cargo doc可以为项目构建文档cargo publish可以将库发布到 crates.iorustup update更新版本cargo new xxx创建项目cargo run编译并运行(一步构建并运行项目)cargo build会在target/debug目录下生成可执行文件cargo build --release会在target/relaease目录下生成可执行文件
hello world 1
创建一个新项目: cargo new hello-rust
生成一个名为 hello-rust 的新目录, 目录结构如下:
hello-rust|- Cargo.toml //Rust 的清单文件。其中包含了项目的元数据和依赖库|- src|- main.rs //为编写应用代码的地方
cargo run 运行项目
$ cargo runCompiling hello-rust v0.1.0 (/Users/ag_dubs/rust/hello-rust)Finished dev [unoptimized + debuginfo] target(s) in 1.34sRunning `target/debug/hello-rust`Hello, world!
问题记录
windows执行cargo run 后出现如下错误:please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option
解决方案:
first: rustup toolchain install stable-x86_64-pc-windows-gnu
then:
rustup default stable-x86_64-pc-windows-gnu
end:
cargo build / cargo run
hello world 2
在Cargo.toml文件[dependencies] 下 添加依赖 ferris-says = "0.2"
修改src/main.rs内容为:
use ferris_says::say; // from the previous stepuse 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();}
然后 cargo run,输出如下:
__________________________< Hello fellow Rustaceans! >--------------------------\\_~^~^~_\) / o o \ (/'_ - _'/ '-----' \
另外:允许未使用的方法
写在文件开头,可过滤过掉该项提示 #![allow(unused)]
