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

前言

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

知识点

mod 关键字

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

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

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

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 生命周期

生命周期的语法是

&i32        // 引用
&'a i32     // 带有显式生命周期的引用
&'a mut i32 // 带有显式生命周期的可变引用

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

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

题目

Mod1

题目
// 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修饰符即可。

// 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

题目
// 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...语句的目的是为了缩短调用路径,简化代码。

// 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

题目
// 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!"),
    }
}

题解

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

// 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!"),
    }
}
10-10 17:51