I'm trying to match a struct containing String members. I found this will make me confusing below, how can I operate to avoid it? I kown using if...else...
could solve the problem, how can I use match
to make sense?
#[derive(Debug, PartialEq)]
struct UserData{
name: String,
id: u64,
}
fn some_test(){
let mud2 = UserData{
name: String::from("name two"),
id: 77,
};
let name_string = String::from("name one");
//***attention: the name_string below is regarded as a new variables, so the first arm will match any value of a UserData
match mud2{
//It's something may confuse!!!
UserData{name: name_string, id} => println!("{}", id),
UserData{name, id: 77} => println!("{}", name),
UserData{name, id} => println!("{}-{}", name, id),
}
}