Lv777

Lv777

github
twitter
jike
playstation
xiaoyuzhou

結構體

During the translation process, you need to pay special attention to maintaining the integrity of all Markdown syntax and tags, and not changing the function of HTML tags, to ensure that the translated content does not affect the rendering of any syntax or tags. Please follow the following rules to translate:

  1. Identify and translate text content: Only identify and translate plain text content in Markdown, including text in headings, paragraphs, and list items.

  2. Retain tags and attributes: When encountering HTML tags (such as ,

  3. Special syntax processing: For Markdown-specific syntax (such as link, image tag ), only translate the descriptive text part (such as alt text) without changing the link or syntax structure.

  4. Keep the format unchanged: Ensure that all Markdown formatting (such as bold, italics, code blocks) remains unchanged during translation.

  5. Your task is to ensure that the translated content is both accurate and does not break the original Markdown structure and the function of HTML tags. Please check carefully during translation to ensure the correct rendering of syntax and tags.

  6. You are only allowed to return the translated text and nothing else.

IMPORTANT: ONLY RETURN TRANSLATED TEXT AND NOTHING ELSE.

Translate the following text to Chinese (Traditional) language:

第一遍边抄示例边看文档,一天能过两大章,现在写着笔记,两天不过半章. 😔

可变性#

如果想修改某个字段的值,则结构体实例必须是可变的.

结构体更新语法#

结构体更新语法 有点像 JS 里的 Spread 语法,但只能用旧的实例创建同一结构体实例。这啥场景用,直接修改可变实例的字段不行吗,难道不想创建可变实例而去做合并的操作,immutability?

let user2 = User {
	age: 19,
	..user1
}

还有俩特殊的结构体定义

// 元组结构体
struct Color(i32, i32, i32);
// 类单元结构体
struct Something;

理解#

  • 相比元组可以为复合的值提供可读的标识。在 TS 中也有元组的概念,就是有限长度的数组,数组是 key 为数字索引的特殊对象,在一些场景里可以代替对象的使用,但指定的 key 更好理解字段的意义.
  • 让值的集合成为一个抽象的存在,之后就可以针对这个集合实现方法和关联函数.

方法和关联函数#

可以为结构体实现与之关联的 [[函数]], 称为关联函数, 类似 JS 类的静态方法。如果关联函数第一个参数通过 self 获取实例的上下文,则被称为方法.

impl User {
	// 关联函数
	fn new() -> Self {
		Self {
			name: String::from(""),
			age: 1,
		}
	}
	// 实例方法
	fn get_name(&self) -> &str {
		&self.name
	}
	// 实例可变方法
	fn set_name(&mut self, name: String) {
		self.name = name
	}
}

定义方法时第一个参数 &selfself: &Self 的简写。也可以去掉 & 获取实例的所有权.

第一遍邊抄示例邊看文檔,一天能過兩大章,現在寫著筆記,兩天不過半章。 😔

可變性#

如果想修改某個字段的值,則結構體實例必須是可變的。

結構體更新語法#

結構體更新語法 有點像 JS 裡的 Spread 語法,但只能用舊的實例創建同一結構體實例。這啥場景用,直接修改可變實例的字段不行嗎,難道不想創建可變實例而去做合併的操作,immutability

let user2 = User {
	age: 19,
	..user1
}

還有兩個特殊的結構體定義

// 元組結構體
struct Color(i32, i32, i32);
// 類單元結構體
struct Something;

理解#

  • 相比元組可以為複合的值提供可讀的標識。在 TS 中也有元組的概念,就是有限長度的數組,數組是 key 為數字索引的特殊對象,在一些場景裡可以代替對象的使用,但指定的 key 更好理解字段的意義。
  • 讓值的集合成為一個抽象的存在,之後就可以針對這個集合實現方法和關聯函數。

方法和關聯函數#

可以為結構體實現與之關聯的 [[函數]],稱為關聯函數,類似 JS 類的靜態方法。如果關聯函數第一個參數通過 self 獲取實例的上下文,則被稱為方法

impl User {
	// 關聯函數
	fn new() -> Self {
		Self {
			name: String::from(""),
			age: 1,
		}
	}
	// 實例方法
	fn get_name(&self) -> &str {
		&self.name
	}
	// 實例可變方法
	fn set_name(&mut self, name: String) {
		self.name = name
	}
}

定義方法時第一個參數 &selfself: &Self 的簡寫。也可以去掉 & 獲取實例的所有權。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。