欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

柚子快報(bào)激活碼778899分享:Rust- 結(jié)構(gòu)體

柚子快報(bào)激活碼778899分享:Rust- 結(jié)構(gòu)體

http://yzkb.51969.com/

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:

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:

println!("User's email: {}", user1.email);

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

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.

// Tuple struct

struct Color(u8, u8, u8);

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

// Unit struct

struct Unit;

A comprehensive case is as follows:

fn main() {

/*

元組結(jié)構(gòu)體

struct Pair(String, i32);

C語(yǔ)言風(fēng)格結(jié)構(gòu)體

struct 結(jié)構(gòu)體名稱(chēng) {

...

}

單元結(jié)構(gòu)體,不帶字段,在泛型中很有用。

type Unit

struct 結(jié)構(gòu)體名稱(chēng) {

字段1:數(shù)據(jù)類(lèi)型

字段2:數(shù)據(jù)類(lèi)型

...

}

let 實(shí)例名稱(chēng) = 結(jié)構(gòu)體名稱(chēng) {

字段1:數(shù)據(jù)1

字段2:數(shù)據(jù)2

...

}

*/

let s = Study {

name: String::from("Rust"),

target: String::from("可以熟練書(shū)寫(xiě)Rust程序"),

spend: 36,

};

println!("{:?}", s); // 輸出:Study { name: "Rust", target: "可以熟練書(shū)寫(xiě)Rust程序", spend: 36 }

println!("{}", s.name); // 輸出:Rust

let s3 = get_instance(

String::from("Rust Programming Language"),

String::from("熟練書(shū)寫(xiě)Rust程序"),

36,

);

println!("{:?}", s3); // 輸出:Study { name: "Rust Programming Language", target: "熟練書(shū)寫(xiě)Rust程序", spend: 36 }

// show(s3);

/*

impl 結(jié)構(gòu)體 {

fn 方法名(&self, 參數(shù)列表) 返回值 {

// 方法體

}

}

函數(shù) 可以直接調(diào)用,同一個(gè)程序不能出現(xiàn)2個(gè)相同的函數(shù)簽名的函數(shù),因?yàn)楹瘮?shù)不歸屬任何owner.

方法 歸屬某一個(gè)owner,不同的owner可以有相同的方法簽名。

&self 表示當(dāng)前結(jié)構(gòu)體的實(shí)例。也是結(jié)構(gòu)體普通方法固定的第一個(gè)參數(shù),其他參數(shù)可選。

結(jié)構(gòu)體靜態(tài)方法

fn 方法名(參數(shù):數(shù)據(jù)類(lèi)型,...) -> 返回類(lèi)型 {

// 方法體

}

調(diào)用方式 結(jié)構(gòu)體名稱(chēng)::方法名(參數(shù)列表)

*/

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

let s4 = Study::get_instance_another(

String::from("Rust Programming Language"),

String::from("熟練書(shū)寫(xiě)Rust程序"),

36,

);

println!("s4 {:?}", s4); // 輸出:s4 Study { name: "Rust Programming Language", target: "熟練書(shū)寫(xiě)Rust程序", spend: 36 }

/*

單元結(jié)構(gòu)體

是一個(gè)類(lèi)型,有且只有一個(gè)值()

*/

// 元組結(jié)構(gòu)體

let pair = (String::from("Rust"), 1);

println!("pair 包含{:?} and {:?}", pair.0, pair.1); // 輸出:pair 包含"Rust" and 1

// 解構(gòu)元組結(jié)構(gòu)體

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,

};

}

}

柚子快報(bào)激活碼778899分享:Rust- 結(jié)構(gòu)體

http://yzkb.51969.com/

相關(guān)文章

評(píng)論可見(jiàn),查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。

轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19002502.html

發(fā)布評(píng)論

您暫未設(shè)置收款碼

請(qǐng)?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪(fǎng)問(wèn)

文章目錄