From 509bec89fa88b8a946e21e5fe97045e1a7e5f703 Mon Sep 17 00:00:00 2001 From: Chris C Cerami Date: Tue, 20 Oct 2015 21:21:44 -0400 Subject: [PATCH 1/2] Change headers in Traits section of the book --- src/doc/trpl/traits.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index 27debf86e39..6ba0f287be3 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -47,7 +47,7 @@ As you can see, the `trait` block looks very similar to the `impl` block, but we don’t define a body, just a type signature. When we `impl` a trait, we use `impl Trait for Item`, rather than just `impl Item`. -## Traits bounds for generic functions +## Trait constraints on generic functions Traits are useful because they allow a type to make certain promises about its behavior. Generic functions can exploit this to constrain the types they @@ -155,7 +155,7 @@ We get a compile-time error: error: the trait `HasArea` is not implemented for the type `_` [E0277] ``` -## Traits bounds for generic structs +## Trait constraints on generic structs Your generic structs can also benefit from trait constraints. All you need to do is append the constraint when you declare type parameters. Here is a new From 00c1419e32e4c8fcb7947545a7d33e71e38801c5 Mon Sep 17 00:00:00 2001 From: Chris C Cerami Date: Fri, 23 Oct 2015 00:45:44 -0400 Subject: [PATCH 2/2] Define bounds in glossary.md --- src/doc/trpl/glossary.md | 8 ++++++++ src/doc/trpl/traits.md | 14 ++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/doc/trpl/glossary.md b/src/doc/trpl/glossary.md index 106d225d2d0..0956580ade0 100644 --- a/src/doc/trpl/glossary.md +++ b/src/doc/trpl/glossary.md @@ -38,6 +38,14 @@ let z = (8, 2, 6); In the example above `x` and `y` have arity 2. `z` has arity 3. +### Bounds + +Bounds are constraints on a type or [trait][traits]. For example, if a bound +is placed on the argument a function takes, types passed to that function +must abide by that constraint. + +[traits]: traits.html + ### DST (Dynamically Sized Type) A type without a statically known size or alignment. ([more info][link]) diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index 6ba0f287be3..7b6d0b730a3 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -47,12 +47,14 @@ As you can see, the `trait` block looks very similar to the `impl` block, but we don’t define a body, just a type signature. When we `impl` a trait, we use `impl Trait for Item`, rather than just `impl Item`. -## Trait constraints on generic functions +## Trait bounds on generic functions Traits are useful because they allow a type to make certain promises about its -behavior. Generic functions can exploit this to constrain the types they +behavior. Generic functions can exploit this to constrain, or [bound][bounds], the types they accept. Consider this function, which does not compile: +[bounds]: glossary.html#bounds + ```rust,ignore fn print_area(shape: T) { 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 ``` Because `T` can be any type, we can’t be sure that it implements the `area` -method. But we can add a ‘trait constraint’ to our generic `T`, ensuring +method. But we can add a trait bound to our generic `T`, ensuring that it does: ```rust @@ -155,10 +157,10 @@ We get a compile-time error: error: the trait `HasArea` is not implemented for the type `_` [E0277] ``` -## Trait constraints on generic structs +## Trait bounds on generic structs -Your generic structs can also benefit from trait constraints. All you need to -do is append the constraint when you declare type parameters. Here is a new +Your generic structs can also benefit from trait bounds. All you need to +do is append the bound when you declare type parameters. Here is a new type `Rectangle` and its operation `is_square()`: ```rust