How to put a value in a struct vec field

Hello all,

I hate to keep bothering everyone with my trivial questions. Two steps forward and one step back. I am trying to place a value, a string, into a vector field of a structure. I get this compiler error.

```error[E0599]: no method named push_str found for struct std::vec::Vec<Team> in the current scope
--> src/main.rs:73:19
|
73 | baseball.team.push_str( String::from("Rockies") );
| ^^^^^^^^ method not found in std::vec::Vec<Team>

error: aborting due to previous error

For more information about this error, try rustc --explain E0599.
error: could not compile struct-test.```

my code is:

    structure declarations
*/
#[derive(Debug)]
struct Baseball {
    team:              Vec<Team>,
    }
#[derive(Debug)]
struct Team {
    name:               String,
    mascot:             String,
    coach:              Coach,
    }
#[derive(Debug)]
struct Coach {
    name:               String,
    experience_years:   u32,
    years_hd_coach:     u32,
    win_lose:           (u32,u32),
    asst_coaches:       Vec<AsstCoach>,
    }
#[derive(Debug)]
struct AsstCoach {
    name:               String,
    coach_of:           String,
    experience_years:   u32,
    players:            Vec<Players>,
    }
#[derive(Debug)]
struct Players {
    name:               String,
    position:           String,
    experience_years:   u32,
    errors_game:        u32,
    batting_avg:        u32,
    }
/*
    start of main
*/
fn main() {

    let mut players           = Players {
        name:                   String::new(),
        position:               String::new(),
        experience_years:       0,
        errors_game:            0,
        batting_avg:            0,
    };
    let mut asstcoach         = AsstCoach {
        name:                   String::new(),
        coach_of:               String::new(),
        experience_years:       0,
        players:                vec![players],
    };
    let mut coach             = Coach {
        name:                   String::new(),
        experience_years:       0,
        years_hd_coach:         0,
        win_lose:               (0,0),
        asst_coaches:           vec![asstcoach],
    };
    let mut team              = Team {
        name:                   String::new(),
        mascot:                 String::new(),
        coach,
    };
    let mut baseball          = Baseball {
        team:                   vec![team],
    };

    println!("content of structures: {:#?}", baseball);

    baseball.team.push_str( String::from("Rockies") );



}

thank you all for your help

rustuser99

The push_str method is defined on String, but baseball.team has type Vec<Team>. You probably want the push method instead.

Additionally, it is a vector of Team, so you can't push a String to it. You need to change your rockies string into a Team object.

Thanks alice for the reply. I am really, really a rust newbie, what do you mean by changing rockies into a Team object? Can you give an example and an explaination. OR, point me to the proper place in "the book".

thanks
rustuser99

For example, it could look like this:

baseball.team.push(Team {
    name: "Rockies".to_string(),
    mascot: "Rockies' mascot".to_string(),
    coach: Coach {
        name: "Rockies' coach".to_string(),
        experience_years: 10,
        years_hd_coach: 12,
        win_lose: (5, 8),
        asst_coaches: vec![],
    },
});

You can see that team requires the item to be a Team object here:

struct Baseball {
    team: Vec<Team>,
//            ^^^^ here
}

Note that you are using very nonstandard formatting in your code. Take a look at this example to see how most Rust code is formatted. You may be interested in the tool called rustfmt, which can automatically format code.

in addition to what @alice said, there seems to be a larger problem with the general structure of your data model. in your code,

  • the players belong to an assistant coach instead of the team
  • assistant coaches belong to a coach instead of the team
  • assistant coaches have a kind of reverse reference to "their" coach, but it's a string

Here's my attempt at improving the structure of your data:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7b25f08ccf417332aadc44abe9adb3aa
(also, i'm sorry i don't know much about baseball, so please excuse the shitty example data)

Edit: apparently, while i typed this, alice was quicker to provide a working code example.

Thanks Alice, mmmmib,

I know my formatting is non-standard, I will work on that as I go along. I appreciate the help but you are both over my head. Let me ponder what you sent to me and do more reading and then I'll see if I understand more. Is there a book that explains these as you described or is this something you just have to learn by doing?

Thanks again.

rustuser99

Hello all,

mmmmib, I was trying to think of some hierarchical data structure so I could learn how to move,add,remove data in and out of the structure. So I wanted some strings, vectors and integers in each layer/level. This is just what came to mind first. Alice and mmmmib, your examples appear to be a one time thru the program design/setup/layout? My thought was to, for example, if I read thru some data that was not organized, to put that data into the teams and coaches and players as the input data was read. This is just a learning tool nothing else. In other words take unorganized data and organized it. Now having said all this BS, I do thank you for your input and look forward to any thoughts you may have. Maybe my thinking is wrong to begin with, so think on that too.

thanks for your help

rustuser99

Hi @rustuser99,
(if you tag people with an at-sign live this, they get a notification that they were mentioned)

If you want to read data and mutate the structure, i added a basic example on how to do that:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2e6d8f1efa01b6abafbe706899eed19b

Basically, just baseball needs to be mut and then you can mutate all its members (and members of members). If you want to do lookups (like baseball.teams.get("Niners")), maybe have a look at Map-like data structures instead of Vec, like HashMap or BTreeMap: https://doc.rust-lang.org/stable/std/collections/index.html#use-a-hashmap-when

Hello @mmmmib,

Thanks for the help. I do have a question. Both examples show updating all fields at the same time. How would you do an update for only one or two fields. Example, as the baseball season progresses the only thing in the Coach/coach structure to need updating is the win-lose field, how would you do that?

Thanks for the help.

rustuser99

previous example, but with values being updated:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0455a25f8bf63b02b7194bdd52563a48

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.