rustlings

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

generics1.rs (642B)


      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     // TODO: Fix the compiler error by annotating the type of the vector
      7     // `Vec<T>`. Choose `T` as some integer type that can be created from
      8     // `u8` and `i8`.
      9     let mut numbers: Vec<i16> = Vec::new();
     10 
     11     // Don't change the lines below.
     12     let n1: u8 = 42;
     13     numbers.push(n1.into());
     14     let n2: i8 = -1;
     15     numbers.push(n2.into());
     16 
     17     println!("{numbers:?}");
     18 }