【rCore OS 开源操作系统】Rust mod模块和static生命周期 知识点及练习题

【rCore OS 开源操作系统】Rust 模块 mod 知识点及练习题

前言

虽然是模块 的章节,但是也涉及到了一些结构体访问控制生命周期 相关的知识点。

这里暂不讨论 crate,个人觉得这个东西属于工程化的内容,而不是 rust 的模块语法。

知识点

mod 关键字

mod 关键词是用来声明一个模块的,模块可以被use关键词引入,同时use 可以配合as关键词重写导入内容的名称,比如下面这样:

rust 复制代码
use self::fruits::PEAR as fruit; // 使用 self 关键字进行导入
use super::xxxx;  // 使用 super 关键字进行导入
use crate::xxxx;  // 使用关键字 crate 进行导入
use my_mod::xxxx; // 使用模块名称进行导入

这里涉及到了几种关键字,不过我们先不急着了解具体含义,先来看看代码。

虽然根据字面意思,也能猜测到 self 和 super 大概是什么了。

rust 复制代码
mod delicious_snacks {
    pub use self::fruits::PEAR as fruit; // self 关键词指的就是当前模块
    pub use self::veggies::CUCUMBER as veggie;

	// 模块可以嵌套
    mod fruits {
        pub const PEAR: &'static str = "Pear"; // 这里的 'static 是一个生命周期
        pub const APPLE: &'static str = "Apple";
    }

    mod veggies {
        pub const CUCUMBER: &'static str = "Cucumber";
        pub const CARROT: &'static str = "Carrot";
    }
}

上面的代码已经解释了self关键词的含义。

至于super,其实相当于当前模块的父级模块------也就是嵌套关系中,处于外层的模块。

crate 呢,目前可以简单理解为是一个最外层的大模块,所有模块都在 crate 下,是"祖先"模块。

use 关键字

use是用来导入模块的。

这里比较奇特的点是在前面加了一个 pub 修饰,区别在于:

  • pub: 成为当前模块的一个 pub 字段,可被外部使用。
  • pub: 仅被当前模块使用,不能被外部使用。

这里还提到了生命周期。

static 生命周期

生命周期的语法是

参考: 《Rust 高级程序设计》生命周期

rust 复制代码
&i32        // 引用
&'a i32     // 带有显式生命周期的引用
&'a mut i32 // 带有显式生命周期的可变引用

其实呢,我们之前写的字符串字面量(切片&str),生命周期默认就是static,这代表它是在整个程序运行期间一直存在的。

当然,本章节的主题是模块,所以不对生命周期做过多阐述,能看懂就行了。

题目

Mod1

题目
rust 复制代码
// modules1.rs
//
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

mod sausage_factory {
    // Don't let anybody outside of this module see this!
    fn get_secret_recipe() -> String {
        String::from("Ginger")
    }

    fn make_sausage() {
        get_secret_recipe();
        println!("sausage!");
    }
}

fn main() {
    sausage_factory::make_sausage();
}
题解

这里是考察可访问性 ,只需要给调用的方法前加一个pub修饰符即可。

rust 复制代码
// modules1.rs
//
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint.

mod sausage_factory {
    // Don't let anybody outside of this module see this!
    fn get_secret_recipe() -> String {
        String::from("Ginger")
    }

    pub fn make_sausage() {
        get_secret_recipe();
        println!("sausage!");
    }
}

fn main() {
    sausage_factory::make_sausage();
}

Mod2

题目
rust 复制代码
// modules2.rs
//
// You can bring module paths into scopes and provide new names for them with
// the 'use' and 'as' keywords. Fix these 'use' statements to make the code
// compile.
//
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

mod delicious_snacks {
    // TODO: Fix these use statements
    use self::fruits::PEAR as ???
    use self::veggies::CUCUMBER as ???

    mod fruits {
        pub const PEAR: &'static str = "Pear";
        pub const APPLE: &'static str = "Apple";
    }

    mod veggies {
        pub const CUCUMBER: &'static str = "Cucumber";
        pub const CARROT: &'static str = "Carrot";
    }
}

fn main() {
    println!(
        "favorite snacks: {} and {}",
        delicious_snacks::fruit,
        delicious_snacks::veggie
    );
}
题解

这里主要是考察as关键字的运用。

这里使用use...as...语句的目的是为了缩短调用路径,简化代码。

rust 复制代码
// modules2.rs
//
// You can bring module paths into scopes and provide new names for them with
// the 'use' and 'as' keywords. Fix these 'use' statements to make the code
// compile.
//
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint.

mod delicious_snacks {
    pub use self::fruits::PEAR as fruit;
    pub use self::veggies::CUCUMBER as veggie;

    mod fruits {
        pub const PEAR: &'static str = "Pear";
        pub const APPLE: &'static str = "Apple";
    }

    mod veggies {
        pub const CUCUMBER: &'static str = "Cucumber";
        pub const CARROT: &'static str = "Carrot";
    }
}

fn main() {
    println!(
        "favorite snacks: {} and {}",
        delicious_snacks::fruit,
        delicious_snacks::veggie
    );
}

Mod3

题目
rust 复制代码
// modules3.rs
//
// You can use the 'use' keyword to bring module paths from modules from
// anywhere and especially from the Rust standard library into your scope. Bring
// SystemTime and UNIX_EPOCH from the std::time module. Bonus style points if
// you can do it with one line!
//
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

// TODO: Complete this use statement
use ???

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
    }
}
题解

注释里面已经告诉我们需要导入哪个包了,我们这里直接导入这个包中的所有内容即可。

rust 复制代码
// modules3.rs
//
// You can use the 'use' keyword to bring module paths from modules from
// anywhere and especially from the Rust standard library into your scope. Bring
// SystemTime and UNIX_EPOCH from the std::time module. Bonus style points if
// you can do it with one line!
//
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.

use std::time::*;

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
    }
}
相关推荐
红尘散仙4 小时前
六、WebGPU 基础入门——Vertex 缓冲区和 Index 缓冲区
前端·rust·gpu
苏近之4 小时前
深入浅出 Rust 异步运行时原理
rust·源码
红尘散仙5 小时前
四、WebGPU 基础入门——Uniform 缓冲区与内存对齐
前端·rust·gpu
望获linux5 小时前
实时操作系统在服务型机器人中的关键作用
linux·机器人·操作系统·开源软件·rtos·具身智能
大卫小东(Sheldon)8 小时前
魔方求解器桌面版(层先法,基于Tauri实现)
rust
景天科技苑11 小时前
【Rust结构体】Rust结构体详解:从基础到高级应用
开发语言·后端·rust·结构体·关联函数·rust结构体·结构体方法
苏近之11 小时前
说明白 Rust 中的泛型: 泛型是一种多态
设计模式·rust
bruce5411013 小时前
【智能指针】一文带你入门Rust 智能指针
rust
LUCIAZZZ1 天前
说一下Redis的发布订阅模型和PipeLine
java·数据库·redis·缓存·操作系统
用户099691880181 天前
Rust JSON 数据处理:take 与 clone 的权衡
rust