Created
April 2, 2021 11:07
-
-
Save gemcave/64e4afa927fe12b7f1a8c4888f2e0921 to your computer and use it in GitHub Desktop.
frontendmasters rust lesson3 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CitySize { | |
Town, // approximate residents: 1_000 | |
City, // approximate residents: 10_000 | |
Metropolis, // approximate residents: 1_000_000 | |
} | |
struct City { | |
description: String, | |
residents: u64, | |
is_coastal: bool, | |
} | |
impl City { | |
fn new(city_size: CitySize, is_coastal: bool) -> City { | |
let (description, residents) = match city_size { | |
CitySize::Town => { | |
let residents = 1_000; | |
( | |
format!("a *town* of approximately {} residents", residents), | |
residents, | |
) | |
} | |
// 👉 TODO Handle the other CitySize variants individually, | |
// in a similar way to how *town* is handled here | |
CitySize::Metropolis => { | |
let residents = 1_000_000; | |
( | |
format!("a *metropolis* of approximately {} residents", residents), | |
residents, | |
) | |
}, | |
CitySize::City => { | |
let residents = 10_000; | |
( | |
format!("a *city* of approximately {} residents", residents), | |
residents, | |
) | |
}, | |
_ => { | |
let residents = 1_000; | |
( | |
format!( | |
"an *unknown-size city* of approximately {} residents", | |
residents | |
), | |
residents, | |
) | |
} | |
}; | |
City { | |
description, | |
residents, | |
is_coastal, | |
} | |
} | |
} | |
fn main() { | |
// 👉 TODO Use City::new() to create a Metropolis-sized city here | |
let rustville = City::new(CitySize::Metropolis, true); | |
println!("This city is {}", rustville.description); | |
if rustville.residents > 100_000 { | |
println!("Wow!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment