Rust- 结构体

In Rust, a struct (short for "structure") is a custom data type that lets you name and package together multiple related values that make up a meaningful group. If you're coming from an object-oriented language, a struct is like an object's data attributes.

Here's an example of a struct in Rust:

rust 复制代码
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

// You can create an instance of the struct like this:
let user1 = User {
    email: String::from("someone@163.com"),
    username: String::from("someusername"),
    active: true,
    sign_in_count: 1,
};

This code defines a User struct that has four fields: username, email, sign_in_count, and active. The struct is then instantiated with the User { ... } syntax.

You can access the fields in a struct instance with dot notation:

rust 复制代码
println!("User's email: {}", user1.email);

Structs are also capable of having methods, which are defined within an impl block:

rust 复制代码
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

let rect1 = Rectangle { width: 30, height: 50 };

println!(
    "The area of the rectangle is {} square pixels.",
    rect1.area()
);

In the above code, Rectangle is defined with width and height fields, and an area method is defined for it. The area method can be called on instances of Rectangle with dot notation, just like fields.

There are a few different types of structs in Rust, each with different use cases:

  • Classic structs, which are named and have a set of named fields. This is the most commonly used.

  • Tuple structs, which are named and have a set of unnamed fields. These are useful when you want to give a name to a tuple.

  • Unit structs, which are field-less, are useful for cases when you need to implement a trait on some type but don't have any data you need to store in the type itself.

rust 复制代码
// Tuple struct
struct Color(u8, u8, u8);

let white = Color(255, 255, 255);

// Unit struct
struct Unit;

A comprehensive case is as follows:

rust 复制代码
fn main() {
    /*
       元组结构体
       struct Pair(String, i32);

       C语言风格结构体
       struct 结构体名称 {
           ...
       }

       单元结构体,不带字段,在泛型中很有用。
       type Unit


       struct 结构体名称 {
           字段1:数据类型
           字段2:数据类型
           ...
       }

       let 实例名称 = 结构体名称 {
           字段1:数据1
           字段2:数据2
           ...
       }
    */

    let s = Study {
        name: String::from("Rust"),
        target: String::from("可以熟练书写Rust程序"),
        spend: 36,
    };

    println!("{:?}", s);    // 输出:Study { name: "Rust", target: "可以熟练书写Rust程序", spend: 36 }
    println!("{}", s.name); // 输出:Rust

    let s3 = get_instance(
        String::from("Rust Programming Language"),
        String::from("熟练书写Rust程序"),
        36,
    );

    println!("{:?}", s3);   // 输出:Study { name: "Rust Programming Language", target: "熟练书写Rust程序", spend: 36 }
    // show(s3);

    /*
       impl 结构体 {
           fn 方法名(&self, 参数列表) 返回值 {
               // 方法体
           }
       }

       函数 可以直接调用,同一个程序不能出现2个相同的函数签名的函数,因为函数不归属任何owner.
       方法 归属某一个owner,不同的owner可以有相同的方法签名。

       &self 表示当前结构体的实例。也是结构体普通方法固定的第一个参数,其他参数可选。

       结构体静态方法
       fn 方法名(参数:数据类型,...) -> 返回类型 {
            // 方法体
       }
       调用方式  结构体名称::方法名(参数列表)
    */

    println!("spend {}", s3.get_spend());   // 输出:spend 36

    let s4 = Study::get_instance_another(
        String::from("Rust Programming Language"),
        String::from("熟练书写Rust程序"),
        36,
    );

    println!("s4 {:?}", s4);    // 输出:s4 Study { name: "Rust Programming Language", target: "熟练书写Rust程序", spend: 36 }

    /*
       单元结构体
       是一个类型,有且只有一个值()
    */

    // 元组结构体
    let pair = (String::from("Rust"), 1);
    println!("pair 包含{:?} and {:?}", pair.0, pair.1); // 输出:pair 包含"Rust" and 1

    // 解构元组结构体
    let (study, spend) = pair;
    println!("pair 包含{:?} and {:?}", study, spend);   // 输出:pair 包含"Rust" and 1
}

fn show(s: Study) {
    println!(
        "name is {} target is {} spend is {}",
        s.name, s.target, s.spend
    );
}

fn get_instance(name: String, target: String, spend: i32) -> Study {
    return Study {
        name,
        target,
        spend,
    };
}

#[derive(Debug)]
struct Study {
    name: String,
    target: String,
    spend: i32,
}

impl Study {
    fn get_spend(&self) -> i32 {
        return self.spend;
    }

    fn get_instance_another(name: String, target: String, spend: i32) -> Study {
        return Study {
            name,
            target,
            spend,
        };
    }
}
相关推荐
LvManBa9 小时前
Vue学习记录之六(组件实战及BEM框架了解)
vue.js·学习·rust
VinciYan15 小时前
Rust使用Actix-web和SeaORM库开发WebAPI通过Swagger UI查看接口文档
rust·api·web·orm
白总Server18 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
新知图书1 天前
Rust编程的作用域与所有权
开发语言·后端·rust
许野平2 天前
Rust: Warp RESTful API 如何得到客户端IP?
tcp/ip·rust·restful·ip地址
许野平2 天前
Rust:Result 和 Error
开发语言·后端·rust·error·result
Freestyle Coding2 天前
使用rust自制操作系统内核
c语言·汇编·microsoft·rust·操作系统
许野平3 天前
Rust 编译器使用的 C++ 编译器吗?
c++·rust
怪我冷i3 天前
Rust GUI框架Tauri V1 入门
rust
新知图书3 天前
Rust的常量
算法·机器学习·rust