rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

generics1.rs (562B)


      1 // `Vec<T>` is generic over the type `T`. In most cases, the compiler is able to
      2 // infer `T`, for example after pushing a value with a concrete type to the vector.
      3 // But in this exercise, the compiler needs some help through a type annotation.
      4 
      5 fn main() {
      6     // `u8` and `i8` can both be converted to `i16`.
      7     let mut numbers: Vec<i16> = Vec::new();
      8     //             ^^^^^^^^^^ added
      9 
     10     // Don't change the lines below.
     11     let n1: u8 = 42;
     12     numbers.push(n1.into());
     13     let n2: i8 = -1;
     14     numbers.push(n2.into());
     15 
     16     println!("{numbers:?}");
     17 }