@@ -47,12 +47,14 @@ As you can see, the `trait` block looks very similar to the `impl` block,
47
47
but we don’t define a body, just a type signature. When we ` impl ` a trait,
48
48
we use ` impl Trait for Item ` , rather than just ` impl Item ` .
49
49
50
- ## Trait constraints on generic functions
50
+ ## Trait bounds on generic functions
51
51
52
52
Traits are useful because they allow a type to make certain promises about its
53
- behavior. Generic functions can exploit this to constrain the types they
53
+ behavior. Generic functions can exploit this to constrain, or [ bound ] [ bounds ] , the types they
54
54
accept. Consider this function, which does not compile:
55
55
56
+ [ bounds ] : glossary.html#bounds
57
+
56
58
``` rust,ignore
57
59
fn print_area<T>(shape: T) {
58
60
println!("This shape has an area of {}", shape.area());
@@ -66,7 +68,7 @@ error: no method named `area` found for type `T` in the current scope
66
68
```
67
69
68
70
Because ` T ` can be any type, we can’t be sure that it implements the ` area `
69
- method. But we can add a ‘ trait constraint’ to our generic ` T ` , ensuring
71
+ method. But we can add a trait bound to our generic ` T ` , ensuring
70
72
that it does:
71
73
72
74
``` rust
@@ -155,10 +157,10 @@ We get a compile-time error:
155
157
error: the trait `HasArea` is not implemented for the type `_` [E0277]
156
158
```
157
159
158
- ## Trait constraints on generic structs
160
+ ## Trait bounds on generic structs
159
161
160
- Your generic structs can also benefit from trait constraints . All you need to
161
- do is append the constraint when you declare type parameters. Here is a new
162
+ Your generic structs can also benefit from trait bounds . All you need to
163
+ do is append the bound when you declare type parameters. Here is a new
162
164
type ` Rectangle<T> ` and its operation ` is_square() ` :
163
165
164
166
``` rust
0 commit comments