Rust中的结构体及方法语法
2024-08-28 07:31:30
这个可以和类作比较,或是go当中的方法比较。
#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } fn square(size: u32) -> Rectangle { Rectangle {width: size, height: size} } } impl Rectangle { fn can_hold(&self, other: &Rectangle) -> bool { self.width > other.width && self.height > other.height } } fn main() { let rect1 = Rectangle { width: 30, height: 50}; let rect2 = Rectangle { width: 10, height: 40}; let rect3 = Rectangle { width: 60, height: 45}; println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); println!("rect1 is {:#?}", rect1); println!( "The area of retangle is {} square pixels", rect1.area() ); println!( "The area of retangle is {} square pixels", area(&rect1) ); let sq = Rectangle::square(5); println!("square is {:#?}", sq); println!("The area of Square is {} square pixels", sq.area()); } fn area(rectangle: &Rectangle) -> u32 { rectangle.width * rectangle.height }
最新文章
- Redis主从复制
- Bugtags 测试平台(支持ios、android)
- Linux 启动项介绍
- Matlab绘图(一二三维)
- MySQL到MsSQL的迁移工具——SSMA
- JAVA导出数据到excel中大数据量的解决方法
- OSI(Open System Interconnection)网络模型
- 快速理解RequireJs
- 初识CLR
- Codeforces Round #380 (Div. 2)D. Sea Battle
- ACdream 1031 Cut
- C++中的const总结
- 一键安装Davinci
- C#操作剪贴板实现复制粘贴
- Spring Security 安全验证
- 隐藏非选中的checkBox
- 在Idea创建Spring Boot + MyBatis的web项目
- 记录python接口自动化测试--根据excel中的期望输出是否存在请求返回的响应来判断用例是否执行成功(第八目)
- idea使用git提交代码到远程,这里是没有冲突的演示
- 使用 IntraWeb (22) - 基本控件之 TIWCalendar