How to initialize a structure field?

Hello all,
I appreciate all you folks helping others learn to use Rust. I am a newbie with Rust, I do a little bit then do a little bit more. Now, I trying to use Rust structures. I posted a question a few days ago about how to define hierarchical data. Now I am trying to initialize that data. The compiler gives this error:

error[E0308]: mismatched types
  --> src/main.rs:48:29
   |
48 |         coach:              String::new(),
   |                             ^^^^^^^^^^^^^ expected struct `Coach`, found struct `std::string::String`

error: aborting due to previous error

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

My code is:

/*
    structure declarations
*/
#[derive(Debug)]
struct Baseball {
    teams: Vec<Team>,
    }
#[derive(Debug)]
struct Team {
    team_name: String,
    mascot_name: 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,
    experience_years: u32,
    coach_of: String,
    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 baseball      = Baseball {
        teams:              Vec::new(),
    };
    let mut team          = Team {
        team_name:          String::new(),
        mascot_name:        String::new(),
        coach:              String::new(),
    };
    let mut coach         = Coach {
        name:               String::new(),
        experience_years:   0,
        years_hd_coach:     0,
        win_lose:           (0,0),
        asst_coaches:       Vec::new(),
    };
    let mut asstcoach     = AsstCoach {
        name:               String::new(),
        experience_years:   0,
        coach_of:           String::new(),
        players:            Vec::new(),
    };
    let mut players       = Players {
        name:               String::new(),
        position:           String::new(),
        experience_years:   0,
        errors_game:        0,
        batting_avg:        0,
    };
    println!("content of structures: {:?}, {:?}, {:?}, {:?}, {:?}", baseball, team, coach, asstcoach, players);
}

Thanks for any help

rustuser99

The type of the coach field in Team is Coach, so you need to provide a Coach, not a String. Perhaps you meant something like this, playground

Thanks, RustyYato

I thought I tried just "coach," and it didn't work , but let me try again. I noticed you placed the initialization statements in reverse order than I had them, is this important?

thanks again

rustuser99

You need to define your variables before you use them.

Thanks RustyYato,

I know to define before use, but, this looked different so I didn't. I changed my code to match your suggestion and I removed my print stmt and used your, compiles cleanly. If I leave my print stmt in I get more compiler errors regarding the borrower. Next I will execute to see my results and if good I'll start adding in data to the fields. By the way where can I find info on "Vec!". I don't recall seeing that before?

Thanks again for your help

rustuser99

vec! is a macro for Vec literals.

Hi RustyYato,

Thanks for the reply. I will do a little reading on this.

rustuser99

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.