Skip to content

Commit a6a7772

Browse files
committed
---
yaml --- r: 233105 b: refs/heads/beta c: 8f828a3 h: refs/heads/master i: 233103: 6158dab v: v3
1 parent 64e123b commit a6a7772

File tree

8 files changed

+30
-159
lines changed

8 files changed

+30
-159
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: 3a2af8736341f5afeb3372ba7c112b2c75ce9584
26+
refs/heads/beta: 8f828a3a9d66a3ff2465861f1d8f12dd0f088d23
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: 370fe2786109360f7c35b8ba552b83b773dd71d6
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/doc/trpl/generics.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Generics are called ‘parametric polymorphism’ in type theory,
66
which means that they are types or functions that have multiple forms (‘poly’
77
is multiple, ‘morph’ is form) over a given parameter (‘parametric’).
88

9-
Anyway, enough type theory, let’s check out some generic code. Rust’s
9+
Anyway, enough with type theory, let’s check out some generic code. Rust’s
1010
standard library provides a type, `Option<T>`, that’s generic:
1111

1212
```rust
@@ -27,7 +27,7 @@ let x: Option<i32> = Some(5);
2727

2828
In the type declaration, we say `Option<i32>`. Note how similar this looks to
2929
`Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On
30-
the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`.
30+
the right-hand side of the binding, we do make a `Some(T)`, where `T` is `5`.
3131
Since that’s an `i32`, the two sides match, and Rust is happy. If they didn’t
3232
match, we’d get an error:
3333

@@ -101,6 +101,11 @@ fn takes_two_things<T, U>(x: T, y: U) {
101101
}
102102
```
103103

104+
Generic functions are most useful with ‘trait bounds’, which we’ll cover in the
105+
[section on traits][traits].
106+
107+
[traits]: traits.html
108+
104109
## Generic structs
105110

106111
You can store a generic type in a `struct` as well:
@@ -117,28 +122,3 @@ let float_origin = Point { x: 0.0, y: 0.0 };
117122

118123
Similarly to functions, the `<T>` is where we declare the generic parameters,
119124
and we then use `x: T` in the type declaration, too.
120-
121-
When you want to add an implementation for the generic struct, you just
122-
declare the type parameter after the `impl`:
123-
124-
```rust
125-
# struct Point<T> {
126-
# x: T,
127-
# y: T,
128-
# }
129-
#
130-
impl<T> Point<T> {
131-
fn swap(&mut self) {
132-
std::mem::swap(&mut self.x, &mut self.y);
133-
}
134-
}
135-
```
136-
137-
So far you’ve seen generics that take absolutely any type. These are useful in
138-
many cases: you’ve already seen `Option<T>`, and later you’ll meet universal
139-
container types like [`Vec<T>`][Vec]. On the other hand, often you want to
140-
trade that flexibility for increased expressive power. Read about [trait
141-
bounds][traits] to see why and how.
142-
143-
[traits]: traits.html
144-
[Vec]: ../std/vec/struct.Vec.html

branches/beta/src/doc/trpl/guessing-game.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ Great! Next up: let’s compare our guess to the secret guess.
533533
# Comparing guesses
534534

535535
Now that we’ve got user input, let’s compare our guess to the random guess.
536-
Here’s our next step, though it doesn’t quite compile yet:
536+
Here’s our next step, though it doesn’t quite work yet:
537537

538538
```rust,ignore
539539
extern crate rand;
@@ -617,7 +617,7 @@ match guess.cmp(&secret_number) {
617617
If it’s `Less`, we print `Too small!`, if it’s `Greater`, `Too big!`, and if
618618
`Equal`, `You win!`. `match` is really useful, and is used often in Rust.
619619

620-
I did mention that this won’t quite compile yet, though. Let’s try it:
620+
I did mention that this won’t quite work yet, though. Let’s try it:
621621

622622
```bash
623623
$ cargo build

