super和self
super 和 self
在路径上使用 super (父级)和 self(自身)关键字,可以在访问项时消除歧义和防止不必要的路径的硬编码。
fn function() {println!("called `function()`");}mod cool {pub fn function() {println!("called `cool::function()`");}}mod my {fn function() {println!("called `my::function()`");}mod cool {pub fn function() {println!("called `my::cool::function()`");}}pub fn indirect_call() {// 让我们从这个作用域中访问所有名为 `function` 的函数!print!("called `my::indirect_call()`, that\n> ");// `self` 关键字表示当前的模块作用域——在这个例子是 `my`。// 调用 `self::function()` 和直接访问 `function()` 两者都得到相同的结果,// 因为他们表示相同的函数。self::function();function();// 我们也可以使用 `self` 来访问 `my` 内部的另一个模块:self::cool::function();// `super` 关键字表示父级作用域(在 `my` 模块外面)。super::function();// 这将在 *crate* 作用域内绑定 `cool::function` 。// 在这个例子中,crate 作用域是最外面的作用域。{use cool::function as root_function;root_function();}}}fn main() {my::indirect_call();}
