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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="see_also_11"><strong><a l:href="#see_also_11">See also:</a></strong></p>

Box and methods

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

Rust has two different types of constants which can be declared in any scope including global. Both require explicit type annotation:

   • const: An unchangeable value (the common case).

   • static: A possibly mutable variable with 'static lifetime. The static lifetime is inferred and does not have to be specified. Accessing or modifying a mutable static variable is unsafe.

// Globals are declared outside all other scopes.

static LANGUAGE: &str = "Rust";

const THRESHOLD: i32 = 10;

fn is_big(n: i32) -> bool {

// Access constant in some function

n > THRESHOLD

}

fn main() {

let n = 16;

// Access constant in the main thread

println!("This is {}", LANGUAGE);

println!("The threshold is {}", THRESHOLD);

println!("{} is {}", n, if is_big(n) { "big" } else { "small" });

// Error! Cannot modify a `const`.

THRESHOLD = 5;

// FIXME ^ Comment out this line

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="see_also_12"><strong><a l:href="#see_also_12">See also:</a></strong></p>

Theconst/staticRFC, 'staticlifetime

<p id="variable_bindings"><strong><a l:href="#variable_bindings">Variable Bindings</a></strong></p>

Rust provides type safety via static typing. Variable bindings can be type annotated when declared. However, in most cases, the compiler will be able to infer the type of the variable from the context, heavily reducing the annotation burden.

Values (like literals) can be bound to variables, using the let binding.

fn main() {

let an_integer = 1u32;

let a_boolean = true;

let unit = ();

// copy `an_integer` into `copied_integer`

let copied_integer = an_integer;

println!("An integer: {:?}", copied_integer);

println!("A boolean: {:?}", a_boolean);

println!("Meet the unit value: {:?}", unit);

// The compiler warns about unused variable bindings; these warnings can

// be silenced by prefixing the variable name with an underscore

let _unused_variable = 3u32;

let noisy_unused_variable = 2u32;

// FIXME ^ Prefix with an underscore to suppress the warning

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

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

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