Search code examples
arraysvectorrust

Pushing array values to a vector in Rust


The Rust Programming Language has a task to print The Twelve Days of Christmas taking advantage of its repetitiveness.

My idea was to gather all the presents in an array and push them to the vector which would be printed from iteration to iteration.

It seems that it's either impossible, not easy, or I don't get something.

The code:

fn main() {
    let presents = [
        "A song and a Christmas tree",
        "Two candy canes",
        "Three boughs of holly",
    ];

    let mut current_presents = Vec::new();

    for day in presents {
        current_presents.push(presents[day]);
        println!(
            "On the {} day of Christmas my good friends brought to me",
            day + 1
        );
        println!("{current_presents} /n");
    }
}
error[E0277]: the type `[&str]` cannot be indexed by `&str`
  --> src/main.rs:11:31
   |
11 |         current_presents.push(presents[day]);
   |                               ^^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
   |
   = help: the trait `SliceIndex<[&str]>` is not implemented for `&str`
   = note: required because of the requirements on the impl of `Index<&str>` for `[&str]`

error[E0369]: cannot add `{integer}` to `&str`
  --> src/main.rs:14:17
   |
14 |             day + 1
   |             --- ^ - {integer}
   |             |
   |             &str

error[E0277]: `Vec<_>` doesn't implement `std::fmt::Display`
  --> src/main.rs:16:20
   |
16 |         println!("{current_presents} /n");
   |                    ^^^^^^^^^^^^^^^^ `Vec<_>` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Vec<_>`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

Please help me debug or push me in the right direction that wouldn't be typing down 12 separate strings and printing them 1 by 1.


Solution

  • This works:

    fn main() {
        let presents = [
            "A song and a Christmas tree",
            "Two candy canes",
            "Three boughs of holly",
        ];
    
        let mut current_presents = Vec::new();
    
        for (day, present) in presents.iter().enumerate() {
            current_presents.push(present);
            println!(
                "On the {} day of Christmas my good friends brought to me",
                day + 1
            );
            println!("{current_presents:?}\n");
        }
    }