branches/beta/src/doc/trpl/lifetimes.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,18 @@ Before we get to that, though, let’s break the explicit example down:
7777
fn bar<'a>(...)
7878
```
7979

80-
This part declares our lifetimes. This says that `bar` has one lifetime, `'a`.
81-
If we had two reference parameters, it would look like this:
80+
We previously talked a little about [function syntax][functions], but we didn’t
81+
discuss the `<>`s after a function’s name. A function can have ‘generic
82+
parameters’ between the `<>`s, of which lifetimes are one kind. We’ll discuss
83+
other kinds of generics [later in the book][generics], but for now, let’s
84+
just focus on the lifteimes aspect.
85+
86+
[functions]: functions.html
87+
[generics]: generics.html
88+
89+
We use `<>` to declare our lifetimes. This says that `bar` has one lifetime,
90+
`'a`. If we had two reference parameters, it would look like this:
91+
8292

8393
```rust,ignore
8494
fn bar<'a, 'b>(...)

branches/beta/src/doc/trpl/operators-and-overloading.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -81,55 +81,3 @@ will let you do this:
8181
let p: Point = // ...
8282
let x: f64 = p + 2i32;
8383
```
84-
85-
# Using operator traits in generic structs
86-
87-
Now that we know how operator traits are defined, we can define our `HasArea`
88-
trait and `Square` struct from the [traits chapter][traits] more generically:
89-
90-
[traits]: traits.html
91-
92-
```rust
93-
use std::ops::Mul;
94-
95-
trait HasArea<T> {
96-
fn area(&self) -> T;
97-
}
98-
99-
struct Square<T> {
100-
x: T,
101-
y: T,
102-
side: T,
103-
}
104-
105-
impl<T> HasArea<T> for Square<T>
106-
where T: Mul<Output=T> + Copy {
107-
fn area(&self) -> T {
108-
self.side * self.side
109-
}
110-
}
111-
112-
fn main() {
113-
let s = Square {
114-
x: 0.0f64,
115-
y: 0.0f64,
116-
side: 12.0f64,
117-
};
118-
119-
println!("Area of s: {}", s.area());
120-
}
121-
```
122-
123-
For `HasArea` and `Square`, we just declare a type parameter `T` and replace
124-
`f64` with it. The `impl` needs more involved modifications:
125-
126-
```ignore
127-
impl<T> HasArea<T> for Square<T>
128-
where T: Mul<Output=T> + Copy { ... }
129-
```
130-
131-
The `area` method requires that we can multiply the sides, so we declare that
132-
type `T` must implement `std::ops::Mul`. Like `Add`, mentioned above, `Mul`
133-
itself takes an `Output` parameter: since we know that numbers don't change
134-
type when multiplied, we also set it to `T`. `T` must also support copying, so
135-
Rust doesn't try to move `self.side` into the return value.

branches/beta/src/doc/trpl/traits.md

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ As you can see, the `trait` block looks very similar to the `impl` block,
4747
but we don’t define a body, just a type signature. When we `impl` a trait,
4848
we use `impl Trait for Item`, rather than just `impl Item`.
4949

50-
## Traits bounds for generic functions
51-
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
54-
accept. Consider this function, which does not compile:
50+
We can use traits to constrain our generics. Consider this function, which
51+
does not compile:
5552

5653
```rust,ignore
5754
fn print_area<T>(shape: T) {
@@ -78,7 +75,7 @@ fn print_area<T: HasArea>(shape: T) {
7875
}
7976
```
8077

81-
The syntax `<T: HasArea>` means any type that implements the `HasArea` trait.”
78+
The syntax `<T: HasArea>` means `any type that implements the HasArea trait`.
8279
Because traits define function type signatures, we can be sure that any type
8380
which implements `HasArea` will have an `.area()` method.
8481

