通过例子学习Rust

17 结构体

There are three types of structures ("structs") that can be created using the struct keyword:

  • Tuple structs, which are, basically, named tuples.
  • The classic C structs
  • Unit structs, which are field-less structs.
// A unit struct struct Nil; // A tuple struct struct Pair(int, f64); // A struct with two fields struct Point { x: f64, y: f64, } // Structs can be reused as fields of another struct #[allow(dead_code)] struct Rectangle { p1: Point, p2: Point, } fn main() { // Instantiate a `Point` let point: Point = Point { x: 0.3, y: 0.4 }; // Access the fields of the point println!("point coordinates: ({}, {})", point.x, point.y); // Destructure the point using a `let` binding let Point { x: my_x, y: my_y } = point; let _rectangle = Rectangle { // struct instantiation is an expression too p1: Point { x: my_y, y: my_x }, p2: point, }; // Instantiate a unit struct let _nil = Nil; // Instantiate a tuple struct let pair = Pair(1, 0.1); // Destructure a tuple struct let Pair(integer, decimal) = pair; println!("pair contains {} and {}", integer, decimal); }