Rust—结构体
结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //定义结构体Student<br>struct Student{ name:&' static str, score:i32, } fn main(){ let score = 59; let username = "zhangsan" ; let mut student = Student{ score, name:username, }; student.score = 60; println!( "name:{},score:{}" ,student.name,student.score); let student2 = Student{ name: "lisi" , ..student //有别于其他编程语言地方 }; println!( "name:{},score:{}" ,student2.name,student2.score); } |
输出结果:
a.特殊结构体之元组结构体
只有字段类型,没有字段名称的特殊结构体
如定义颜色结构体:
1 2 | struct Color(i32,i32,i32); let balck = Color(0,0,0); |
定义坐标结构体:
1 2 | struct Site(f64,f64,f64); let location = Site(1.0,1.0,1.0); |
b.特殊结构体之单元结构体
无任何成员
如struct just_unit_struct;
c.整个结构体输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #[derive(Debug)] //{:?}使用必需语句 struct Student{ name:&' static str, score:i32, } fn main(){ let score = 59; let username = "zhangsan" ; let mut student = Student{ score, name:username, }; student.score = 60; println!( "name:{},score:{}" ,student.name,student.score); let student2 = Student{ name: "lisi" , ..student }; println!( "name:{},score:{}" ,student2.name,student2.score); //输出结构体student println!( "struct student is {:?}" ,&student); } |
输出结果:
d.结构体方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | const pi:f64 = 3.141592f64; #[derive(Debug)] struct Point(f64,f64); #[derive(Debug)] struct Circle{ Radius:f64, center_point:Point, }<br> //结构体方法定义,拥有&self参数 impl Circle{ fn area(&self)->f64{ self.Radius*self.Radius*pi } } fn main(){ let radius1:f64 = 1f64; let point1= Point(2f64,2f64); //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处OK let circle1 = Circle{ Radius:radius1, center_point:point1, }; //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处会报错,point1已经不存在了 println!( "circle center point is ({},{})" ,circle1.center_point.0,circle1.center_point.1); println!( "{:?} area is {}" ,circle1,circle1.area()); } |
输出结果:
e.结构体关联函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | const pi:f64 = 3.141592f64; #[derive(Debug)] struct Point(f64,f64); ///结构体关联函数 impl Point{ fn create(x:f64,y:f64)->Point{ Point(x,y) } } #[derive(Debug)] struct Circle{ Radius:f64, center_point:Point, } ///结构体方法 impl Circle{ fn area(&self)->f64{ self.Radius*self.Radius*pi } } fn main(){ let radius1:f64 = 1f64; let point1= Point(2f64,2f64); let point2 = Point::create(1.0, 2.0); //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处OK let circle1 = Circle{ Radius:radius1, center_point:point1, }; //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处会报错,point1已经不存在了 println!( "circle center point1 is ({},{})" ,circle1.center_point.0,circle1.center_point.1); println!( "point2 is ({},{})" ,point2.0,point2.1); println!( "{:?} area is {}" ,circle1,circle1.area()); } |
运行结果:
------------------------------------
承接
**视觉检测软件开发及调试
**工业软件开发
**上位机软件开发
wechat:luoran2024
qq:565934058
email:taoyuansu@qq.com
海量教育资源及影视资源下载
微信公众号:EFun科技
------------------------------------
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战