experiments

All kinds of coding experiments
Log | Files | Refs | Submodules

custom_types2.rs (2556B)


      1 #[derive(Debug)]
      2 struct Person {
      3     name: String,
      4     age: u8,
      5 }
      6 
      7 // A unit struct
      8 struct Unit;
      9 
     10 // A tuple struct
     11 struct Pair(i32, f32);
     12 
     13 // A struct with two fields
     14 #[derive(Debug)]
     15 struct Point {
     16     x: f32,
     17     y: f32,
     18 }
     19 
     20 // Structs can be reused as fields of another struct
     21 #[derive(Debug)]
     22 #[allow(dead_code)]
     23 struct Rectangle {
     24     // A rectangle can be specified by where the top left and bottom right
     25     // corners are in space.
     26     top_left: Point,
     27     bottom_right: Point,
     28 }
     29 
     30 fn rect_area(r: &Rectangle) -> f32 {
     31     // Nested struct destructuring demonstrated here:
     32     let Rectangle {
     33         top_left: Point {
     34             x: ref x1,
     35             y: ref y1,
     36         },
     37         bottom_right: Point {
     38             x: ref x2,
     39             y: ref y2,
     40         },
     41     } = *r;
     42     (x1 - x2) * (y1 - y2)
     43 }
     44 
     45 fn square(p: Point, v: f32) -> Rectangle {
     46     Rectangle {
     47         top_left: Point { x: p.x, y: p.y + v },
     48         bottom_right: Point { x: p.x + v, y: p.y },
     49     }
     50 }
     51 
     52 fn main() {
     53     // Create struct with field init shorthand
     54     let name = String::from("Peter");
     55     let age = 27;
     56     let peter = Person { name, age };
     57 
     58     // Print debug struct
     59     println!("{:?}", peter);
     60 
     61     // Instantiate a `Point`
     62     let point: Point = Point { x: 10.3, y: 0.4 };
     63 
     64     // Access the fields of the point
     65     println!("point coordinates: ({}, {})", point.x, point.y);
     66 
     67     // Make a new point by using struct update syntax to use the fields of our
     68     // other one
     69     let bottom_right = Point { x: 5.2, ..point };
     70 
     71     // `bottom_right.y` will be the same as `point.y` because we used that field
     72     // from `point`
     73     println!("second point: ({}, {})", bottom_right.x, bottom_right.y);
     74 
     75     // Destructure the point using a `let` binding
     76     let Point {
     77         x: left_edge,
     78         y: top_edge,
     79     } = point;
     80 
     81     let _rectangle = Rectangle {
     82         // struct instantiation is an expression too
     83         top_left: Point {
     84             x: left_edge,
     85             y: top_edge,
     86         },
     87         bottom_right: bottom_right,
     88     };
     89 
     90     // Instantiate a unit struct
     91     let _unit = Unit;
     92 
     93     // Instantiate a tuple struct
     94     let pair = Pair(1, 0.1);
     95 
     96     // Access the fields of a tuple struct
     97     println!("pair contains {:?} and {:?}", pair.0, pair.1);
     98 
     99     // Destructure a tuple struct
    100     let Pair(integer, decimal) = pair;
    101 
    102     println!("pair contains {:?} and {:?}", integer, decimal);
    103     println!("rect: {:?}", _rectangle);
    104     println!("area: {}", rect_area(&_rectangle));
    105     println!("rect: {:?}", square(Point { x: 0.0, y: 0.0 }, 3.5))
    106 }