From 37dd7a24949813a48c8e2be7178a3c0b5ba5c998 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 1 Jun 2015 23:06:23 +0200 Subject: [PATCH 1/3] book: some tiny improvements --- src/doc/trpl/closures.md | 16 ++++++++-------- src/doc/trpl/trait-objects.md | 4 ++-- src/doc/trpl/traits.md | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/doc/trpl/closures.md b/src/doc/trpl/closures.md index d2fe9c6e550..4479fdb7baa 100644 --- a/src/doc/trpl/closures.md +++ b/src/doc/trpl/closures.md @@ -33,8 +33,8 @@ let plus_two = |x| { assert_eq!(4, plus_two(2)); ``` -You’ll notice a few things about closures that are a bit different than regular -functions defined with `fn`. The first of which is that we did not need to +You’ll notice a few things about closures that are a bit different from regular +functions defined with `fn`. The first is that we did not need to annotate the types of arguments the closure takes or the values it returns. We can: @@ -48,10 +48,10 @@ But we don’t have to. Why is this? Basically, it was chosen for ergonomic reas While specifying the full type for named functions is helpful with things like documentation and type inference, the types of closures are rarely documented since they’re anonymous, and they don’t cause the kinds of error-at-a-distance -that inferring named function types can. +problems that inferring named function types can. The second is that the syntax is similar, but a bit different. I’ve added spaces -here to make them look a little closer: +here for easier comparison: ```rust fn plus_one_v1 (x: i32) -> i32 { x + 1 } @@ -59,7 +59,7 @@ let plus_one_v2 = |x: i32| -> i32 { x + 1 }; let plus_one_v3 = |x: i32| x + 1 ; ``` -Small differences, but they’re similar in ways. +Small differences, but they’re similar. # Closures and their environment @@ -99,7 +99,7 @@ note: previous borrow ends here fn main() { let mut num = 5; let plus_num = |x| x + num; - + let y = &mut num; } ^ @@ -161,7 +161,7 @@ of `num`. So what’s the difference? ```rust let mut num = 5; -{ +{ let mut add_num = |x: i32| num += x; add_num(5); @@ -180,7 +180,7 @@ If we change to a `move` closure, it’s different: ```rust let mut num = 5; -{ +{ let mut add_num = move |x: i32| num += x; add_num(5); diff --git a/src/doc/trpl/trait-objects.md b/src/doc/trpl/trait-objects.md index c0112905741..3da29c9b817 100644 --- a/src/doc/trpl/trait-objects.md +++ b/src/doc/trpl/trait-objects.md @@ -261,7 +261,7 @@ static Foo_for_String_vtable: FooVtable = FooVtable { ``` The `destructor` field in each vtable points to a function that will clean up -any resources of the vtable’s type, for `u8` it is trivial, but for `String` it +any resources of the vtable’s type: for `u8` it is trivial, but for `String` it will free the memory. This is necessary for owning trait objects like `Box`, which need to clean-up both the `Box` allocation as well as the internal type when they go out of scope. The `size` and `align` fields store @@ -270,7 +270,7 @@ essentially unused at the moment since the information is embedded in the destructor, but will be used in the future, as trait objects are progressively made more flexible. -Suppose we’ve got some values that implement `Foo`, then the explicit form of +Suppose we’ve got some values that implement `Foo`. The explicit form of construction and use of `Foo` trait objects might look a bit like (ignoring the type mismatches: they’re all just pointers anyway): diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index 2ef9e7ca22e..ae42b3af656 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -45,7 +45,7 @@ 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`. We can use traits to constrain our generics. Consider this function, which -does not compile, and gives us a similar error: +does not compile: ```rust,ignore fn print_area(shape: T) { @@ -212,8 +212,8 @@ This will compile without error. This means that even if someone does something bad like add methods to `i32`, it won’t affect you, unless you `use` that trait. -There’s one more restriction on implementing traits. Either the trait or the -type you’re writing the `impl` for must be defined by you. So, we could +There’s one more restriction on implementing traits: either the trait, or the +type you’re writing the `impl` for, must be defined by you. So, we could implement the `HasArea` type for `i32`, because `HasArea` is in our code. But if we tried to implement `Float`, a trait provided by Rust, for `i32`, we could not, because neither the trait nor the type are in our code. From dc99fdbd664a936d5f31783774a1362491b31e14 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 1 Jun 2015 23:06:51 +0200 Subject: [PATCH 2/3] book: update error message --- src/doc/trpl/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index ae42b3af656..d3f50884a16 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -56,7 +56,7 @@ fn print_area(shape: T) { Rust complains: ```text -error: type `T` does not implement any method in scope named `area` +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` From 09c6203ad6508dcf3374cddfc2778b861cd9a6be Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Mon, 1 Jun 2015 23:07:42 +0200 Subject: [PATCH 3/3] booK: Float no longer exists --- src/doc/trpl/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index d3f50884a16..162a2db1ec7 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -215,7 +215,7 @@ it won’t affect you, unless you `use` that trait. There’s one more restriction on implementing traits: either the trait, or the type you’re writing the `impl` for, must be defined by you. So, we could implement the `HasArea` type for `i32`, because `HasArea` is in our code. But -if we tried to implement `Float`, a trait provided by Rust, for `i32`, we could +if we tried to implement `ToString`, a trait provided by Rust, for `i32`, we could not, because neither the trait nor the type are in our code. One last thing about traits: generic functions with a trait bound use