rust学习-rust中的保留字

rust学习-rust中的保留字

保留字是语言中预定义的标识符,不能用作变量名、函数名或其他自定义标识符,Rust的保留字大致可以分为两类:已使用的保留字和未来可能使用的保留字

已使用的保留字

  1. as:用于类型转换
rust 复制代码
let num: i32 = 5;
let float_num = num as f64;
  1. break:用于终止循环
rust 复制代码
loop {
    println!("再次循环");
    break;
}
  1. const:用于定义常量
rust 复制代码
const MAX_POINTS: u32 = 100_000;
  1. continue:用于跳过当前循环的剩余部分并开始下一次迭代
rust 复制代码
for i in 0..10 {
    if i % 2 == 0 {
        continue;
    }
    println!("奇数: {}", i);
}
  1. crate:用于指定 crate 根模块
rust 复制代码
// 在 Cargo.toml 中定义 crate
// Cargo.toml
[package]
name = "my_crate"
version = "0.1.0"
  1. dyn:用于动态分发的 trait 对象
rust 复制代码
trait Draw {
    fn draw(&self);
}

struct Rectangle;

impl Draw for Rectangle {
    fn draw(&self) {
        println!("绘制矩形");
    }
}

fn draw_item(item: &dyn Draw) {
    item.draw();
}

let rectangle = Rectangle;
draw_item(&rectangle);
  1. else:用于条件语句中的 if 之后的分支
rust 复制代码
let x = 5;
if x == 5 {
    println!("x 是 5");
} else {
    println!("x 不是 5");
}
  1. enum:用于定义枚举类型
rust 复制代码
enum Color {
    Red,
    Green,
    Blue,
}

let color = Color::Red;
  1. extern:用于定义外部函数或链接外部库
rust 复制代码
extern "C" {
    fn printf(format: *const u8, ...) -> i32;
}

fn main() {
    unsafe {
        printf(b"Hello, World!\0".as_ptr());
    }
}
  1. false:布尔常量,表示假
rust 复制代码
let is_false = false;
  1. fn:用于定义函数
rust 复制代码
fn add(a: i32, b: i32) -> i32 {
    a + b
}

let sum = add(5, 3);
  1. for:用于循环
rust 复制代码
for i in 0..3 {
    println!("i: {}", i);
}
  1. if:用于条件语句
rust 复制代码
let x = 5;
if x > 0 {
    println!("x 是正数");
}
  1. impl:用于实现方法或 trait
rust 复制代码
struct Point {
    x: i32,
    y: i32,
}

impl Point {
    fn new(x: i32, y: i32) -> Point {
        Point { x, y }
    }

    fn distance(&self, other: &Point) -> f64 {
        (((self.x - other.x).pow(2) + (self.y - other.y).pow(2)) as f64).sqrt()
    }
}

let p1 = Point::new(0, 0);
let p2 = Point::new(3, 4);
println!("距离: {}", p1.distance(&p2));
  1. in:用于 for 循环或 match 语句
rust 复制代码
for i in 0..3 {
    println!("i: {}", i);
}

match 2 {
    1 => println!("一是 1"),
    2 => println!("二是 2"),
    _ => println!("其他"),
}
  1. let:用于声明变量
rust 复制代码
let x = 5;
let y: i32 = 10;
  1. loop:用于无限循环
rust 复制代码
loop {
    println!("再次循环");
    break;
}
  1. match:用于模式匹配
rust 复制代码
let x = 2;
match x {
    1 => println!("一是 1"),
    2 => println!("二是 2"),
    _ => println!("其他"),
}
  1. mod:用于定义模块
rust 复制代码
mod my_module {
    pub fn say_hello() {
        println!("Hello from my_module!");
    }
}

fn main() {
    my_module::say_hello();
}
  1. move:用于闭包捕获环境
rust 复制代码
let x = 5;
let y = 10;

let closure = move || {
    println!("x: {}, y: {}", x, y);
};

closure();
  1. mut:用于声明可变变量
rust 复制代码
let mut x = 5;
x = 10;
  1. pub:用于声明公共项
rust 复制代码
pub fn public_function() {
    println!("这是公共函数");
}

mod my_mod {
    pub fn public_function() {
        println!("这是 my_mod 中的公共函数");
    }
}
  1. ref:用于在模式匹配中获取引用
