# rust.hello **Repository Path**: zinface/rust.hello ## Basic Information - **Project Name**: rust.hello - **Description**: 一个 rust hello 程序 - **Primary Language**: Rust - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-05-25 - **Last Updated**: 2023-10-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: Rust ## README # rust.hello > 一个 rust hello 程序 ## 入门 快速配置 Rust 开发环境并编写一个小应用! 安装 Rust - 您可以在 Rust 演练场上在线试用 Rust 而无需在计算机上安装任何东西。\ https://play.rust-lang.org/ Rustup:Rust安装器和版本管理工具 - 安装 Rust 的主要方式是通过 Rustup 这一工具,它既是一个 Rust 安装器又是一个版本管理工具。 - 如果是正在运行 macOS、Linux 或其它类 Unix 系统。\ 要下载 Rustup 并安装 Rust,请在终端中运行以下命令,然后遵循屏幕上的指示。 ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # 默认安装目录为 ~/.cargo # 在 ~/.cargo/bin 目录下有以下程序, ~/.cargo $ tree . ├── bin │   ├── cargo │   ├── cargo-clippy │   ├── cargo-fmt │   ├── cargo-miri │   ├── clippy-driver │   ├── rls │   ├── rustc │   ├── rustdoc │   ├── rustfmt │   ├── rust-gdb │   ├── rust-lldb │   └── rustup └── env 安装程序已经将 '. $HOME/.cargo/env' 放入到 '~/.bashrc' 了 在 ~/.cargo/env 文件中有以下内容 #!/bin/sh # rustup shell setup # affix colons on either side of $PATH to simplify matching case ":${PATH}:" in *:"$HOME/.cargo/bin":*) ;; *) # Prepending path in case a system-installed rustc needs to be overridden export PATH="$HOME/.cargo/bin:$PATH" ;; esac 意: 这个文件自动化配置了rust开发环境 ``` Rust 是最新的吗? - Rust 的升级非常频繁。如果您安装 Rustup 后已有一段时间,那么很可能您的 Rust 版本已经过时了。运行 rustup update 获取最新版本的 Rust。 Cargo:Rust 的构建工具和包管理器 - cargo build 可以构建项目 - cargo run 可以运行项目 - cargo test 可以测试项目 - cargo doc 可以为项目构建文档 - cargo publish 可以将库发布到 crates.io。 要检查您是否安装了 Rust 和 Cargo,可以在终端中运行: - `cargo --version` 其它工具 - Rust 支持多种编辑器:\ VS CODE, SUBLIME TEXT, ATOM, INTELLIJ IDEA, ECLIPSE, VIM, EMACS, GEANY ## 创建新项目 我们将在新的 Rust 开发环境中编写一个小应用。首先用 Cargo 创建一个新项目。在您的终端中执行: - `cargo new hello-rust` 这会生成一个名为 hello-rust 的新目录,其中包含以下文件: - 结构 ``` hello-rust |- Cargo.toml |- src |- main.rs ``` - Cargo.toml 为 Rust 的清单文件。其中包含了项目的元数据和依赖库。 - src/main.rs 为编写应用代码的地方 cargo new 会生成一个新的“Hello, world!”项目!\ 我们可以进入新创建的目录中,执行下面的命令来运行此程序: - `cargo run` ``` $ cargo run Compiling hello-rust v0.1.0 (/Users/ag_dubs/rust/hello-rust) Finished dev [unoptimized + debuginfo] target(s) in 1.34s Running `target/debug/hello-rust` Hello, world! ``` ## 添加依赖 现在我们来为应用添加依赖。您可以在 [crates.io](https://crates.io),即 Rust 包的仓库中找到所有类别的库。在 Rust 中,我们通常把包称作“crates”。 在本项目中,我们使用了名为 ferris-says 的库。 我们在 Cargo.toml 文件中添加以下信息(从 crate 页面上获取): ```toml [dependencies] ferris-says = "0.2" ``` 接着运行: - `cargo build`\ …之后 Cargo 就会安装该依赖。 运行此命令会创建一个新文件 Cargo.lock,该文件记录了本地所用依赖库的精确版本。 要使用该依赖库,我们可以打开 main.rs,删除其中所有的内容(它不过是个示例而已),然后在其中添加下面这行代码: ```rs use ferris_says::say; ``` 这样我们就可以使用 ferris-says crate 中导出的 say 函数了。 ## 一个 Rust 小应用 现在我们用新的依赖库编写一个小应用。在 main.rs 中添加以下代码: ```rs use ferris_says::say; // from the previous step 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(); } ``` 保存完毕后,我们可以输入以下命令来运行此应用: - cargo run 如果一切正确,您会看到该应用将以下内容打印到了屏幕上: ``` ---------------------------- < Hello fellow Rustaceans! > ---------------------------- \ \ _~^~^~_ \) / o o \ (/ '_ - _' / '-----' \ ``` 这只螃蟹是谁? Ferris ? - Ferris 是 Rust 社区的非官方吉祥物。很多 Rust 程序员自称“Rustaceans”, 它与“crustacean”相似。 我们用“they”、“them”等代词,而不用带性别的代词来指代 Ferris。 - Ferris 与形容词“ferrous”相似,它的含义与铁有关。由于 Rust(锈)通常由铁形成, 因此它算得上是个吉祥物名字的有趣来源。 - 您可以在 http://rustacean.net/ 上找到更多 Ferris 的图片。 ![一个横着走的 Ferris 动图](https://www.rust-lang.org/static/images/ferris.gif) ---------------------------- ## Rust crates.io 索引镜像使用帮助([mirrors.bfsu.edu.cn][mirrors-bfsu-crates-io]) - 编辑 ~/.cargo/config 文件,添加以下内容: ```ini [source.crates-io] replace-with = 'tuna' [source.tuna] registry = "https://mirrors.bfsu.edu.cn/git/crates.io-index.git" ``` 该镜像可加快 cargo 读取软件包索引的速度。 [mirrors-bfsu-crates-io]: https://mirrors.bfsu.edu.cn/help/crates.io-index.git/ ``` $ cargo build Updating `tuna` index Fetch [======> ] 29.66%, 11.11MiB/s ``` ## Rustup - Rust 安装工具与镜像站配置 > 本节内出现的 export 的配置建议写入环境变量配置文件中 - 使用字节跳动提供的 rsproxy.cn > https://rsproxy.cn/ 步骤一:设置 Rustup 镜像 ```shell export RUSTUP_DIST_SERVER="https://rsproxy.cn" export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" ``` 步骤二:安装 Rust ``` curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh ``` 步骤三:设置 crates.io 镜像, 修改配置 ~/.cargo/config,已支持git协议和sparse协议,>=1.68 版本建议使用 sparse-index,速度更快。 ```ini [source.crates-io] replace-with = 'rsproxy-sparse' [source.rsproxy] registry = "https://rsproxy.cn/crates.io-index" [source.rsproxy-sparse] registry = "sparse+https://rsproxy.cn/index/" [registries.rsproxy] index = "https://rsproxy.cn/crates.io-index" [net] git-fetch-with-cli = true ``` - 使用 https://mirrors.cernet.edu.cn/ 校园网联合镜像站 > https://help.mirrors.cernet.edu.cn/rustup/ 以其中 USTC - 中国科学技术大学开源软件镜像 为例: ```shell # for bash export RUSTUP_UPDATE_ROOT=https://mirror.sjtu.edu.cn/rust-static/rustup export RUSTUP_DIST_SERVER=https://mirror.sjtu.edu.cn/rust-static ```