Statements and Expressions

Examples

1. fn main() {
2. let x = 5u32;

4. let y = {
5. let x_squared = x * x;
6. let x_cube = x_squared * x;

8. // This expression will be assigned to `y`
9. x_cube + x_squared + x
10. };

12. let z = {
13. // The semicolon suppresses this expression and `()` is assigned to `z`
14. 2 * x;
15. };

17. println!("x is {:?}", x);
18. println!("y is {:?}", y);
19. println!("z is {:?}", z);
20. }

Exercises

  1. 🌟🌟
1. // Make it work with two ways
2. fn main() {
3. let v = {
4. let mut x = 1;
5. x += 2
6. };

8. assert_eq!(v, 3);

10. println!("Success!");
11. }
  1. 🌟
2. fn main() {
3. let v = (let x = 3);

5. assert!(v == 3);

7. println!("Success!");
8. }
  1. 🌟
2. fn main() {
3. let s = sum(1 , 2);
4. assert_eq!(s, 3);

6. println!("Success!");
7. }

9. fn sum(x: i32, y: i32) -> i32 {
10. x + y
11. }

You can find the solutions here(under the solutions path), but only use it when you need it