fn main() { let x = 5u; let y = { let x_squared = x * x; let x_cube = x_squared * x; // `y` x_cube + x_squared + x }; let z = { // , // , `z` `()` 2 * x; }; println!("x is {}", x); println!("y is {}", y); println!("z is {}", z); }
if-else
is C-like. Unlike C, the logical condition should not be enclosed in parentheses, and curly braces are required after each condition.if-else
is also an expression; and, due to the type safety of Rust, all branches must return a value of the same type. fn main() { let n = 5i; if n < 0 { print!("{} is negative", n); } else if n > 0 { print!("{} is positive", n); } else { print!("{} is zero", n); } let big_n = if n < 10 && n > -10 { println!(", and is a small number, increase ten-fold"); // `int` 10 * n } else { println!(", and is a big number, reduce by two"); // `int` n / 2 // ^ }; println!("{} -> {}", n, big_n); }
loop
keyword in Rust creates an infinite loop (loop).break
statement allows you to exit the loop at any time, and continue
skips the rest of the iteration and starts execution again. fn main() { let mut count = 0u; println!("Let's count until infinity!"); // loop { count += 1; if count == 3 { println!("three"); // continue; } println!("{}", count); if count == 5 { println!("OK, that's enough"); // break; } } }
'label
, and break/continue
statements should be written with this label. fn main() { 'outer: loop { println!("Entered the outer loop"); 'inner: loop { println!("Entered the inner loop"); // //break; // break 'outer; } println!("This point will never be reached"); } println!("Exited the outer loop"); }
while
is true.while
. fn main() { // let mut n = 1u; // `n` 101 while n < 101 { if n % 15 == 0 { println!("fizzbuzz"); } else if n % 3 == 0 { println!("fizz"); } else if n % 5 == 0 { println!("buzz"); } else { println!("{}", n); } // n += 1; } }
for in
construct can be used to iterate over Iterator
and a lazy generator (more later). The range
function is one of the most common iterators. range(a, b)
will produce values from a to b-1 by changing the step by one.for
instead of while
. fn main() { // `n` : 1, 2, ..., 100 for n in range(1u, 101) { if n % 15 == 0 { println!("fizzbuzz"); } else if n % 3 == 0 { println!("fizz"); } else if n % 5 == 0 { println!("buzz"); } else { println!("{}", n); } } }
return
operator to return the value early from the function, even from an internal loop or conditional operator. // , fn is_divisible_by(lhs: uint, rhs: uint) -> bool { // , if rhs == 0 { return false; } // , `return` lhs % rhs == 0 } // , , `()` fn fizzbuzz(n: uint) -> () { if is_divisible_by(n, 15) { println!("fizzbuzz"); } else if is_divisible_by(n, 3) { println!("fizz"); } else if is_divisible_by(n, 5) { println!("buzz"); } else { println!("{}", n); } } // `()`, fn fizzbuzz_to(n: uint) { for n in range(1, n + 1) { fizzbuzz(n); } } fn main() { fizzbuzz_to(100); }
dead_code
lint to warn about unused functions. You can add the #[allow(dead_code)]
attribute to disable the notification. fn used_function() {} // `#[allow(dead_code)]` , `dead_code` #[allow(dead_code)] fn unused_function() {} fn noisy_unused_function() {} // ^ , fn main() { used_function(); }
Source: https://habr.com/ru/post/233621/
All Articles