这个可以和类作比较,或是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
}

  

最新文章

  1. Redis主从复制
  2. Bugtags 测试平台(支持ios、android)
  3. Linux 启动项介绍
  4. Matlab绘图(一二三维)
  5. MySQL到MsSQL的迁移工具——SSMA
  6. JAVA导出数据到excel中大数据量的解决方法
  7. OSI(Open System Interconnection)网络模型
  8. 快速理解RequireJs
  9. 初识CLR
  10. Codeforces Round #380 (Div. 2)D. Sea Battle
  11. ACdream 1031 Cut
  12. C++中的const总结
  13. 一键安装Davinci
  14. C#操作剪贴板实现复制粘贴
  15. Spring Security 安全验证
  16. 隐藏非选中的checkBox
  17. 在Idea创建Spring Boot + MyBatis的web项目
  18. 记录python接口自动化测试--根据excel中的期望输出是否存在请求返回的响应来判断用例是否执行成功(第八目)
  19. idea使用git提交代码到远程,这里是没有冲突的演示
  20. 使用 IntraWeb (22) - 基本控件之 TIWCalendar

热门文章

  1. 高频Python面试题分享
  2. CentOS7 部署 Django 项目
  3. Java并发编程核心知识体系精讲
  4. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
  5. AtCoder Grand Contest 036
  6. 深挖计算机基础:MySQL实战45讲学习笔记
  7. B1013
  8. 《一起学mysql》3
  9. CompletableFuture3
  10. Eclipse 常用快捷键-java