如题目所示,下面的代码为示例代码,功能:可让struct中的各字段的数据类型为任意类型。
#[derive(Debug)]
struct Mypoint<T1,T2>{
x:T1,
y:T2
}
fn main(){
let p = Mypoint{x:10.1,y:2};
println!("{:?}",p);
let p = Mypoint{x:10,y:2.0};
println!("{:?}",p);
let p = Mypoint{x:10,y:22};
println!("{:?}",p);
let p = Mypoint{x:10.1,y:22.1};
println!("{:?}",p);
let p = Mypoint{x:3.14,y:"hello"};
println!("{:?}",p);
let p = Mypoint{x:true,y:"world"};
println!("{:?}",p);
}
运行上述代码可输出如下:
Mypoint { x: 10.1, y: 2 }
Mypoint { x: 10, y: 2.0 }
Mypoint { x: 10, y: 22 }
Mypoint { x: 10.1, y: 22.1 }
Mypoint { x: 3.14, y: "hello" }
Mypoint { x: true, y: "world" }