ภาคผนวก 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 ใน statementuseasync— ReturnFutureแทนการ block เธรดปัจจุบันawait— ระงับการ execute จนกระทั่งผลของFutureพร้อมbreak— Exit loop ทันทีconst— นิยาม item constant หรือ raw pointer constantcontinue— ดำเนินไป iteration loop ถัดไปcrate— ใน path โมดูล อ้างถึง root cratedyn— Dynamic dispatch ไปยัง trait objectelse— Fallback สำหรับ construct control flowifและif letenum— นิยาม enumerationextern— Link ฟังก์ชันหรือตัวแปรภายนอกfalse— Literal Boolean falsefn— นิยามฟังก์ชันหรือ type function pointerfor— Loop ผ่าน item จาก iterator, implement trait หรือระบุ lifetime แบบ higher rankedif— แตกแขนงตามผลของ expression conditionalimpl— Implement functionality inherent หรือ traitin— ส่วนของ syntax loopforlet— Bind ตัวแปรloop— Loop ไม่มีเงื่อนไขmatch— Match ค่ากับ patternmod— นิยามโมดูลmove— ทำให้ closure รับ ownership ของ capture ทั้งหมดของมันmut— แสดง mutability ใน reference, raw pointer หรือ binding patternpub— แสดง visibility public ใน field struct, blockimplหรือโมดูลref— Bind โดย referencereturn— Return จากฟังก์ชันSelf— type alias สำหรับ type ที่เรากำลังนิยามหรือ implementself— subject เมธอดหรือโมดูลปัจจุบันstatic— ตัวแปร global หรือ lifetime ที่ใช้ตลอดการ execute โปรแกรมทั้งหมดstruct— นิยามโครงสร้างsuper— โมดูล parent ของโมดูลปัจจุบันtrait— นิยาม traittrue— Literal Boolean truetype— นิยาม type alias หรือ associated typeunion— นิยาม union — เป็นคีย์เวิร์ด เพียงเมื่อใช้ในการประกาศ unionunsafe— แสดงโค้ด ฟังก์ชัน trait หรือ implementation ที่ unsafeuse— นำ symbol เข้า scopewhere— แสดง clause ที่ constrain typewhile— Loop conditional ตามผลของ expression
คีย์เวิร์ดที่ Reserve สำหรับการใช้อนาคต
คีย์เวิร์ดต่อไปนี้ยังไม่มี functionality แต่ถูก reserve โดย Rust สำหรับ การใช้อนาคตที่อาจมี:
abstractbecomeboxdofinalgenmacrooverrideprivtrytypeofunsizedvirtualyield
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