Numbers

Integer

  1. 🌟

Tips: If we don’t explicitly assign a type to a variable, then the compiler will infer one for us.

2. // Remove something to make it work
3. fn main() {
4. let x: i32 = 5;
5. let mut y: u32 = 5;

7. y = x;

9. let z = 10; // Type of z ?

11. println!("Success!");
12. }
  1. 🌟
2. // Fill the blank
3. fn main() {
4. let v: u16 = 38_u8 as __;

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

Tips: If we don’t explicitly assign a type to a variable, then the compiler will infer one for us.

2. // Modify `assert_eq!` to make it work
3. fn main() {
4. let x = 5;
5. assert_eq!("u32".to_string(), type_of(&x));

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

10. // Get the type of given variable, return a string representation of the type  , e.g "i8", "u8", "i32", "u32"
11. fn type_of<T>(_: &T) -> String {
12. format!("{}", std::any::type_name::<T>())
13. }
  1. 🌟🌟
2. // Fill the blanks to make it work
3. fn main() {
4. assert_eq!(i8::MAX, __);
5. assert_eq!(u8::MAX, __);

7. println!("Success!");
8. }
  1. 🌟🌟
2. // Fix errors and panics to make it work
3. fn main() {
4. let v1 = 251_u8 + 8;
5. let v2 = i8::checked_add(251, 8).unwrap();
6. println!("{},{}",v1,v2);
7. }
  1. 🌟🌟
2. // Modify `assert!` to make it work
3. fn main() {
4. let v = 1_024 + 0xff + 0o77 + 0b1111_1111;
5. assert!(v == 1579);

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

Floating-Point

  1. 🌟
2. // Fill the blank to make it work
3. fn main() {
4. let x = 1_000.000_1; // ?
5. let y: f32 = 0.12; // f32
6. let z = 0.01_f64; // f64

8. assert_eq!(type_of(&x), "__".to_string());
9. println!("Success!");
10. }

12. fn type_of<T>(_: &T) -> String {
13. format!("{}", std::any::type_name::<T>())
14. }
  1. 🌟🌟 Make it work in two distinct ways
2. fn main() {
3. assert!(0.1+0.2==0.3);

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

Range

  1. 🌟🌟 Two goals: 1. Modify assert! to make it work 2. Make println! output list of numbers between 97 and 122
1. fn main() {
2. let mut sum = 0;
3. for i in -3..2 {
4. sum += i
5. }

7. assert!(sum == -3);

9. for c in 'a'..='z' {
10. println!("{}",c);
11. }
12. }
  1. 🌟🌟
2. // Fill the blanks
3. use std::ops::{Range, RangeInclusive};
4. fn main() {
5. assert_eq!((1..__), Range{ start: 1, end: 5 });
6. assert_eq!((1..__), RangeInclusive::new(1, 5));

8. println!("Success!");
9. }

Computations

  1. 🌟
2. // Fill the blanks and fix the errors
3. fn main() {
4. // Integer addition
5. assert!(1u32 + 2 == __);

7. // Integer subtraction
8. assert!(1i32 - 2 == __);
9. assert!(1u8 - 2 == -1);

11. assert!(3 * 50 == __);

13. assert!(9.6 / 3.2 == 3.0); // error ! make it work

15. assert!(24 % 5 == __);
16. // Short-circuiting boolean logic
17. assert!(true && false == __);
18. assert!(true || false == __);
19. assert!(!true == __);

21. // Bitwise operations
22. println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
23. println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
24. println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
25. println!("1 << 5 is {}", 1u32 << 5);
26. println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
27. }

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