मैं एक संरचना के एक सामान्य क्षेत्र के डेटाटाइप पर मिलान करने की कोशिश कर रहा हूं और तदनुसार प्रतिक्रिया करता हूं। मेरे सामान्य विचार इस तरह था (कोड संकलन नहीं करता है):जंग में डेटा प्रकार से कैसे मिलान करें?
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's a string!"),
u32 => println!("It's a u32!"),
_ => println!("Something else"),
};
println!("end of program!");
}
rustc
से त्रुटि संदेश:
error[E0001]: unreachable pattern
--> src/main.rs:18:9
|
18 | u32 => println!("It's a u32!"),
| ^^^ this is an unreachable pattern
|
note: this pattern matches any value
--> src/main.rs:17:9
|
17 | String => println!("It's 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's a string!"),
| ^^^^^^
क्या मैं पहली बार एक मैच के लिए x
के लिए चाहता था, और y
मैच के लिए दूसरा एक। मुझे वास्तव में यकीन नहीं है कि मैं क्या करना चाहता हूं, लेकिन वांछित प्रभाव क्या प्राप्त होगा?
संक्षिप्त उत्तर "नहीं बल्ले से दूर सही" है ऐसा नहीं करते हैं। आपको यह प्रश्न उपयोगी हो सकता है: [जंग कैसे प्रतिबिंब लागू करता है?] (Http://stackoverflow.com/q/36416773/1233251) –
जंग जंगलों, enums, और लक्षणों के लिए 'CamelCase' का उपयोग करता है। आपको 'फू' का उपयोग करना चाहिए। – Shepmaster