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

стр.

>

>//my_mod::nested::public_function_in_my_mod();

>// TODO ^ Try uncommenting this line

>// Private items of a module cannot be directly accessed, even if

>// nested in a public module:

>// Error! `private_function` is private

>//my_mod::private_function();

>// TODO ^ Try uncommenting this line

>// Error! `private_function` is private

>//my_mod::nested::private_function();

>// TODO ^ Try uncommenting this line

>// Error! `private_nested` is a private module

>//my_mod::private_nested::function();

>// TODO ^ Try uncommenting this line

>// Error! `private_nested` is a private module

>//my_mod::private_nested::restricted_function();

>// TODO ^ Try uncommenting this line

>}

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

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Structs have an extra level of visibility with their fields. The visibility defaults to private, and can be overridden with the pub modifier. This visibility only matters when a struct is accessed from outside the module where it is defined, and has the goal of hiding information (encapsulation).

>mod my {

>// A public struct with a public field of generic type `T`

>pub struct OpenBox {

>pub contents: T,

>}

>// A public struct with a private field of generic type `T`

>#[allow(dead_code)]

>pub struct ClosedBox {

>contents: T,

>}

>impl ClosedBox {

>// A public constructor method

>pub fn new(contents: T) -> ClosedBox {

>ClosedBox {

>contents: contents,

>}

>}

>}

>}

>fn main() {

>// Public structs with public fields can be constructed as usual

>let open_box = my::OpenBox { contents: "public information" };

>// and their fields can be normally accessed.

>println!("The open box contains: {}", open_box.contents);

>// Public structs with private fields cannot be constructed using field names.

>// Error! `ClosedBox` has private fields

>//let closed_box = my::ClosedBox { contents: "classified information" };

>// TODO ^ Try uncommenting this line

>// However, structs with private fields can be created using

>// public constructors

>let _closed_box = my::ClosedBox::new("classified information");

>// and the private fields of a public struct cannot be accessed.

>// Error! The `contents` field is private

>//println!("The closed box contains: {}", _closed_box.contents);

>// TODO ^ Try uncommenting this line

>}

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

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The use declaration can be used to bind a full path to a new name, for easier access. It is often used like this:

>use crate::deeply::nested::{

>my_first_function,

>my_second_function,

>AndATraitType

>};

>fn main() {

>my_first_function();

>}

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

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

You can use the as keyword to bind imports to a different name:

>// Bind the `deeply::nested::function` path to `other_function`.