I am trying to match the data type of a common field structure and respond accordingly. My general idea was this (code does not compile):
struct foo<T> { zaz: String, bar: T, } fn main() { let x = foo::<String> { zaz: "Hello".to_string(), bar: "world".to_string(), }; let y = foo::<u32> { zaz: "Hello".to_string(), bar: 5, }; match x.bar { String => println!("It a string!"), u32 => println!("It a u32!"), _ => println!("Something else"), }; println!("end of program!"); }
Error message from rustc
:
error[E0001]: unreachable pattern --> src/main.rs:18:9 | 18 | u32 => println!("It a u32!"), | ^^^ this is an unreachable pattern | note: this pattern matches any value --> src/main.rs:17:9 | 17 | String => println!("It a string!"), | ^^^^^^ error[E0001]: unreachable pattern --> src/main.rs:19:9 | 19 | _ => println!("Something else"), | ^ this is an unreachable pattern | note: this pattern matches any value --> src/main.rs:17:9 | 17 | String => println!("It a string!"), | ^^^^^^
I wanted x
match the first and y
to the second. Actually, Iβm not sure what I want to do, but to achieve the desired effect?
source share