Читаем Rust by Example полностью

// u32, but it is still fine, because it never returns and therefore

// does not violate the type requirements of the match expression.

false => continue,

};

acc += addition;

}

acc

}

println!("Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9));

}

It is also the return type of functions that loop forever (e.g. loop {}) like network servers or functions that terminate the process (e.g. exit()).

<p id="modules"><strong><a l:href="#modules">Modules</a></strong></p>

Rust provides a powerful module system that can be used to hierarchically split code in logical units (modules), and manage visibility (public/private) between them.

A module is a collection of items: functions, structs, traits, impl blocks, and even other modules.

<p id="visibility"><strong><a l:href="#visibility">Visibility</a></strong></p>

By default, the items in a module have private visibility, but this can be overridden with the pub modifier. Only the public items of a module can be accessed from outside the module scope.

// A module named `my_mod`

mod my_mod {

// Items in modules default to private visibility.

fn private_function() {

println!("called `my_mod::private_function()`");

}

// Use the `pub` modifier to override default visibility.

pub fn function() {

println!("called `my_mod::function()`");

}

// Items can access other items in the same module,

// even when private.

pub fn indirect_access() {

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

private_function();

}

// Modules can also be nested

pub mod nested {

pub fn function() {

println!("called `my_mod::nested::function()`");

}

#[allow(dead_code)]

fn private_function() {

println!("called `my_mod::nested::private_function()`");

}

// Functions declared using `pub(in path)` syntax are only visible

// within the given path. `path` must be a parent or ancestor module

pub(in crate::my_mod) fn public_function_in_my_mod() {

print!("called `my_mod::nested::public_function_in_my_mod()`, that\n> ");

public_function_in_nested();

}

// Functions declared using `pub(self)` syntax are only visible within

// the current module, which is the same as leaving them private

pub(self) fn public_function_in_nested() {

println!("called `my_mod::nested::public_function_in_nested()`");

}

// Functions declared using `pub(super)` syntax are only visible within

// the parent module

pub(super) fn public_function_in_super_mod() {

println!("called `my_mod::nested::public_function_in_super_mod()`");

}

}

pub fn call_public_function_in_my_mod() {

print!("called `my_mod::call_public_function_in_my_mod()`, that\n> ");

nested::public_function_in_my_mod();

print!("> ");

nested::public_function_in_super_mod();

}

// pub(crate) makes functions visible only within the current crate

pub(crate) fn public_function_in_crate() {

println!("called `my_mod::public_function_in_crate()`");

}

// Nested modules follow the same rules for visibility

mod private_nested {

#[allow(dead_code)]

pub fn function() {

Перейти на страницу:

Похожие книги