Rust 初探,跑起你的第一个Rust程序

5,065 阅读3分钟

本文已参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

Rust

官网

Rust 语言是一种高效、可靠的通用高级语言。其高效不仅限于开发效率,它的执行效率也是令人称赞的,是一种少有的兼顾开发效率和执行效率的语言。Rust 语言由 Mozilla 开发,最早发布于 2014 年 9 月。Rust 的编译器是在 MIT License 和 Apache License 2.0 双重协议声明下的免费开源软件。截至目前( 2020 年 1 月)最新的编译器版本是 1.41.0。

安装

安装程序和版本管理工具

在macOS、 Linux 或类 unix 操作系统下载 Rustup 并安装 Rust,在终端中运行以下命令,然后按照屏幕上的说明进行操作。 如果使用 Windows,请参阅“其他安装方法”。

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

更新

rustup update

版本查看

cargo --version

Cargo: rust 建立工具和包管理器

安装 Rustup 的时候,会得到最新的稳定版本的 Rust 构建工具和软件包管理器,称为 Cargo。

Cargo 命令:

cargo build     建立项目
cargo run       运行项目
cargo test      测试项目
cargo doc       构建文档
cargo publish   项目发布出到crates.io

创建一个新项目

用Rust开发环境编写一个小型应用程序。首先,我们将使用 Cargo 为我们制作一个新项目。
cargo new hello-rust
将生成一个名为 hello-rust 的新目录,其中包含以下文件:
hello-rust
|- Cargo.toml  // 清单文件,用于保存项目的元数据以及依赖项
|- src
  |- main.rs  // 编写应用程序代码的地方

运行你的rust

hello-rust根目录下运行
cargo run
运行结果
Compiling hello-rust v0.1.0 (/Users/guokai/Desktop/GUOKAI/Front-end-Development-Notes/Rust/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.63s
    Running `target/debug/hello-rust`
Hello, world!

添加依赖项

让我们向应用程序添加一个依赖项。您可以在 crates.io 上找到各种类型的库,这是 Rust 的包注册表。在rust中,我们经常把包装称为“crates”

在你的项目中添加 ferris-says。

hello-rust/Cargo.toml 文件中添加

[dependencies]
ferris-says = "0.2"
接下来可以安装依赖
  • 创建了一个新文件 Cargo.lock。此文件是本地使用的依赖项的确切版本的日志。
cargo build
  • 安装完成
    Updating crates.io index
warning: spurious network error (2 tries remaining): error sending data: Broken pipe; class=Net (12)
  Downloaded ferris-says v0.2.1
  Downloaded smawk v0.3.1
  Downloaded unicode-width v0.1.9
  Downloaded smallvec v0.4.5
  Downloaded textwrap v0.13.4
  Downloaded 5 crates (97.7 KB) in 3.52s
   Compiling unicode-width v0.1.9
   Compiling smawk v0.3.1
   Compiling smallvec v0.4.5
   Compiling textwrap v0.13.4
   Compiling ferris-says v0.2.1
   Compiling hello-rust v0.1.0 (/Users/guokai/Desktop/GUOKAI/Front-end-Development-Notes/Rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3m 10s

使用依赖

  • 要使用这个依赖项,我们可以打开 main.rs,删除其中的所有内容,然后添加以下行:
use ferris_says::say;

以上代码意思是 使用say函数

创建一个小的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 

运行成功结果

假设一切进展顺利,屏幕上打印如下内容:

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/hello-rust`
 __________________________
< Hello fellow Rustaceans! >
 --------------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \

后续新功能增加 Github github.com/guokaigdg/m…

2021/09/28「 前端 」当Rust 遇到 YEW

感兴趣的可以给个star, 一起来玩啊

「欢迎在评论区讨论,掘金官方将在掘力星计划活动结束后,在评论区抽送100份掘金周边,抽奖详情见活动文章」