Skip to content

Commit 149134c

Browse files
committed
---
yaml --- r: 233197 b: refs/heads/beta c: 1db1417 h: refs/heads/master i: 233195: 9d8bedb v: v3
1 parent 0a3eac4 commit 149134c

File tree

118 files changed

+3111
-2659
lines changed

Some content is hidden

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

118 files changed

+3111
-2659
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: 5847ea76195d9cbe9d066418bdf44ba2a0398649
26+
refs/heads/beta: 1db1417736fa9d682fb0c3e36d37c123c2944a17
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/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/beta/src/doc/nomicon/lifetimes.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ likely desugar to the following:
5252
}
5353
```
5454

55-
Wow. That's... awful. Let's all take a moment to thank Rust for being a
56-
diabetes-inducing torrent of syrupy-goodness.
55+
Wow. That's... awful. Let's all take a moment to thank Rust for making this easier.
5756

5857
Actually passing references to outer scopes will cause Rust to infer
5958
a larger lifetime:

branches/beta/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/beta/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/beta/src/doc/trpl/advanced-linking.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ Static linking refers to the process of creating output that contain all
3838
required libraries and so don't need libraries installed on every system where
3939
you want to use your compiled project. Pure-Rust dependencies are statically
4040
linked by default so you can use created binaries and libraries without
41-
installing the Rust everywhere. By contrast, native libraries
42-
(e.g. `libc` and `libm`) usually dynamically linked, but it is possible to
41+
installing Rust everywhere. By contrast, native libraries
42+
(e.g. `libc` and `libm`) are usually dynamically linked, but it is possible to
4343
change this and statically link them as well.
4444

45-
Linking is a very platform dependent topic — on some platforms, static linking
46-
may not be possible at all! This section assumes some basic familiarity with
45+
Linking is a very platform-dependent topic, and static linking may not even be
46+
possible on some platforms! This section assumes some basic familiarity with
4747
linking on your platform of choice.
4848

4949
## Linux
@@ -71,8 +71,7 @@ Dynamic linking on Linux can be undesirable if you wish to use new library
7171
features on old systems or target systems which do not have the required
7272
dependencies for your program to run.
7373

74-
Static linking is supported via an alternative `libc`, `musl` - this must be
75-
enabled at Rust compile-time with some prerequisites available. You can compile
74+
Static linking is supported via an alternative `libc`, `musl`. You can compile
7675
your own version of Rust with `musl` enabled and install it into a custom
7776
directory with the instructions below:
7877

@@ -123,7 +122,7 @@ $ du -h musldist/bin/rustc
123122
```
124123

125124
You now have a build of a `musl`-enabled Rust! Because we've installed it to a
126-
custom prefix we need to make sure our system can the binaries and appropriate
125+
custom prefix we need to make sure our system can find the binaries and appropriate
127126
libraries when we try and run it:
128127

129128
```text

branches/beta/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/beta/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/beta/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/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 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/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 lifetimes 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>(...)

0 commit comments

Comments
 (0)