Use `use` keyword to import when you use double colon a lot
Consider this code snippet
fn main(){
let my_mood = Mood::Joyful;
println!("On a scale of 1 - 10, what's your mood tooday? {}", match_mood(&my_mood))
}
enum Mood{
Happy,
Sad,
Sleepy,
Bland,
Joyful,
Content,
}
fn match_mood(mood: &Mood) -> u8{
use Mood::*;
match mood{
Happy => 8,
Sad => 2,
Joyful => 10,
Content => 7,
Bland => 6,
Sleepy => 5,
}
}
Instead of using the Mood enum
like this:
...
Mood::Happy => 8,
Mood::Sad => 8,
....
We import all the options in mood by using the use
keyword.
//other code
use Mood::*;
The *
refers to 'every option' in Mood enum
, then we can use the option without needing to type Mood::option
all the time.
The use
keyword isn't just for enum
One way you know to use it is if you figure you are using alot of ::
.