话不多说,如题,直接上代码:
#[derive(Debug)]
struct MyRgb<U>{
r:U,
g:U,
b:U,
}
#[derive(Debug)]
enum Mycolor<U>{
W(U),
G(U),
Rgb(U),
}
impl<U> Mycolor<U>{
fn getvalue(&self) -> &U{
match &self{
Mycolor::W(x) => x,
Mycolor::G(x) => x,
Mycolor::Rgb(x) => x,
}
}
}
fn main(){
let s = MyRgb{r:120,g:150,b:133};
let m = Mycolor::Rgb(s);
println!("{:?}",m.getvalue().b);
println!("* * * * * * * * * * * * * * * * * *");
let m = Mycolor::W(99.9);
println!("{:?}",m.getvalue());
println!("* * * * * * * * * * * * * * * * * *");
let m = Mycolor::G("Oh.my god.".to_string());
println!("{:?}",m.getvalue());
}
/*
输出如下内容:
133
* * * * * * * * * * * * * * * * * *
99.9
* * * * * * * * * * * * * * * * * *
"Oh.my god."
*/