3 > **<sup>Syntax</sup>**\
5 > `unsafe`<sup>?</sup> `trait` [IDENTIFIER]
6 > [_Generics_]<sup>?</sup>
7 > ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup>
8 > [_WhereClause_]<sup>?</sup> `{`\
9 > _TraitItem_<sup>\*</sup>\
13 > [_OuterAttribute_]<sup>\*</sup> [_Visibility_]<sup>?</sup> (\
14 > _TraitFunc_\
15 > | _TraitMethod_\
16 > | _TraitConst_\
17 > | _TraitType_\
18 > | [_MacroInvocationSemi_]\
22 > _TraitFunctionDecl_ ( `;` | [_BlockExpression_] )
25 > _TraitMethodDecl_ ( `;` | [_BlockExpression_] )
27 > _TraitFunctionDecl_ :\
28 > [_FunctionQualifiers_] `fn` [IDENTIFIER] [_Generics_]<sup>?</sup>\
29 > `(` _TraitFunctionParameters_<sup>?</sup> `)`\
30 > [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
32 > _TraitMethodDecl_ :\
33 > [_FunctionQualifiers_] `fn` [IDENTIFIER] [_Generics_]<sup>?</sup>\
34 > `(` [_SelfParam_] (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup> `)`\
35 > [_FunctionReturnType_]<sup>?</sup> [_WhereClause_]<sup>?</sup>
37 > _TraitFunctionParameters_ :\
38 > _TraitFunctionParam_ (`,` _TraitFunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
40 > _TraitFunctionParam_<sup>[†](#parameter-patterns)</sup> :\
41 > [_OuterAttribute_]<sup>\*</sup> ( [_Pattern_] `:` )<sup>?</sup> [_Type_]
44 > `const` [IDENTIFIER] `:` [_Type_] ( `=` [_Expression_] )<sup>?</sup> `;`
47 > `type` [IDENTIFIER] ( `:` [_TypeParamBounds_]<sup>?</sup> )<sup>?</sup> `;`
49 A _trait_ describes an abstract interface that types can implement. This
50 interface consists of [associated items], which come in three varieties:
52 - [functions](associated-items.md#associated-functions-and-methods)
53 - [types](associated-items.md#associated-types)
54 - [constants](associated-items.md#associated-constants)
56 All traits define an implicit type parameter `Self` that refers to "the type
57 that is implementing this interface". Traits may also contain additional type
58 parameters. These type parameters, including `Self`, may be constrained by
59 other traits and so forth [as usual][generics].
61 Traits are implemented for specific types through separate [implementations].
63 Items associated with a trait do not need to be defined in the trait, but they
64 may be. If the trait provides a definition, then this definition acts as a
65 default for any implementation which does not override it. If it does not, then
66 any implementation must provide a definition.
70 Generic items may use traits as [bounds] on their type parameters.
74 Type parameters can be specified for a trait to make it generic. These appear
75 after the trait name, using the same syntax used in [generic functions].
80 fn elt_at(&self, n: u32) -> T;
81 fn iter<F>(&self, f: F) where F: Fn(T);
87 Object safe traits can be the base trait of a [trait object]. A trait is
88 *object safe* if it has the following qualities (defined in [RFC 255]):
90 * It must not require `Self: Sized`
91 * All associated functions must either have a `where Self: Sized` bound, or
92 * Not have any type parameters (although lifetime parameters are allowed),
94 * Be a [method] that does not use `Self` except in the type of the receiver.
95 * It must not have any associated constants.
96 * All supertraits must also be object safe.
100 **Supertraits** are traits that are required to be implemented for a type to
101 implement a specific trait. Furthermore, anywhere a [generic][generics] or [trait object]
102 is bounded by a trait, it has access to the associated items of its supertraits.
104 Supertraits are declared by trait bounds on the `Self` type of a trait and
105 transitively the supertraits of the traits declared in those trait bounds. It is
106 an error for a trait to be its own supertrait.
108 The trait with a supertrait is called a **subtrait** of its supertrait.
110 The following is an example of declaring `Shape` to be a supertrait of `Circle`.
113 trait Shape { fn area(&self) -> f64; }
114 trait Circle : Shape { fn radius(&self) -> f64; }
117 And the following is the same example, except using [where clauses].
120 trait Shape { fn area(&self) -> f64; }
121 trait Circle where Self: Shape { fn radius(&self) -> f64; }
124 This next example gives `radius` a default implementation using the `area`
125 function from `Shape`.
128 # trait Shape { fn area(&self) -> f64; }
129 trait Circle where Self: Shape {
130 fn radius(&self) -> f64 {
134 (self.area() /std::f64::consts::PI).sqrt()
139 This next example calls a supertrait method on a generic parameter.
142 # trait Shape { fn area(&self) -> f64; }
143 # trait Circle : Shape { fn radius(&self) -> f64; }
144 fn print_area_and_radius<C: Circle>(c: C) {
145 // Here we call the area method from the supertrait `Shape` of `Circle`.
146 println!("Area: {}", c.area());
147 println!("Radius: {}", c.radius());
151 Similarly, here is an example of calling supertrait methods on trait objects.
154 # trait Shape { fn area(&self) -> f64; }
155 # trait Circle : Shape { fn radius(&self) -> f64; }
157 # impl Shape for UnitCircle { fn area(&self) -> f64 { std::f64::consts::PI } }
158 # impl Circle for UnitCircle { fn radius(&self) -> f64 { 1.0 } }
159 # let circle = UnitCircle;
160 let circle = Box::new(circle) as Box<dyn Circle>;
161 let nonsense = circle.radius() * circle.area();
166 Traits items that begin with the `unsafe` keyword indicate that *implementing* the
167 trait may be [unsafe]. It is safe to use a correctly implemented unsafe trait.
168 The [trait implementation] must also begin with the `unsafe` keyword.
170 [`Sync`] and [`Send`] are examples of unsafe traits.
172 ## Parameter patterns
174 Function or method declarations without a body only allow [IDENTIFIER] or
175 `_` [wild card][WildcardPattern] patterns. `mut` [IDENTIFIER] is currently
176 allowed, but it is deprecated and will become a hard error in the future.
177 <!-- https://github.com/rust-lang/rust/issues/35203 -->
179 In the 2015 edition, the pattern for a trait function or method parameter is
184 fn f(i32); // Parameter identifiers are not required.
188 The kinds of patterns for parameters is limited to one of the following:
192 * [`_`][WildcardPattern]
196 Beginning in the 2018 edition, function or method parameter patterns are no
197 longer optional. Also, all irrefutable patterns are allowed as long as there
198 is a body. Without a body, the limitations listed above are still in effect.
202 fn f1((a, b): (i32, i32)) {}
203 fn f2(_: (i32, i32)); // Cannot use tuple pattern without a body.
209 Trait items syntactically allow a [_Visibility_] annotation, but this is
210 rejected when the trait is validated. This allows items to be parsed with a
211 unified syntax across different contexts where they are used. As an example,
212 an empty `vis` macro fragment specifier can be used for trait items, where the
213 macro rule may be used in other situations where visibility is allowed.
216 macro_rules! create_method {
217 ($vis:vis $name:ident) => {
218 $vis fn $name(&self) {}
223 // Empty `vis` is allowed.
224 create_method! { method_of_t1 }
230 // Visibility is allowed here.
231 create_method! { pub method_of_s }
243 [IDENTIFIER]: ../identifiers.md
244 [WildcardPattern]: ../patterns.md#wildcard-pattern
245 [_BlockExpression_]: ../expressions/block-expr.md
246 [_Expression_]: ../expressions.md
247 [_FunctionQualifiers_]: functions.md
248 [_FunctionReturnType_]: functions.md
249 [_Generics_]: generics.md
250 [_MacroInvocationSemi_]: ../macros.md#macro-invocation
251 [_OuterAttribute_]: ../attributes.md
252 [_Pattern_]: ../patterns.md
253 [_SelfParam_]: associated-items.md#methods
254 [_TypeParamBounds_]: ../trait-bounds.md
255 [_Type_]: ../types.md#type-expressions
256 [_Visibility_]: ../visibility-and-privacy.md
257 [_WhereClause_]: generics.md#where-clauses
258 [bounds]: ../trait-bounds.md
259 [trait object]: ../types/trait-object.md
260 [RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
261 [associated items]: associated-items.md
262 [method]: associated-items.md#methods
263 [implementations]: implementations.md
264 [generics]: generics.md
265 [where clauses]: generics.md#where-clauses
266 [generic functions]: functions.md#generic-functions
267 [unsafe]: ../unsafety.md
268 [trait implementation]: implementations.md#trait-implementations
269 [`Send`]: ../special-types-and-traits.md#send
270 [`Sync`]: ../special-types-and-traits.md#sync