11# Traits
22
3+ > ** <sup >Syntax</sup >** \
4+ > _ Trait_ :\
5+ >   ;  ; ` unsafe ` <sup >?</sup > ` trait ` [ IDENTIFIER]   ;
6+ > [ _ Generics_ ] <sup >?</sup >
7+ > [ _ WhereClause_ ] <sup >?</sup > ` { ` \
8+ >   ;  ;  ;  ; _ TraitItem_ <sup >\* </sup >\
9+ >   ;  ; ` } `
10+ >
11+ > _ TraitItem_ :\
12+ >   ;  ; [ _ OuterAttribute_ ] <sup >\* </sup > (_ TraitFunc_ | _ TraitMethod_ | _ TraitConst_ | _ TraitType_ )
13+ >
14+ > _ TraitFunc_ :\
15+ >   ;  ;   ;  ; _ TraitFunctionDecl_ ( ` ; ` | [ _ BlockExpression_ ] )
16+ >
17+ > _ TraitMethod_ :\
18+ >   ;  ;   ;  ; _ TraitMethodDecl_ ( ` ; ` | [ _ BlockExpression_ ] )
19+ >
20+ > _ TraitFunctionDecl_ :\
21+ >   ;  ; [ _ FunctionFront_ ] ` fn ` [ IDENTIFIER]   ; [ _ Generics_ ] <sup >?</sup >\
22+ >   ;  ;   ;  ; ` ( ` _ TraitFunctionParameters_ <sup >?</sup > ` ) ` \
23+ >   ;  ;   ;  ; [ _ FunctionReturnType_ ] <sup >?</sup > [ _ WhereClause_ ] <sup >?</sup >
24+ >
25+ > _ TraitMethodDecl_ :\
26+ >   ;  ; [ _ FunctionFront_ ] ` fn ` [ IDENTIFIER]   ; [ _ Generics_ ] <sup >?</sup >\
27+ >   ;  ;   ;  ; ` ( ` [ _ SelfParam_ ] (` , ` _ TraitFunctionParam_ )<sup >\* </sup > ` , ` <sup >?</sup > ` ) ` \
28+ >   ;  ;   ;  ; [ _ FunctionReturnType_ ] <sup >?</sup > [ _ WhereClause_ ] <sup >?</sup >
29+ >
30+ > _ TraitFunctionParameters_ :\
31+ >   ;  ; _ TraitFunctionParam_ (` , ` _ TraitFunctionParam_ )<sup >\* </sup > ` , ` <sup >?</sup >
32+ >
33+ > _ TraitFunctionParam_ <sup >[ †] ( #parameter-patterns ) </sup > :\
34+ >   ;  ; ( [ _ Pattern_ ] ` : ` )<sup >?</sup > [ _ Type_ ]
35+ >
36+ > _ TraitConst_ :\
37+ >   ;  ; ` const ` [ IDENTIFIER] ( ( ` : ` [ _ Type_ ] ) ( ` = ` [ _ Expression_ ] )<sup >?</sup > )<sup >?</sup > ` ; `
38+ >
39+ > _ TraitType_ :\
40+ >   ;  ; ` type ` [ IDENTIFIER] ( ` : ` [ _ TypeParamBounds_ ] )<sup >?</sup > ` ; `
41+
342A _ trait_ describes an abstract interface that types can implement. This
443interface consists of [ associated items] , which come in three varieties:
544
@@ -115,6 +154,63 @@ let circle = Box::new(circle) as Box<dyn Circle>;
115154let nonsense = circle . radius () * circle . area ();
116155```
117156
157+ ## Unsafe traits
158+
159+ Traits items that begin with the ` unsafe ` keyword indicate that * implementing* the
160+ trait may be [ unsafe] . It is safe to use a correctly implemented unsafe trait.
161+ The [ trait implementation] must also begin with the ` unsafe ` keyword.
162+
163+ [ ` Sync ` ] and [ ` Send ` ] are examples of unsafe traits.
164+
165+ ## Parameter patterns
166+
167+ Function or method declarations without a body only allow [ IDENTIFIER] or
168+ ` _ ` [ wild card] [ WildcardPattern ] patterns. ` mut ` [ IDENTIFIER] is currently
169+ allowed, but it is deprecated and will become a hard error in the future.
170+ <!-- https://github.com/rust-lang/rust/issues/35203 -->
171+
172+ In the 2015 edition, the pattern for a trait function or method parameter is
173+ optional:
174+
175+ ``` rust
176+ trait T {
177+ fn f (i32 ); // Parameter identifiers are not required.
178+ }
179+ ```
180+
181+ The kinds of patterns for parameters is limited to one of the following:
182+
183+ * [ IDENTIFIER]
184+ * ` mut ` [ IDENTIFIER]
185+ * [ ` _ ` ] [ WildcardPattern ]
186+ * ` & ` [ IDENTIFIER]
187+ * ` && ` [ IDENTIFIER]
188+
189+ Beginning in the 2018 edition, function or method parameter patterns are no
190+ longer optional. Also, all irrefutable patterns are allowed as long as there
191+ is a body. Without a body, the limitations listed above are still in effect.
192+
193+ ``` rust,edition2018
194+ trait T {
195+ fn f1((a, b): (i32, i32)) {}
196+ fn f2(_: (i32, i32)); // Cannot use tuple pattern without a body.
197+ }
198+ ```
199+
200+ [ IDENTIFIER ] : identifiers.html
201+ [ WildcardPattern ] : patterns.html#wildcard-pattern
202+ [ _BlockExpression_ ] : expressions/block-expr.html
203+ [ _Expression_ ] : expressions.html
204+ [ _FunctionFront_ ] : items/functions.html
205+ [ _FunctionParam_ ] : items/functions.html
206+ [ _FunctionReturnType_ ] : items/functions.html
207+ [ _Generics_ ] : items/generics.html
208+ [ _OuterAttribute_ ] : attributes.html
209+ [ _Pattern_ ] : patterns.html
210+ [ _SelfParam_ ] : items/associated-items.html#methods
211+ [ _TypeParamBounds_ ] : trait-bounds.html
212+ [ _Type_ ] : types.html
213+ [ _WhereClause_ ] : items/generics.html#where-clauses
118214[ bounds ] : trait-bounds.html
119215[ trait object ] : types.html#trait-objects
120216[ explicit ] : expressions/operator-expr.html#type-cast-expressions
@@ -125,3 +221,7 @@ let nonsense = circle.radius() * circle.area();
125221[ generics ] : items/generics.html
126222[ where clauses ] : items/generics.html#where-clauses
127223[ generic functions ] : items/functions.html#generic-functions
224+ [ unsafe ] : unsafety.html
225+ [ trait implementation ] : items/implementations.html#trait-implementations
226+ [ `Send` ] : special-types-and-traits.html#send
227+ [ `Sync` ] : special-types-and-traits.html#sync
0 commit comments