【Rust】结构体-area
2021/11/30 23:07:12
本文主要是介绍【Rust】结构体-area,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境
- Rust 1.55.0
- VSCode 1.59.1
概念
参考:https://doc.rust-lang.org/stable/rust-by-example/custom_types/structs.html
Add a function rect_area
which calculates the area of a Rectangle
(try using nested destructuring).
示例
struct Point { x: f32, y: f32, } struct Rectangle { top_left: Point, bottom_right: Point, } impl Rectangle { fn rect_area(&self) -> f32 { let Rectangle { top_left: Point { x: x1, y: y1 }, bottom_right: Point { x: x2, y: y2 }, } = self; ((x1 - x2) * (y1 - y2)).abs() } } fn main() { let rec = Rectangle { top_left: Point { x: 0.0, y: 0.0 }, bottom_right: Point { x: 2.0, y: 3.0 }, }; println!("rec area: {}", rec.rect_area()); }
总结
根据要求,实现求面积的方法,并使用嵌套的解构。
附录
这篇关于【Rust】结构体-area的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南
- 2024-09-26Springboot微服务资料入门教程