组件 类比
rustc gcc, g++
cargo make, cmake, ninja, npx, npi
create package, library

Rust 里变量默认是不可变的,引用默认也是不可变的。

Rust doesn't use header files; instead, the equivalent information is stored as metadata in the compiled crates.

no_std cannot use println.

  • Rust 允许使用 r# 前缀来引用与关键字同名的标识符,因此 r#in 被用来避免 in 作为关键字的问题。

学习 Rust 要点:

  • 变量名称遮蔽;
  • 函数可以先使用再定义,只要在一个作用域;
  • 注意表达式和语句的区别。

Rust 支持变量名称遮蔽:

fn main() {
    let x = 5;
    let x = x + 1;
    
    {
        let x = x * 2;
        // The value of x in the inner scope is: 12
        println!("The value of x in the inner scope is: {}", x);
    }

    // The value of x is: 6
    println!("The value of x is: {}", x);
}

Rust 学习资料

Rust 程序设计语言 - Rust 程序设计语言 中文版

简单易懂,讲解很详细,适合对 Rust 零基础入门。

常量和不可变变量的区别

  • 常量可以在任意作用域内声明,包括全局作用域,这对于代码中很多部分都需要知道一个值的情况特别有用。
  • 常量只能设置为常量表达式,而不能是函数调用的结果或是只能在运行时计算得到的值
  • Rust 常量的命名约定是全部字母都使用大写,并使用下划线分隔单词:THREE_HOURS_IN_SECONDS

版本管理和 Cargo.lock

这里讲的不错:

猜数字游戏 - Rust 程序设计语言 中文版

Cfg

条件编译用的。

Loop in rust

Rust provides a loop keyword to indicate an infinite loop.

fn main() {
    // Infinite loop
    loop {
        count += 1;
        if count == 3 {
            continue;
        }
        if count == 5 {
            break;
        }
    }
}

Rust compile process

Compilation begins when a user writes a Rust source program in text and invokes the rustc compiler on it.

Rust Linkage

Cargo.toml

Each Cargo.toml correspondings to a package.

The Cargo.toml file for each package is called its manifest.

Crate

crate 是一个 Rust 代码包。

  • 我们正在构建的项目是一个二进制 crate,它生成一个可执行文件。
  • rand crate 是一个库 crate,库 crate 可以包含任意能被其他程序使用的代码,但是不能独自执行。
  • A crate is a compilation unit in Rust.
  • A crate is the output artifact of the compiler.

A package may contain one or more crates, for example one library crate, named as the package name and zero or more executable crates

Whenever rustc some_file.rs is called, some_file.rs is treated as the crate file.

Extern crate (obsolete?)

An extern crate declaration specifies a dependency on an external crate. (Why not listing it in cargo.toml?)

extern crate is no longer needed in 99% of circumstances. From Path and module system changes - The Rust Edition Guide

Since Rust 2018 you're only required to add external dependencies to your Cargo.toml, so you no longer need to use extern crate foo. use works the same as before.

rust - What's the difference between use and extern crate? - Stack Overflow

Core crate 核心库

Rust 语言的语法由核心库和标准库共同提供。 其中 Rust 核心库是标准库的基础。

Std crate 标准库

Keywords

Match

like a C switch.

match - Rust By Example

Class, struct and impl

In Rust, we don’t have class keyword but we have struct and impl so we can mix them to do this:

// struct + impl = "class"
struct Dog;

impl Dog {
 fn bark(&self) {
   println!(baf!);
 }
}

fn main() {
 let d = Dog;
 d.bark();
}

Enum

enum 里为什么会有下面这种类似函数的东西?

enum MigrationState {
    WaitForRequest,
    Operate(MigrationOperation),
    Complete(RequestInformation),
}

这是一种把 enum 和 class 结合起来的方式,如果我们要表示一个 ip 地址:

enum IpAddrKind {
    V4,
    V6,
}

struct IpAddr {
    kind: IpAddrKind,
    address: String,
}

在 Rust 里,我们可以简化为这种方式:

enum IpAddr {
    V4(String),
    V6(String),
}

let home = IpAddr::V4(String::from("127.0.0.1"));
let loopback = IpAddr::V6(String::from("::1"));

Trait(显著特点、特征)

不同的类共享相同的方法。trait 类似于其他语言中的常被称为 接口(interfaces)的功能,虽然有一些不同。

trait 告诉 Rust 编译器某个特定类型拥有可能与其他类型共享的功能。可以通过 trait 以一种抽象的方式定义共享的行为。

pub trait Summary {
    fn summarize(&self) -> String;
}

pub struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}, by {} ({})", self.headline, self.author, self.location)
    }
}

pub struct Tweet {
    pub username: String,
    pub content: String,
    pub reply: bool,
    pub retweet: bool,
}

impl Summary for Tweet {
    fn summarize(&self) -> String {
        format!("{}: {}", self.username, self.content)
    }
}

Cargo

  • cargo build:build dev
  • cargo build --release:build release
  • cargo run:build and run
  • cargo check:cargo check 要比 cargo build 快得多,因为它省略了生成可执行文件的步骤。如果你在编写代码时不断检查你的代码,那么使用 cargo check 命令可以加快这个过程!为此很多 Rustacean 编写代码时会定期运行 cargo check 以确保它们可以编译。当准备好使用可执行文件时才运行 cargo build。
  • cargo doc --open:可以直接打开所有依赖组件所组成的文档,查询文档非常方便。

features

Features - The Cargo Book

cargo xbuild -p test-td-payload --target x86_64-unknown-none --release --features=main,tdx --no-default-features

Log