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

ภาคผนวก 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
!!exprComplement bitwise หรือ logicalNot
!=expr != exprเปรียบเทียบไม่เท่าPartialEq
%expr % exprเศษ arithmeticRem
%=var %= exprเศษ arithmetic และ assignmentRemAssign
&&expr, &mut exprBorrow
&&type, &mut type, &'a type, &'a mut typetype pointer ที่ borrow
&expr & exprBitwise ANDBitAnd
&=var &= exprBitwise AND และ assignmentBitAndAssign
&&expr && exprLogical AND แบบ short-circuit
*expr * exprคูณ arithmeticMul
*=var *= exprคูณ arithmetic และ assignmentMulAssign
**exprDereferenceDeref
**const type, *mut typeRaw pointer
+trait + trait, 'a + traitConstraint type แบบรวม
+expr + exprบวก arithmeticAdd
+=var += exprบวก arithmetic และ assignmentAddAssign
,expr, exprตัวคั่น argument และ element
-- exprNegation arithmeticNeg
-expr - exprลบ arithmeticSub
-=var -= exprลบ arithmetic และ assignmentSubAssign
->fn(...) -> type, |…| -> typereturn type ฟังก์ชันและ closure
.expr.identเข้าถึง field
.expr.ident(expr, ...)เรียกเมธอด
.expr.0, expr.1, และอื่นๆIndexing Tuple
...., expr.., ..expr, expr..exprLiteral range แบบ right-exclusivePartialOrd
..=..=expr, expr..=exprLiteral range แบบ right-inclusivePartialOrd
....exprSyntax update struct literal
..variant(x, ..), struct_type { x, .. }Binding pattern “และที่เหลือ”
...expr...expr(Deprecated, ใช้ ..= แทน) ใน pattern — pattern range inclusive
/expr / exprหาร arithmeticDiv
/=var /= exprหาร arithmetic และ assignmentDivAssign
:pat: type, ident: typeConstraint
:ident: exprInitializer field struct
:'a: loop {...}Label loop
;expr;terminator statement และ item
;[...; len]ส่วนของ syntax array ขนาดคงที่
<<expr << exprLeft-shiftShl
<<=var <<= exprLeft-shift และ assignmentShlAssign
<expr < exprเปรียบเทียบน้อยกว่าPartialOrd
<=expr <= exprเปรียบเทียบน้อยกว่าหรือเท่าPartialOrd
=var = expr, ident = typeAssignment/เทียบเท่า
==expr == exprเปรียบเทียบเท่าPartialEq
=>pat => exprส่วนของ syntax match arm
>expr > exprเปรียบเทียบมากกว่าPartialOrd
>=expr >= exprเปรียบเทียบมากกว่าหรือเท่าPartialOrd
>>expr >> exprRight-shiftShr
>>=var >>= exprRight-shift และ assignmentShrAssign
@ident @ patBinding pattern
^expr ^ exprBitwise exclusive ORBitXor
^=var ^= exprBitwise exclusive OR และ assignmentBitXorAssign
|pat | patทางเลือก pattern
|expr | exprBitwise ORBitOr
|=var |= exprBitwise OR และ assignmentBitOrAssign
||expr || exprLogical OR แบบ short-circuit
?expr?การ propagate error

Symbol ที่ไม่ใช่ Operator

ตารางต่อไปนี้บรรจุ symbol ทั้งหมดที่ไม่ทำหน้าที่เป็น operator — นั่น คือ พวกมันไม่ทำตัวเหมือนการเรียกฟังก์ชันหรือเมธอด

ตาราง B-2 แสดง symbol ที่ปรากฏด้วยตัวเองและ valid ในที่หลากหลาย

ตาราง B-2: Syntax แบบเดี่ยว

Symbolคำอธิบาย
'identLifetime 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
|…| exprClosure
!type bottom ว่างเสมอสำหรับฟังก์ชัน diverging
_Binding pattern “ignore” — ใช้เพื่อทำให้ literal integer อ่านง่ายด้วย

ตาราง B-3 แสดง symbol ที่ปรากฏใน context ของ path ผ่าน hierarchy โมดูล ไปยัง item

ตาราง B-3: Syntax ที่เกี่ยวข้องกับ Path

Symbolคำอธิบาย
ident::identPath namespace
::pathPath สัมพัทธ์กับ root crate (นั่นคือ path absolute อย่างชัดเจน)
self::pathPath สัมพัทธ์กับโมดูลปัจจุบัน (นั่นคือ path สัมพัทธ์อย่างชัดเจน)
super::pathPath สัมพัทธ์กับ parent ของโมดูลปัจจุบัน
type::ident, <type as trait>::identAssociated 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<...> typebound 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: Ugeneric parameter T constrain ให้ type ที่ implement U
T: 'ageneric type T ต้องอยู่ยาวกว่า lifetime 'a (หมายถึง type ไม่สามารถ transitively บรรจุ reference ใดกับ lifetime ที่สั้นกว่า 'a)
T: 'staticgeneric type T ไม่บรรจุ reference ที่ borrow อื่นใดนอกจาก 'static
'b: 'ageneric lifetime 'b ต้องอยู่ยาวกว่า lifetime 'a
T: ?Sizedอนุญาตให้ generic type parameter เป็น dynamically sized type
'a + trait, trait + traitConstraint type แบบรวม

ตาราง B-6 แสดง symbol ที่ปรากฏใน context ของการเรียกหรือนิยาม macro และระบุ attribute บน item

ตาราง B-6: Macro และ Attribute

Symbolคำอธิบาย
#[meta]Attribute ภายนอก
#![meta]Attribute ภายใน
$identการแทนใน Macro
$ident:kindMetavariable 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”