How to collect string of input into array?

Hi and welcome to this forum!

How is everyone overlooking the pinned post on syntax highlighting...

Anyways, this is what your code actually looks like when properly formatted (including fixes for the typos):

use std::io;
fn main() {
    let mut numberofnames = String::new();
    io::stdin()
        .read_line(&mut numberofnames)
        .expect("Need number of names");
    let numberofnames: usize = numberofnames.trim().parse().expect("Need a number");
    let mut names = vec![""; numberofnames];
    for x in 0..numberofnames {
        let mut name = String::new();
        io::stdin().read_line(&mut name).expect("Need name");
        names[x] = &name.trim();
    }
}

Error message:

   Compiling playground v0.0.1 (/playground)
error[E0597]: `name` does not live long enough
  --> src/main.rs:12:21
   |
12 |         names[x] = &name.trim();
   |         -----       ^^^^ borrowed value does not live long enough
   |         |
   |         borrow later used here
13 |     }
   |     - `name` dropped here while still borrowed
2 Likes