@@ -155,63 +152,6 @@ We get a compile-time error:
155152
error: the trait `HasArea` is not implemented for the type `_` [E0277]
156153
```
157154

158-
## Traits bounds for generic structs
159-
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-
type `Rectangle<T>` and its operation `is_square()`:
163-
164-
```rust
165-
struct Rectangle<T> {
166-
x: T,
167-
y: T,
168-
width: T,
169-
height: T,
170-
}
171-
172-
impl<T: PartialEq> Rectangle<T> {
173-
fn is_square(&self) -> bool {
174-
self.width == self.height
175-
}
176-
}
177-
178-
fn main() {
179-
let mut r = Rectangle {
180-
x: 0,
181-
y: 0,
182-
width: 47,
183-
height: 47,
184-
};
185-
186-
assert!(r.is_square());
187-
188-
r.height = 42;
189-
assert!(!r.is_square());
190-
}
191-
```
192-
193-
`is_square()` needs to check that the sides are equal, so the sides must be of
194-
a type that implements the [`core::cmp::PartialEq`][PartialEq] trait:
195-
196-
```ignore
197-
impl<T: PartialEq> Rectangle<T> { ... }
198-
```
199-
200-
Now, a rectangle can be defined in terms of any type that can be compared for
201-
equality.
202-
203-
[PartialEq]: ../core/cmp/trait.PartialEq.html
204-
205-
Here we defined a new struct `Rectangle` that accepts numbers of any
206-
precision—really, objects of pretty much any type—as long as they can be
207-
compared for equality. Could we do the same for our `HasArea` structs, `Square`
208-
and `Circle`? Yes, but they need multiplication, and to work with that we need
209-
to know more about [operator traits][operators-and-overloading].
210-
211-
[operators-and-overloading]: operators-and-overloading.html
212-
213-
# Rules for implementing traits
214-
215155
So far, we’ve only added trait implementations to structs, but you can
216156
implement a trait for any type. So technically, we _could_ implement `HasArea`
217157
for `i32`:
@@ -235,7 +175,7 @@ impl HasArea for i32 {
235175
It is considered poor style to implement methods on such primitive types, even
236176
though it is possible.
237177

238-
This may seem like the Wild West, but there are two restrictions around
178+
This may seem like the Wild West, but there are two other restrictions around
239179
implementing traits that prevent this from getting out of hand. The first is
240180
that if the trait isn’t defined in your scope, it doesn’t apply. Here’s an
241181
example: the standard library provides a [`Write`][write] trait which adds
@@ -400,10 +340,10 @@ This shows off the additional feature of `where` clauses: they allow bounds
400340
where the left-hand side is an arbitrary type (`i32` in this case), not just a
401341
plain type parameter (like `T`).
402342

403-
# Default methods
343+
## Default methods
404344

405-
If you already know how a typical implementor will define a method, you can
406-
let your trait supply a default:
345+
There’s one last feature of traits we should cover: default methods. It’s
346+
easiest just to show an example:
407347

408348
```rust
409349
trait Foo {

branches/beta/src/libcollections/borrow.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ use self::Cow::*;
3838
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
3939
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
4040
///
41-
/// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
42-
/// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
43-
///
4441
/// `Borrow` is very similar to, but different than, `AsRef`. See
4542
/// [the book][book] for more.
4643
///

branches/beta/src/libcore/marker.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,7 @@ macro_rules! impls{
273273
/// even though it does not. This allows you to inform the compiler about certain safety properties
274274
/// of your code.
275275
///
276-
/// # A ghastly note 👻👻👻
277-
///
278-
/// Though they both have scary names, `PhantomData<T>` and 'phantom types' are related, but not
279-
/// identical. Phantom types are a more general concept that don't require `PhantomData<T>` to
280-
/// implement, but `PhantomData<T>` is the most common way to implement them in a correct manner.
276+
/// Though they both have scary names, `PhantomData<T>` and "phantom types" are unrelated. 👻👻👻
281277
///
282278
/// # Examples
283279
///

0 commit comments

Comments
 (0)