• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Rust 入门笔记

武飞扬头像
云满笔记
帮助2

1. Rust 入门笔记

1.1. Rustup: Rust安装器和版本管理工具

安装 Rust 的主要方式是通过 Rustup 这一工具, 它既是一个 Rust 安装器又是一个版本管理工具。

您似乎正在运行 macOS、Linux 或其它类 Unix 系统。要下载 Rustup 并安装 Rust, 请在终端中运行以下命令, 然后遵循屏幕上的指示。如果您在 Windows 上, 请参见 “其他安装方式”。

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

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /Users/mac/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /Users/mac/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo’s bin directory, located at:

  /Users/mac/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /Users/mac/.profile
  /Users/mac/.zshenv

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: aarch64-apple-darwin
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

......

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo’s bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

要检查是否正确安装了 Rust,打开命令行并输入:

$ rustc --version

你应该可以看到按照以下格式显示的最新稳定版本的版本号、对应的 Commit Hash 和 Commit 日期:

rustc x.y.z (abcabcabc yyyy-mm-dd)

如果看到了这样的信息,就说明 Rust 已经安装成功了!

1.1.1. Rust 是最新的吗?

Rust 的升级非常频繁。如果您安装 Rustup 后已有一段时间, 那么很可能您的 Rust 版本已经过时了。运行 rustup update 获取最新版本的 Rust。

若要卸载 Rust 和 rustup,请在命令行中运行如下卸载脚本:

$ rustup self uninstall

1.2. Cargo: Rust 的构建工具和包管理器

您在安装 Rustup 时, 也会安装 Rust 构建工具和包管理器的最新稳定版, 即 Cargo。Cargo 可以做很多事情:

  • cargo build 可以构建项目
  • cargo run 可以运行项目
  • cargo test 可以测试项目
  • cargo doc 可以为项目构建文档
  • cargo publish 可以将库发布到 crates.io

要检查您是否安装了 Rust 和 Cargo, 可以在终端中运行:

cargo --version

More: cargo 手册

1.2.1. 创建新项目

我们将在新的 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!

1.2.2. 添加依赖

现在我们来为应用添加依赖。您可以在 crates.io, 即 Rust 包的仓库中找到所有类别的库。在 Rust 中, 我们通常把包称作 “crates”。

在本项目中, 我们使用了名为 ferris-says 的库。

我们在 Cargo.toml 文件中添加以下信息(从 crate 页面上获取):

[dependencies]
ferris-says = "0.2"

接着运行:

cargo build

…之后 Cargo 就会安装该依赖。

运行此命令会创建一个新文件 Cargo.lock, 该文件记录了本地所用依赖库的精确版本。

要使用该依赖库, 我们可以打开 main.rs, 删除其中所有的内容(它不过是个示例而已), 然后在其中添加下面这行代码:

use ferris_says::say;

这样我们就可以使用 ferris-says crate 中导出的 say 函数了。

1.2.3. 一个 Rust 小应用

现在我们用新的依赖库编写一个小应用。在 main.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 的图片。

1.3. 了解更多!

您已经是一名 Rustacean 了! 欢迎! 我们很高兴您的加入! 当您准备好后, 跳转到学习页面, 您可以在那里找到大量的文档, 它们可以帮助您继续 Rust 之旅。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaeeak
系列文章
更多 icon
同类精品
更多 icon
继续加载