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

首頁綜合 正文
目錄

柚子快報激活碼778899分享:Rust- 結構體

柚子快報激活碼778899分享:Rust- 結構體

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() {

/*

元組結構體

struct Pair(String, i32);

C語言風格結構體

struct 結構體名稱 {

...

}

單元結構體,不帶字段,在泛型中很有用。

type Unit

struct 結構體名稱 {

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

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

...

}

let 實例名稱 = 結構體名稱 {

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

字段2:數(shù)據(jù)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, 參數(shù)列表) 返回值 {

// 方法體

}

}

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

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

&self 表示當前結構體的實例。也是結構體普通方法固定的第一個參數(shù),其他參數(shù)可選。

結構體靜態(tài)方法

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

// 方法體

}

調用方式 結構體名稱::方法名(參數(shù)列表)

*/

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,

};

}

}

柚子快報激活碼778899分享:Rust- 結構體

http://yzkb.51969.com/

相關文章

評論可見,查看隱藏內容

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

轉載請注明,如有侵權,聯(lián)系刪除。

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

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