Dzwebs.Net

撰写电脑技术杂文十余年

rust之struct中的字段可为任意类型搭配的方法

Admin | 2023-12-19 21:23:52 | 被阅次数 | 785

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

    如题目所示,下面的代码为示例代码,功能:可让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" }


该杂文来自: 最新技术

上一篇:rust匿名函数之Fn,FnMut,FnOnce的使用

下一篇:rust struct多字段可输入任意泛型数据并计算面积

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计