【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!"),
    }
}
相关推荐
<e^πi+1=0>2 小时前
Rust Web自动化Demo
rust
allanGold3 小时前
VSCode rust文件中的api点击无法跳转问题
vscode·rust
碳苯5 小时前
【rCore OS 开源操作系统】Rust HashMap应用 知识点及练习题
开发语言·rust·操作系统
碳苯9 小时前
【rCore OS 开源操作系统】Rust 练习题题解: Structs
rust·开源·操作系统·os·内存安全
CherishTaoTao11 小时前
Rust环境安装配置
开发语言·后端·rust
程序员吾真本11 小时前
使用默认不可变的Rust变量会踩什么坑
开发语言·后端·rust
Amd79413 小时前
Nuxt.js 应用中的 app:beforeMount 钩子详解
生命周期·初始化·数据加载·钩子·用户认证·nuxtjs·应用优化
结衣结衣.16 小时前
Linux——环境变量
linux·运维·服务器·c语言·笔记·学习·操作系统
凌云行者1 天前
使用rust写一个Web服务器——多线程版本
服务器·前端·rust