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("[email protected]"),
    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,
        };
    }
}
相关推荐
techdashen6 小时前
Rust主流框架性能比拼: Actix vs Axum vs Rocket
开发语言·后端·rust
Source.Liu8 小时前
【Raqote】 1.2 路径填充ShaderClipMaskBlitter结构体(blitter.rs)
rust·cad
pumpkin845149 小时前
理解 Rust 中的 String 分配机制
开发语言·rust
维维酱12 小时前
Rust - Deref 强制转换
rust
XuanXu14 小时前
我使用Tauri deep-link插件踩过的坑
rust
无名之逆16 小时前
[特殊字符] 超轻高性能的 Rust HTTP 服务器 —— Hyperlane [特殊字符][特殊字符]
java·服务器·开发语言·前端·网络·http·rust
不爱学英文的码字机器18 小时前
Rust 的征服:从系统编程到全栈开发的 IT 新宠
开发语言·后端·rust
维维酱18 小时前
Rust 智能指针
rust
无名之逆1 天前
[特殊字符] Hyperlane:Rust 高性能 HTTP 服务器库,开启 Web 服务新纪元!
java·服务器·开发语言·前端·网络·http·rust
无名之逆1 天前
Hyperlane框架:下一代高性能Rust Web框架 [特殊字符]
服务器·开发语言·前端·后端·http·rust