Is this push struct to vector well implemented?

struct Person{
  name: String,
  age: i8,

fn main() {
  let mut persons = Vec::new();

    name: String::from("Test"), 
    age: 22

    name: String::from("Doom"), 
    age: 45
  println!("Vector of persons: {:#?}", persons);



Vector of persons: [
    Person {
        name: "Test",
        age: 22,
    Person {
        name: "Doom",
        age: 45,

What's the question?


The result is what is expected. Beyond that I've no idea what you're asking.


Yes, it's fine.

If you have only these two, you can use vec![Person{…}, Person{…}].

If you have more, and know how many, you can optimize it a bit by replacing Vec::new() with Vec::with_capacity(number_of_items_you_will_push).

Good use of String in Person. Conversion from borrowed &str is the right choice here, because structs typically own their data.

1 Like

I'm new so i was trying to do something that i already did in Go, so i was worry that it was not well implemented.

Thank you a lot for your answer. :stuck_out_tongue_winking_eye:

i was just worry of a bad implementation.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.