Filling a vector with strings

Below is some code that fills a vector with String entries. I'm using it to test a function I'm trying to convert over to using generics. Anyway, even though it works, it just seems clumsy to me. There has to be a better, more flexible way to fill a vector with String entries? Thoughts?

fn main() {
    let mut santa: Vec<String> = Vec::new();
    
    santa.push("Trees".to_string());
    santa.push("Posts".to_string());
    santa.push("Staples".to_string());
    santa.push("Cows".to_string());
    santa.push("Bulls".to_string());
    
    println!("\n In main() the vector vector is:\n    {:?}", santa);
}

How about this?

fn main() {
    let santa = vec![
        "Trees".to_string(),
        "Posts".to_string(),
        "Staples".to_string(),
        "Cows".to_string(),
        "Bulls".to_string(),
    ];

    println!("\n In main() the vector vector is:\n    {:?}", santa);
}
1 Like

This is just

let stanta: Vec<_> = [
    "Trees",
    "Posts",
    "Staples",
].into_iter().map(String::from).collect();
2 Likes

This is also clumsy

fn main() {
    let santa = "Trees Posts Staples Cows Bulls"
        .split(" ")
        .map(String::from)
        .collect::<Vec<_>>();

    println!("\n In main() the vector vector is:\n    {:?}", santa);
}