ภาคผนวก B — Operator และ Symbol
ภาคผนวกนี้บรรจุ glossary ของ syntax ของ Rust รวม operator และ symbol อื่นที่ปรากฏด้วยตัวเองหรือใน context ของ path, generic, trait bound, macro, attribute, comment, tuple และ bracket
Operator
ตาราง B-1 บรรจุ operator ใน Rust, ตัวอย่างของวิธีที่ operator จะปรากฏ ใน context, คำอธิบายสั้น และว่า operator นั้น overloadable หรือไม่ ถ้า operator overloadable, trait ที่เกี่ยวข้องที่ใช้เพื่อ overload operator นั้นถูก list
ตาราง B-1: Operator
| Operator | ตัวอย่าง | คำอธิบาย | Overloadable? |
|---|---|---|---|
! | ident!(...), ident!{...}, ident![...] | การ expand Macro | |
! | !expr | Complement bitwise หรือ logical | Not |
!= | expr != expr | เปรียบเทียบไม่เท่า | PartialEq |
% | expr % expr | เศษ arithmetic | Rem |
%= | var %= expr | เศษ arithmetic และ assignment | RemAssign |
& | &expr, &mut expr | Borrow | |
& | &type, &mut type, &'a type, &'a mut type | type pointer ที่ borrow | |
& | expr & expr | Bitwise AND | BitAnd |
&= | var &= expr | Bitwise AND และ assignment | BitAndAssign |
&& | expr && expr | Logical AND แบบ short-circuit | |
* | expr * expr | คูณ arithmetic | Mul |
*= | var *= expr | คูณ arithmetic และ assignment | MulAssign |
* | *expr | Dereference | Deref |
* | *const type, *mut type | Raw pointer | |
+ | trait + trait, 'a + trait | Constraint type แบบรวม | |
+ | expr + expr | บวก arithmetic | Add |
+= | var += expr | บวก arithmetic และ assignment | AddAssign |
, | expr, expr | ตัวคั่น argument และ element | |
- | - expr | Negation arithmetic | Neg |
- | expr - expr | ลบ arithmetic | Sub |
-= | var -= expr | ลบ arithmetic และ assignment | SubAssign |
-> | fn(...) -> type, |…| -> type | return type ฟังก์ชันและ closure | |
. | expr.ident | เข้าถึง field | |
. | expr.ident(expr, ...) | เรียกเมธอด | |
. | expr.0, expr.1, และอื่นๆ | Indexing Tuple | |
.. | .., expr.., ..expr, expr..expr | Literal range แบบ right-exclusive | PartialOrd |
..= | ..=expr, expr..=expr | Literal range แบบ right-inclusive | PartialOrd |
.. | ..expr | Syntax update struct literal | |
.. | variant(x, ..), struct_type { x, .. } | Binding pattern “และที่เหลือ” | |
... | expr...expr | (Deprecated, ใช้ ..= แทน) ใน pattern — pattern range inclusive | |
/ | expr / expr | หาร arithmetic | Div |
/= | var /= expr | หาร arithmetic และ assignment | DivAssign |
: | pat: type, ident: type | Constraint | |
: | ident: expr | Initializer field struct | |
: | 'a: loop {...} | Label loop | |
; | expr; | terminator statement และ item | |
; | [...; len] | ส่วนของ syntax array ขนาดคงที่ | |
<< | expr << expr | Left-shift | Shl |
<<= | var <<= expr | Left-shift และ assignment | ShlAssign |
< | expr < expr | เปรียบเทียบน้อยกว่า | PartialOrd |
<= | expr <= expr | เปรียบเทียบน้อยกว่าหรือเท่า | PartialOrd |
= | var = expr, ident = type | Assignment/เทียบเท่า | |
== | expr == expr | เปรียบเทียบเท่า | PartialEq |
=> | pat => expr | ส่วนของ syntax match arm | |
> | expr > expr | เปรียบเทียบมากกว่า | PartialOrd |
>= | expr >= expr | เปรียบเทียบมากกว่าหรือเท่า | PartialOrd |
>> | expr >> expr | Right-shift | Shr |
>>= | var >>= expr | Right-shift และ assignment | ShrAssign |
@ | ident @ pat | Binding pattern | |
^ | expr ^ expr | Bitwise exclusive OR | BitXor |
^= | var ^= expr | Bitwise exclusive OR และ assignment | BitXorAssign |
| | pat | pat | ทางเลือก pattern | |
| | expr | expr | Bitwise OR | BitOr |
|= | var |= expr | Bitwise OR และ assignment | BitOrAssign |
|| | expr || expr | Logical OR แบบ short-circuit | |
? | expr? | การ propagate error |
Symbol ที่ไม่ใช่ Operator
ตารางต่อไปนี้บรรจุ symbol ทั้งหมดที่ไม่ทำหน้าที่เป็น operator — นั่น คือ พวกมันไม่ทำตัวเหมือนการเรียกฟังก์ชันหรือเมธอด
ตาราง B-2 แสดง symbol ที่ปรากฏด้วยตัวเองและ valid ในที่หลากหลาย
ตาราง B-2: Syntax แบบเดี่ยว
| Symbol | คำอธิบาย |
|---|---|
'ident | Lifetime named หรือ label loop |
Digit ที่ตามทันทีด้วย u8, i32, f64, usize และอื่น ๆ | Literal ตัวเลขของ type เฉพาะ |
"..." | Literal string |
r"...", r#"..."#, r##"..."## และอื่น ๆ | Literal string raw — character escape ไม่ process |
b"..." | Literal string byte — สร้าง array ของ byte แทน string |
br"...", br#"..."#, br##"..."## และอื่น ๆ | Literal string byte raw — รวมของ literal string raw และ byte |
'...' | Literal character |
b'...' | Literal byte ASCII |
|…| expr | Closure |
! | type bottom ว่างเสมอสำหรับฟังก์ชัน diverging |
_ | Binding pattern “ignore” — ใช้เพื่อทำให้ literal integer อ่านง่ายด้วย |
ตาราง B-3 แสดง symbol ที่ปรากฏใน context ของ path ผ่าน hierarchy โมดูล ไปยัง item
ตาราง B-3: Syntax ที่เกี่ยวข้องกับ Path
| Symbol | คำอธิบาย |
|---|---|
ident::ident | Path namespace |
::path | Path สัมพัทธ์กับ root crate (นั่นคือ path absolute อย่างชัดเจน) |
self::path | Path สัมพัทธ์กับโมดูลปัจจุบัน (นั่นคือ path สัมพัทธ์อย่างชัดเจน) |
super::path | Path สัมพัทธ์กับ parent ของโมดูลปัจจุบัน |
type::ident, <type as trait>::ident | Associated constant, function และ type |
<type>::... | Associated item สำหรับ type ที่ไม่สามารถ name โดยตรง (ตัวอย่างเช่น <&T>::..., <[T]>::... และอื่น ๆ) |
trait::method(...) | Disambiguate การเรียกเมธอดโดย name trait ที่นิยามมัน |
type::method(...) | Disambiguate การเรียกเมธอดโดย name type ที่มันถูกนิยาม |
<type as trait>::method(...) | Disambiguate การเรียกเมธอดโดย name trait และ type |
ตาราง B-4 แสดง symbol ที่ปรากฏใน context ของการใช้ generic type parameter
ตาราง B-4: Generic
| Symbol | คำอธิบาย |
|---|---|
path<...> | ระบุ parameter ให้ generic type ใน type (ตัวอย่างเช่น Vec<u8>) |
path::<...>, method::<...> | ระบุ parameter ให้ generic type, ฟังก์ชัน หรือเมธอดใน expression — มักอ้างถึงเป็น turbofish (ตัวอย่างเช่น "42".parse::<i32>()) |
fn ident<...> ... | นิยาม generic function |
struct ident<...> ... | นิยาม generic structure |
enum ident<...> ... | นิยาม generic enumeration |
impl<...> ... | นิยาม generic implementation |
for<...> type | bound lifetime แบบ higher ranked |
type<ident=type> | generic type ที่หนึ่งหรือมากกว่า associated type มี assignment เฉพาะ (ตัวอย่างเช่น Iterator<Item=T>) |
ตาราง B-5 แสดง symbol ที่ปรากฏใน context ของการ constrain generic type parameter ด้วย trait bound
ตาราง B-5: Constraint Trait Bound
| Symbol | คำอธิบาย |
|---|---|
T: U | generic parameter T constrain ให้ type ที่ implement U |
T: 'a | generic type T ต้องอยู่ยาวกว่า lifetime 'a (หมายถึง type ไม่สามารถ transitively บรรจุ reference ใดกับ lifetime ที่สั้นกว่า 'a) |
T: 'static | generic type T ไม่บรรจุ reference ที่ borrow อื่นใดนอกจาก 'static |
'b: 'a | generic lifetime 'b ต้องอยู่ยาวกว่า lifetime 'a |
T: ?Sized | อนุญาตให้ generic type parameter เป็น dynamically sized type |
'a + trait, trait + trait | Constraint type แบบรวม |
ตาราง B-6 แสดง symbol ที่ปรากฏใน context ของการเรียกหรือนิยาม macro และระบุ attribute บน item
ตาราง B-6: Macro และ Attribute
| Symbol | คำอธิบาย |
|---|---|
#[meta] | Attribute ภายนอก |
#![meta] | Attribute ภายใน |
$ident | การแทนใน Macro |
$ident:kind | Metavariable Macro |
$(...)... | การซ้ำ Macro |
ident!(...), ident!{...}, ident![...] | การเรียก Macro |
ตาราง B-7 แสดง symbol ที่สร้าง comment
ตาราง B-7: Comment
| Symbol | คำอธิบาย |
|---|---|
// | Comment บรรทัด |
//! | Inner line doc comment |
/// | Outer line doc comment |
/*...*/ | Comment block |
/*!...*/ | Inner block doc comment |
/**...*/ | Outer block doc comment |
ตาราง B-8 แสดง context ที่วงเล็บถูกใช้
ตาราง B-8: วงเล็บ
| Symbol | คำอธิบาย |
|---|---|
() | Tuple ว่าง (หรือเรียก unit) ทั้ง literal และ type |
(expr) | Expression ในวงเล็บ |
(expr,) | Expression tuple element เดียว |
(type,) | Type tuple element เดียว |
(expr, ...) | Expression tuple |
(type, ...) | Type tuple |
expr(expr, ...) | Expression เรียกฟังก์ชัน — ใช้เพื่อ initialize tuple struct และ variant tuple enum ด้วย |
ตาราง B-9 แสดง context ที่ curly bracket ถูกใช้
ตาราง B-9: Curly Bracket
| Context | คำอธิบาย |
|---|---|
{...} | Expression block |
Type {...} | Literal struct |
ตาราง B-10 แสดง context ที่ square bracket ถูกใช้
ตาราง B-10: Square Bracket
| Context | คำอธิบาย |
|---|---|
[...] | Literal array |
[expr; len] | Literal array ที่บรรจุ len copy ของ expr |
[type; len] | Type array ที่บรรจุ len instance ของ type |
expr[expr] | Indexing collection — overloadable (Index, IndexMut) |
expr[..], expr[a..], expr[..b], expr[a..b] | Indexing collection แสร้งเป็น slice collection โดยใช้ Range, RangeFrom, RangeTo หรือ RangeFull เป็น “index” |