Rust by Example - страница 32

стр.

>

>use deeply::nested::function as other_function;

>fn function() {

>println!("called `function()`");

>}

>mod deeply {

>pub mod nested {

>pub fn function() {

>println!("called `deeply::nested::function()`");

>}

>}

>}

>fn main() {

>// Easier access to `deeply::nested::function`

>other_function();

>println!("Entering block");

>{

>// This is equivalent to `use deeply::nested::function as function`.

>// This `function()` will shadow the outer one.

>use crate::deeply::nested::function;

>// `use` bindings have a local scope. In this case, the

>// shadowing of `function()` is only in this block.

>function();

>println!("Leaving block");

>}

>function();

>}

>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The super and self keywords can be used in the path to remove ambiguity when accessing items and to prevent unnecessary hardcoding of paths.

>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() {

>// Let's access all the functions named `function` from this scope!

>print!("called `my::indirect_call()`, that\n> ");

>// The `self` keyword refers to the current module scope - in this case `my`.

>// Calling `self::function()` and calling `function()` directly both give

>// the same result, because they refer to the same function.

>self::function();

>function();

>// We can also use `self` to access another module inside `my`:

>self::cool::function();

>// The `super` keyword refers to the parent scope (outside the `my` module).

>super::function();

>// This will bind to the `cool::function` in the *crate* scope.

>// In this case the crate scope is the outermost scope.

>{

>use crate::cool::function as root_function;

>root_function();

>}

>}

>}

>fn main() {

>my::indirect_call();

>}

>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Modules can be mapped to a file/directory hierarchy. Let's break down the visibility example in files:

>$ tree .

>.

>|-- my

>| |-- inaccessible.rs

>| |-- mod.rs

>| `-- nested.rs

>`-- split.rs

In split.rs:

>// This declaration will look for a file named `my.rs` or `my/mod.rs` and will

>// insert its contents inside a module named `my` under this scope

>mod my;


>fn function() {

>println!("called `function()`");

>}


>fn main() {

>my::function();


>function();


>my::indirect_access();


>my::nested::function();

>}


In my/mod.rs:

>// Similarly `mod inaccessible` and `mod nested` will locate the `nested.rs`

>// and `inaccessible.rs` files and insert them here under their respective

>// modules

>mod inaccessible;

>pub mod nested;


>pub fn function() {

>println!("called `my::function()`");

>}


>fn private_function() {

>println!("called `my::private_function()`");

>}


>pub fn indirect_access() {

>print!("called `my::indirect_access()`, that\n> ");


>private_function();

>}

In my/nested.rs:

>pub fn function() {

>println!("called `my::nested::function()`");

>}


>#[allow(dead_code)]

>fn private_function() {

>println!("called `my::nested::private_function()`");

>}

In my/inaccessible.rs:

>#[allow(dead_code)]

>pub fn public_function() {

>println!("called `my::inaccessible::public_function()`");