Rust结构

有三种类型的结构(“structs”),可以使用被struct 关键字创建 :

  • 元组结构,这是命名为元组的基础。
  • 典型的 C 结构
  • 单元结构,这是无字段,对泛型有用。
  • // A unit struct
    struct Nil;
    
    // A tuple struct
    struct Pair(i32, 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);
    }
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3