Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Macros

#![allow(unused)]
fn main() {
#[macro_export]
macro_rules! vec {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = Vec::new();
            $(
                temp_vec.push($x);
            )*
            temp_vec
        }
    };
}
}

Unsafe Code

unsafe {}:

  • 对原始指针进行解引用.
  • 调用不安全的函数 (包括 C 函数, 编译器内建指令, 原始分配器).
  • 实现不安全的特性.
  • 访问union字段.
  • 改变静态数据.

Attributes

  • Crate scope:#![crate_attribute].
  • Module/Function scope: #[item_attribute].
#![allow(unused)]
fn main() {
#[attribute = "value"]
#[attribute(key = "value")]
#[attribute(value)]
}

Crate Attributes

#![allow(unused)]
#![crate_type = "lib"]
#![crate_name = "rand"]
fn main() {
}

Linter Attributes

#![allow(unused)]
fn main() {
#[allow(dead_code)]
#[allow(unused)]
}

Compile Attributes

#[cfg(target_os = "linux")]
fn are_you_on_linux() {
    println!("You are running linux!")
}

#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
    println!("You are *not* running linux!")
}

fn main() {
    are_you_on_linux();

    if cfg!(target_os = "linux") {
        println!("Yes. It's definitely linux!");
    } else {
        println!("Yes. It's definitely *not* linux!");
    }
}

Comments

#![allow(unused)]
fn main() {
// Line Comments
/* Block Comments */
/// Document Line Comments
/** Document Block Comments */
//! Crate Line Comments
/*! Crate Block Comments */

/// [`Option`]
/// [`Type`](struct@Type)
/// [`Type`](fn@Type)

#[doc(alias = "alias" )]
}