Skip to content

Commit f81c2d6

Browse files
committed
---
yaml --- r: 223219 b: refs/heads/auto c: 1181679 h: refs/heads/master i: 223217: 9ed8be8 223215: e593829 v: v3
1 parent 04d2a19 commit f81c2d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+537
-92
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: fd142bb741848236d4ab3a7655e58014d29889da
11+
refs/heads/auto: 1181679c8f84f8ca1c70b60d63ee88336ac363dd
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/doc/nomicon/exotic-sizes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ support values.
8585
Safe code need not worry about ZSTs, but *unsafe* code must be careful about the
8686
consequence of types with no size. In particular, pointer offsets are no-ops,
8787
and standard allocators (including jemalloc, the one used by default in Rust)
88-
generally consider passing in `0` for the size of an allocation as Undefined
89-
Behaviour.
88+
may return `nullptr` when a zero-sized allocation is requested, which is
89+
indistinguishable from out of memory.
9090

9191

9292

branches/auto/src/doc/nomicon/repr-rust.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ struct A {
3636
}
3737
```
3838

39-
will be 32-bit aligned assuming these primitives are aligned to their size.
40-
It will therefore have a size that is a multiple of 32-bits. It will potentially
41-
*really* become:
39+
will be 32-bit aligned on an architecture that aligns these primitives to their
40+
respective sizes. The whole struct will therefore have a size that is a multiple
41+
of 32-bits. It will potentially become:
4242

4343
```rust
4444
struct A {
@@ -50,10 +50,10 @@ struct A {
5050
}
5151
```
5252

53-
There is *no indirection* for these types; all data is stored contiguously as
54-
you would expect in C. However with the exception of arrays (which are densely
55-
packed and in-order), the layout of data is not by default specified in Rust.
56-
Given the two following struct definitions:
53+
There is *no indirection* for these types; all data is stored within the struct,
54+
as you would expect in C. However with the exception of arrays (which are
55+
densely packed and in-order), the layout of data is not by default specified in
56+
Rust. Given the two following struct definitions:
5757

5858
```rust
5959
struct A {
@@ -62,18 +62,17 @@ struct A {
6262
}
6363

6464
struct B {
65-
x: i32,
65+
a: i32,
6666
b: u64,
6767
}
6868
```
6969

7070
Rust *does* guarantee that two instances of A have their data laid out in
71-
exactly the same way. However Rust *does not* guarantee that an instance of A
72-
has the same field ordering or padding as an instance of B (in practice there's
73-
no particular reason why they wouldn't, other than that its not currently
74-
guaranteed).
71+
exactly the same way. However Rust *does not* currently guarantee that an
72+
instance of A has the same field ordering or padding as an instance of B, though
73+
in practice there's no reason why they wouldn't.
7574

76-
With A and B as written, this is basically nonsensical, but several other
75+
With A and B as written, this point would seem to be pedantic, but several other
7776
features of Rust make it desirable for the language to play with data layout in
7877
complex ways.
7978

@@ -133,18 +132,21 @@ struct FooRepr {
133132
}
134133
```
135134

136-
And indeed this is approximately how it would be laid out in general
137-
(modulo the size and position of `tag`). However there are several cases where
138-
such a representation is inefficient. The classic case of this is Rust's
139-
"null pointer optimization". Given a pointer that is known to not be null
140-
(e.g. `&u32`), an enum can *store* a discriminant bit *inside* the pointer
141-
by using null as a special value. The net result is that
142-
`size_of::<Option<&T>>() == size_of::<&T>()`
135+
And indeed this is approximately how it would be laid out in general (modulo the
136+
size and position of `tag`).
137+
138+
However there are several cases where such a representation is inefficient. The
139+
classic case of this is Rust's "null pointer optimization": an enum consisting
140+
of a single outer unit variant (e.g. `None`) and a (potentially nested) non-
141+
nullable pointer variant (e.g. `&T`) makes the tag unnecessary, because a null
142+
pointer value can safely be interpreted to mean that the unit variant is chosen
143+
instead. The net result is that, for example, `size_of::<Option<&T>>() ==
144+
size_of::<&T>()`.
143145

144-
There are many types in Rust that are, or contain, "not null" pointers such as
146+
There are many types in Rust that are, or contain, non-nullable pointers such as
145147
`Box<T>`, `Vec<T>`, `String`, `&T`, and `&mut T`. Similarly, one can imagine
146148
nested enums pooling their tags into a single discriminant, as they are by
147-
definition known to have a limited range of valid values. In principle enums can
149+
definition known to have a limited range of valid values. In principle enums could
148150
use fairly elaborate algorithms to cache bits throughout nested types with
149151
special constrained representations. As such it is *especially* desirable that
150152
we leave enum layout unspecified today.

branches/auto/src/doc/reference.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,29 @@ have an implementation for `Shape`. Multiple supertraits are separated by `+`,
15011501
`trait Circle : Shape + PartialEq { }`. In an implementation of `Circle` for a
15021502
given type `T`, methods can refer to `Shape` methods, since the typechecker
15031503
checks that any type with an implementation of `Circle` also has an
1504-
implementation of `Shape`.
1504+
implementation of `Shape`:
1505+
1506+
```rust
1507+
struct Foo;
1508+
1509+
trait Shape { fn area(&self) -> f64; }
1510+
trait Circle : Shape { fn radius(&self) -> f64; }
1511+
# impl Shape for Foo {
1512+
# fn area(&self) -> f64 {
1513+
# 0.0
1514+
# }
1515+
# }
1516+
impl Circle for Foo {
1517+
fn radius(&self) -> f64 {
1518+
println!("calling area: {}", self.area());
1519+
1520+
0.0
1521+
}
1522+
}
1523+
1524+
let c = Foo;
1525+
c.radius();
1526+
```
15051527

15061528
In type-parameterized functions, methods of the supertrait may be called on
15071529
values of subtrait-bound type parameters. Referring to the previous example of

branches/auto/src/doc/trpl/closures.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,35 @@ assert_eq!(3, answer);
316316
Now we take a trait object, a `&Fn`. And we have to make a reference
317317
to our closure when we pass it to `call_with_one`, so we use `&||`.
318318

319+
# Function pointers and closures
320+
321+
A function pointer is kind of like a closure that has no environment. As such,
322+
you can pass a function pointer to any function expecting a closure argument,
323+
and it will work:
324+
325+
```rust
326+
fn call_with_one(some_closure: &Fn(i32) -> i32) -> i32 {
327+
some_closure(1)
328+
}
329+
330+
fn add_one(i: i32) -> i32 {
331+
i + 1
332+
}
333+
334+
let f = add_one;
335+
336+
let answer = call_with_one(&f);
337+
338+
assert_eq!(2, answer);
339+
```
340+
341+
In this example, we don’t strictly need the intermediate variable `f`,
342+
the name of the function works just fine too:
343+
344+
```ignore
345+
let answer = call_with_one(&add_one);
346+
```
347+
319348
# Returning closures
320349

321350
It’s very common for functional-style code to return closures in various

branches/auto/src/doc/trpl/functions.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,34 @@ as any type:
227227
let x: i32 = diverges();
228228
let x: String = diverges();
229229
```
230+
231+
## Function pointers
232+
233+
We can also create variable bindings which point to functions:
234+
235+
```rust
236+
let f: fn(i32) -> i32;
237+
```
238+
239+
`f` is a variable binding which points to a function that takes an `i32` as
240+
an argument and returns an `i32`. For example:
241+
242+
```rust
243+
fn plus_one(i: i32) -> i32 {
244+
i + 1
245+
}
246+
247+
// without type inference
248+
let f: fn(i32) -> i32 = plus_one;
249+
250+
// with type inference
251+
let f = plus_one;
252+
```
253+
254+
We can then use `f` to call the function:
255+
256+
```rust
257+
# fn plus_one(i: i32) -> i32 { i + 1 }
258+
# let f = plus_one;
259+
let six = f(5);
260+
```

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

Lines changed: 27 additions & 7 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 with type theory, let’s check out some generic code. Rust’s
9+
Anyway, enough 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 do make a `Some(T)`, where `T` is `5`.
30+
the right-hand side of the binding, we 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,11 +101,6 @@ 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-
109104
## Generic structs
110105

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

123118
Similarly to functions, the `<T>` is where we declare the generic parameters,
124119
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/auto/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 work yet:
536+
Here’s our next step, though it doesn’t quite compile 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 work yet, though. Let’s try it:
620+
I did mention that this won’t quite compile yet, though. Let’s try it:
621621

622622
```bash
623623
$ cargo build

branches/auto/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/auto/src/doc/trpl/operators-and-overloading.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,55 @@ 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/auto/src/doc/trpl/references-and-borrowing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ This will print `6`. We make `y` a mutable reference to `x`, then add one to
125125
the thing `y` points at. You’ll notice that `x` had to be marked `mut` as well,
126126
if it wasn’t, we couldn’t take a mutable borrow to an immutable value.
127127

128+
You'll also notice we added an asterisk (`*`) in front of `y`, making it `*y`,
129+
this is because `y` is an `&mut` reference. You'll also need to use them for
130+
accessing the contents of a reference as well.
131+
128132
Otherwise, `&mut` references are just like references. There _is_ a large
129133
difference between the two, and how they interact, though. You can tell
130134
something is fishy in the above example, because we need that extra scope, with

0 commit comments

Comments
 (0)