rust 复制代码
let x = 5;
let ref_x = &x;

match ref_x {
    ref r => println!("r 是引用: {:?}", r),
}
  1. return:用于从函数返回值
rust 复制代码
fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

let sum = add(5, 3);
  1. self:用于表示当前实例
rust 复制代码
struct Point {
    x: i32,
    y: i32,
}

impl Point {
    fn new(x: i32, y: i32) -> Self {
        Point { x, y }
    }

    fn distance_from_origin(&self) -> f64 {
        (self.x.pow(2) + self.y.pow(2)) as f64
    }
}

let p = Point::new(3, 4);
println!("距离原点: {}", p.distance_from_origin());
  1. Self:用于表示当前类型的自身
rust 复制代码
struct Point {
    x: i32,
    y: i32,
}

impl Point {
    fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
}

let p = Point::new(3, 4);
  1. static:用于定义静态变量
rust 复制代码
static X: i32 = 5;

fn main() {
    println!("X: {}", X);
}
  1. struct:用于定义结构体
rust 复制代码
struct Point {
    x: i32,
    y: i32,
}

let p = Point { x: 3, y: 4 };
  1. super:用于访问父模块
rust 复制代码
mod outer {
    pub fn outer_fn() {
        println!("outer_fn");
    }

    mod inner {
        pub fn inner_fn() {
            super::outer_fn();
            println!("inner_fn");
        }
    }
}

fn main() {
    outer::inner::inner_fn();
}
  1. trait:用于定义 trait

trait(特性)是一种定义共享行为的方式,它类似于其他编程语言中的接口(interface),但更加灵活和强大,trait可定义一组方法签名,这些方法可以在不同的类型中实现,通过这种方式,Rust能够实现多态性,并确保类型安全

rust 复制代码
trait Draw {
    fn draw(&self);
}

struct Rectangle;

impl Draw for Rectangle {
    fn draw(&self) {
        println!("绘制矩形");
    }
}

fn draw_item(item: &impl Draw) {
    item.draw();
}

let rectangle = Rectangle;
draw_item(&rectangle);
  1. true:布尔常量,表示真
rust 复制代码
let is_true = true;
  1. type:用于定义类型别名
rust 复制代码
type Kilometers = i32;

let distance: Kilometers = 5;
  1. unsafe:用于编写不安全的代码
rust 复制代码
unsafe fn dangerous() {
    println!("这是不安全的代码");
}

fn main() {
    unsafe {
        dangerous();
    }
}
  1. use:用于引入模块
rust 复制代码
mod my_mod {
    pub fn say_hello() {
        println!("Hello from my_mod!");
    }
}

use my_mod::say_hello;

fn main() {
    say_hello();
}
  1. where:用于指定 trait 约束
rust 复制代码
fn some_func<T: std::fmt::Display>(t: T) where T: std::fmt::Debug {
    println!("t: {:?}", t);
}

fn main() {
    some_func(5);
}

未来可能使用的保留字

  1. abstract
  2. become
  3. box
  4. do
  5. final
  6. macro
  7. override
  8. priv
  9. typeof
  10. unsized
  11. virtual
  12. yield

这些保留字目前在 Rust 中没有具体的用途,但它们被预留以备将来扩展语言时使用

相关推荐
努力努力再努力wz10 分钟前
【C++进阶系列】:万字详解红黑树(附模拟实现的源码)
java·linux·运维·c语言·开发语言·c++
会飞的土拨鼠呀14 分钟前
Linux负载如何判断服务器的压力
linux·服务器·php
枫fengw16 分钟前
9.8 C++
开发语言·c++
王璐WL17 分钟前
【C语言入门级教学】内存函数
c语言·开发语言·算法
啃啃大瓜18 分钟前
python常量变量运算符
开发语言·python·算法
斯普信专业组21 分钟前
多输入(input)多输出(output)验证
运维·服务器·网络·fluent-bit
就是帅我不改39 分钟前
10万QPS压垮系统?老司机一招线程池优化,让性能飞起来!
后端·面试·github
uzong40 分钟前
系统稳定性保障:研发规约V1.0
后端
Ray6640 分钟前
log4j2.xml配置文件详解
后端
CodingCos43 分钟前
【芯片设计-信号完整性 SI 学习 1.1.1 -- Unit Interval,比特周期】
学习·ui·si 比特周期