Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ภาคผนวก A — คีย์เวิร์ด

รายการต่อไปนี้บรรจุคีย์เวิร์ดที่ถูก reserve สำหรับการใช้ปัจจุบันหรือ อนาคตโดยภาษา Rust ดังนั้น พวกมันไม่สามารถถูกใช้เป็น identifier (ยกเว้น เป็น raw identifier ดังที่เราพูดถึงในส่วน “Raw Identifier”) Identifier คือชื่อ ของฟังก์ชัน, ตัวแปร, parameter, field struct, โมดูล, crate, constant, macro, ค่า static, attribute, type, trait หรือ lifetime

คีย์เวิร์ดที่ใช้ปัจจุบัน

ต่อไปนี้คือรายการของคีย์เวิร์ดที่ใช้ปัจจุบัน กับ functionality ของพวก มันอธิบาย

  • as — ทำ casting primitive, disambiguate trait เฉพาะที่บรรจุ item หรือเปลี่ยนชื่อ item ใน statement use
  • async — Return Future แทนการ block เธรดปัจจุบัน
  • await — ระงับการ execute จนกระทั่งผลของ Future พร้อม
  • break — Exit loop ทันที
  • const — นิยาม item constant หรือ raw pointer constant
  • continue — ดำเนินไป iteration loop ถัดไป
  • crate — ใน path โมดูล อ้างถึง root crate
  • dyn — Dynamic dispatch ไปยัง trait object
  • else — Fallback สำหรับ construct control flow if และ if let
  • enum — นิยาม enumeration
  • extern — Link ฟังก์ชันหรือตัวแปรภายนอก
  • false — Literal Boolean false
  • fn — นิยามฟังก์ชันหรือ type function pointer
  • for — Loop ผ่าน item จาก iterator, implement trait หรือระบุ lifetime แบบ higher ranked
  • if — แตกแขนงตามผลของ expression conditional
  • impl — Implement functionality inherent หรือ trait
  • in — ส่วนของ syntax loop for
  • let — Bind ตัวแปร
  • loop — Loop ไม่มีเงื่อนไข
  • match — Match ค่ากับ pattern
  • mod — นิยามโมดูล
  • move — ทำให้ closure รับ ownership ของ capture ทั้งหมดของมัน
  • mut — แสดง mutability ใน reference, raw pointer หรือ binding pattern
  • pub — แสดง visibility public ใน field struct, block impl หรือโมดูล
  • ref — Bind โดย reference
  • return — Return จากฟังก์ชัน
  • Self — type alias สำหรับ type ที่เรากำลังนิยามหรือ implement
  • self — subject เมธอดหรือโมดูลปัจจุบัน
  • static — ตัวแปร global หรือ lifetime ที่ใช้ตลอดการ execute โปรแกรมทั้งหมด
  • struct — นิยามโครงสร้าง
  • super — โมดูล parent ของโมดูลปัจจุบัน
  • trait — นิยาม trait
  • true — Literal Boolean true
  • type — นิยาม type alias หรือ associated type
  • union — นิยาม union — เป็นคีย์เวิร์ด เพียงเมื่อใช้ในการประกาศ union
  • unsafe — แสดงโค้ด ฟังก์ชัน trait หรือ implementation ที่ unsafe
  • use — นำ symbol เข้า scope
  • where — แสดง clause ที่ constrain type
  • while — Loop conditional ตามผลของ expression

คีย์เวิร์ดที่ Reserve สำหรับการใช้อนาคต

คีย์เวิร์ดต่อไปนี้ยังไม่มี functionality แต่ถูก reserve โดย Rust สำหรับ การใช้อนาคตที่อาจมี:

  • abstract
  • become
  • box
  • do
  • final
  • gen
  • macro
  • override
  • priv
  • try
  • typeof
  • unsized
  • virtual
  • yield

Raw Identifier

Raw identifier คือ syntax ที่ให้คุณใช้คีย์เวิร์ดที่พวกมันปกติไม่ถูก อนุญาต คุณใช้ raw identifier โดยนำคีย์เวิร์ดด้วย r#

ตัวอย่างเช่น match คือคีย์เวิร์ด ถ้าคุณลอง compile ฟังก์ชันต่อไปนี้ ที่ใช้ match เป็นชื่อ:

Filename: src/main.rs

fn match(needle: &str, haystack: &str) -> bool {
    haystack.contains(needle)
}

คุณจะได้ error นี้:

error: expected identifier, found keyword `match`
 --> src/main.rs:4:4
  |
4 | fn match(needle: &str, haystack: &str) -> bool {
  |    ^^^^^ expected identifier, found keyword

error แสดงว่าคุณไม่สามารถใช้คีย์เวิร์ด match เป็น identifier ฟังก์ชัน เพื่อใช้ match เป็นชื่อฟังก์ชัน คุณต้องใช้ syntax raw identifier แบบนี้:

Filename: src/main.rs

fn r#match(needle: &str, haystack: &str) -> bool {
    haystack.contains(needle)
}

fn main() {
    assert!(r#match("foo", "foobar"));
}

โค้ดนี้จะ compile โดยไม่มี error สังเกต prefix r# บนชื่อฟังก์ชันใน นิยามของมันรวมทั้งที่ฟังก์ชันถูกเรียกใน main

Raw identifier อนุญาตให้คุณใช้คำใดที่คุณเลือกเป็น identifier แม้คำนั้น บังเอิญเป็นคีย์เวิร์ดที่ reserve นี่ให้เราอิสระมากขึ้นในการเลือกชื่อ identifier รวมทั้งให้เรา integrate กับโปรแกรมที่เขียนในภาษาที่คำเหล่า นี้ไม่ใช่คีย์เวิร์ด นอกจากนี้ raw identifier อนุญาตให้คุณใช้ library ที่เขียนใน Rust edition ต่างจากที่ crate ของคุณใช้ ตัวอย่างเช่น try ไม่ใช่คีย์เวิร์ดใน edition 2015 แต่อยู่ใน edition 2018, 2021 และ 2024 ถ้าคุณ depend บน library ที่เขียนโดยใช้ edition 2015 และมีฟังก์ชัน try คุณจะต้องใช้ syntax raw identifier, r#try ในกรณีนี้ เพื่อ เรียกฟังก์ชันนั้นจากโค้ดของคุณบน edition ภายหลัง ดู ภาคผนวก E สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ edition