“This is the 20th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Dear, hello, everyone. I am “Front-end Xiaoxin”. 😇 has been engaged in front-end development and Android development for a long time


There are two ways to customize types in Rust: by defining constructs and enumerations, you can define complex types that fit real-world scenarios.

Definition and use of structures

  1. The keyword that defines a structure is struct;
  2. There are three structural styles: C-style, Tuple Style and unit-style.

1. C-style common structure:

Definition: contains two fields representing “coordinate point structure”;

struct Point {
    x: f32,
    y: f32,}Copy the code

Create and print the output Point structure using two {:? } PointX and PointY;

let point = Point { x: 10.23, y: 29.39 };
println!("Point x {:? }, {y:? }", point.x, point.y);

// output: Point x 10.23, y29.39
Copy the code

2. Define the field of a struct as another struct:

Definition: a “line structure” containing two coordinate points, starting point and ending point;

struct Line {
    start: Point,
    end: Point,
}
Copy the code

Create and print the output Line structure, using destruct to simplify usage and named to specify output.

let Line { start, end } = Line {
    start: Point { x: 0.00, y: 0.00 },
    end: Point { x: 100.00, y: 0.00}};println!(
    "Line start{startX:? }{startY:? {}, end endX:? }end{endY:? }",
    startX = start.x,
    startY = start.y,
    endX = end.x,
    endY = end.y,
);
// output: Line start0.00.0, end100.0end0.0
Copy the code

3. Tuple Style

Definition: a “ring structure” containing the origin and radius, in which the primary feature of the primitive is that different types of data can be packed together;

struct Ring(Point, Line);
Copy the code

Create and print the Ring structure.

let Ring(_point, _line) = Ring(point, Line { start, end });
println!(
    "Ring {pointX:? } {pointY:? }, start {startX:? } {startY:? {}, end endX:? } {endY:? }",
    pointX = _point.x,
    pointY = _point.y,
    startX = _line.start.x,
    startY = _line.start.y,
    endX = _line.end.x,
    endY = _line.end.y,
);
// output: Ring 10.23 29.39, start0.0 0.0, end100.0 0.0
Copy the code

4. unit-style

Define and create: features that have no fields can be used when you need to implement a feature but don’t want to store any data of the type itself;

struct Empty;
let x = Empty;
println!("{:p}", &x);
/ / the output: 0 xca675df588
Copy the code

Definition and use of enumerations

Define enumerations: The types of enumerations in Rust consist of one or more common, meta-ancestor, or unit constructs;

enum WebEvent {
    PageLoad,                 // Unit structure
    KeyPress(char),           // Progenitor structure
    Click { x: i64, y: i64 }, // Common c-style structure
}
Copy the code

Using aliases: Simplifies subsequent use by using aliases defined by the type keyword;

type WE = WebEvent;
Copy the code

Match enumeration: use match to match the corresponding processing, and use to simplify the code

fn inspect(event: WE) {
    // Simplify with use
    use WebEvent::{Click, KeyPress, PageLoad};

    // Perform corresponding processing after matching
    match event {
        PageLoad => println!("PageLoad"),
        KeyPress(c) => println!("KeyPress: {}", c),
        Click { x, y } => {
            println!(X: {}, y: {}", x, y); }}}// Execute a defined function. When a defined field is not in use, you can type '#! [allow(dead_code)] 'to ignore compilation error warnings
inspect(WebEvent::PageLoad);
inspect(WebEvent::KeyPress('a'));
inspect(WebEvent::Click { x: 100, y: 200 });
Copy the code

Conclusion:

This article summarizes the ways in which the Rust programming language customizes types, mainly the pointer syntax


Welcome to follow my public account “Front-end Xiaoxin students”, the first time to push original technical articles.