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 作为关键字的问题。

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

  • 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

features

Features - The Cargo Book

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

Log