结构体
使用 struct
关键字可以创建三种类型的结构体("structs"):
- 元组结构体:本质上是具名元组。
- 经典的 C 语言风格结构体
- 单元结构体:没有字段,在泛型中很有用。
练习
- 添加一个
rect_area
函数来计算Rectangle
的面积(尝试使用嵌套解构)。 - 添加一个
square
函数,它接受一个Point
和一个f32
作为参数,返回一个Rectangle
,其左上角在该点上,宽度和高度都等于f32
参数。
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help