Skip to content

Commit 766386d

Browse files
committed
---
yaml --- r: 208376 b: refs/heads/snap-stage3 c: e216057 h: refs/heads/master v: v3
1 parent eb78fae commit 766386d

38 files changed

+1503
-513
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 38a97becdf3e6a6157f6f7ec2d98ade8d8edc193
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: d13f765be8d202450bf106057e19d6bd57f51f6c
4+
refs/heads/snap-stage3: e216057dac596904478430858affa1e0bf2cf7d7
55
refs/heads/try: 7b4ef47b7805a402d756fb8157101f64880a522f
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d

branches/snap-stage3/src/doc/index.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ to jump to any particular section.
55

66
# Getting Started
77

8-
If you haven't seen Rust at all yet, the first thing you should read is the [30
9-
minute intro](intro.html). It will give you an overview of the basic ideas of Rust
10-
at a high level.
11-
12-
Once you know you really want to learn Rust, the next step is reading [The
13-
Rust Programming Language](book/index.html). It is a lengthy explanation of
14-
Rust, its syntax, and its concepts. Upon completing the book, you'll be an
15-
intermediate Rust developer, and will have a good grasp of the fundamental
16-
ideas behind Rust.
8+
If you haven't seen Rust at all yet, the first thing you should read is the
9+
introduction to [The Rust Programming Language](book/index.html). It'll give
10+
you a good idea of what Rust is like.
11+
12+
The book provides a lengthy explanation of Rust, its syntax, and its
13+
concepts. Upon completing the book, you'll be an intermediate Rust
14+
developer, and will have a good grasp of the fundamental ideas behind
15+
Rust.
1716

1817
[Rust By Example][rbe] was originally a community resource, but was then
1918
donated to the Rust project. As the name implies, it teaches you Rust through a

branches/snap-stage3/src/doc/reference.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,27 @@ You may also be interested in the [grammar].
3131

3232
## Unicode productions
3333

34-
A few productions in Rust's grammar permit Unicode code points outside the ASCII
35-
range. We define these productions in terms of character properties specified
36-
in the Unicode standard, rather than in terms of ASCII-range code points. The
37-
section [Special Unicode Productions](#special-unicode-productions) lists these
38-
productions.
34+
A few productions in Rust's grammar permit Unicode code points outside the
35+
ASCII range. We define these productions in terms of character properties
36+
specified in the Unicode standard, rather than in terms of ASCII-range code
37+
points. The grammar has a [Special Unicode Productions][unicodeproductions]
38+
section that lists these productions.
39+
40+
[unicodeproductions]: grammar.html#special-unicode-productions
3941

4042
## String table productions
4143

4244
Some rules in the grammar — notably [unary
4345
operators](#unary-operator-expressions), [binary
44-
operators](#binary-operator-expressions), and [keywords](#keywords) — are
46+
operators](#binary-operator-expressions), and [keywords][keywords] — are
4547
given in a simplified form: as a listing of a table of unquoted, printable
4648
whitespace-separated strings. These cases form a subset of the rules regarding
4749
the [token](#tokens) rule, and are assumed to be the result of a
4850
lexical-analysis phase feeding the parser, driven by a DFA, operating over the
4951
disjunction of all such string table entries.
5052

53+
[keywords]: grammar.html#keywords
54+
5155
When such a string enclosed in double-quotes (`"`) occurs inside the grammar,
5256
it is an implicit reference to a single member of such a string table
5357
production. See [tokens](#tokens) for more information.
@@ -75,7 +79,7 @@ An identifier is any nonempty Unicode[^non_ascii_idents] string of the following
7579
- The first character has property `XID_start`
7680
- The remaining characters have property `XID_continue`
7781

78-
that does _not_ occur in the set of [keywords](#keywords).
82+
that does _not_ occur in the set of [keywords][keywords].
7983

8084
> **Note**: `XID_start` and `XID_continue` as character properties cover the
8185
> character ranges used to form the more familiar C and Java language-family
@@ -401,7 +405,7 @@ Symbols are a general class of printable [token](#tokens) that play structural
401405
roles in a variety of grammar productions. They are catalogued here for
402406
completeness as the set of remaining miscellaneous printable tokens that do not
403407
otherwise appear as [unary operators](#unary-operator-expressions), [binary
404-
operators](#binary-operator-expressions), or [keywords](#keywords).
408+
operators](#binary-operator-expressions), or [keywords][keywords].
405409

406410

407411
## Paths
@@ -547,7 +551,7 @@ _name_ s that occur in its body. At the "current layer", they all must repeat
547551
the same number of times, so ` ( $( $i:ident ),* ; $( $j:ident ),* ) => ( $(
548552
($i,$j) ),* )` is valid if given the argument `(a,b,c ; d,e,f)`, but not
549553
`(a,b,c ; d,e)`. The repetition walks through the choices at that layer in
550-
lockstep, so the former input transcribes to `( (a,d), (b,e), (c,f) )`.
554+
lockstep, so the former input transcribes to `(a,d), (b,e), (c,f)`.
551555

552556
Nested repetitions are allowed.
553557

@@ -611,7 +615,7 @@ module needs its own source file: [module definitions](#modules) can be nested
611615
within one file.
612616

613617
Each source file contains a sequence of zero or more `item` definitions, and
614-
may optionally begin with any number of [attributes](#Items and attributes)
618+
may optionally begin with any number of [attributes](#items-and-attributes)
615619
that apply to the containing module, most of which influence the behavior of
616620
the compiler. The anonymous crate module can have additional attributes that
617621
apply to the crate as a whole.
@@ -653,7 +657,7 @@ There are several kinds of item:
653657
* [`use` declarations](#use-declarations)
654658
* [modules](#modules)
655659
* [functions](#functions)
656-
* [type aliases](#type-aliases)
660+
* [type definitions](grammar.html#type-definitions)
657661
* [structures](#structures)
658662
* [enumerations](#enumerations)
659663
* [constant items](#constant-items)
@@ -773,7 +777,7 @@ extern crate std as ruststd; // linking to 'std' under another name
773777
A _use declaration_ creates one or more local name bindings synonymous with
774778
some other [path](#paths). Usually a `use` declaration is used to shorten the
775779
path required to refer to a module item. These declarations may appear at the
776-
top of [modules](#modules) and [blocks](#blocks).
780+
top of [modules](#modules) and [blocks](grammar.html#block-expressions).
777781

778782
> **Note**: Unlike in many languages,
779783
> `use` declarations in Rust do *not* declare linkage dependency with external crates.
@@ -1144,9 +1148,7 @@ let px: i32 = match p { Point(x, _) => x };
11441148
```
11451149

11461150
A _unit-like struct_ is a structure without any fields, defined by leaving off
1147-
the list of fields entirely. Such types will have a single value, just like
1148-
the [unit value `()`](#unit-and-boolean-literals) of the unit type. For
1149-
example:
1151+
the list of fields entirely. Such types will have a single value. For example:
11501152

11511153
```
11521154
struct Cookie;
@@ -2436,11 +2438,6 @@ comma:
24362438
(0); // zero in parentheses
24372439
```
24382440

2439-
### Unit expressions
2440-
2441-
The expression `()` denotes the _unit value_, the only value of the type with
2442-
the same name.
2443-
24442441
### Structure expressions
24452442

24462443
There are several forms of structure expressions. A _structure expression_
@@ -3281,7 +3278,7 @@ constructor or `struct` field may refer, directly or indirectly, to the
32813278
enclosing `enum` or `struct` type itself. Such recursion has restrictions:
32823279

32833280
* Recursive types must include a nominal type in the recursion
3284-
(not mere [type definitions](#type-definitions),
3281+
(not mere [type definitions](grammar.html#type-definitions),
32853282
or other structural types such as [arrays](#array,-and-slice-types) or [tuples](#tuple-types)).
32863283
* A recursive `enum` item must have at least one non-recursive constructor
32873284
(in order to give the recursion a basis case).

branches/snap-stage3/src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [Concurrency](concurrency.md)
1616
* [Error Handling](error-handling.md)
1717
* [FFI](ffi.md)
18+
* [Borrow and AsRef](borrow-and-asref.md)
1819
* [Syntax and Semantics](syntax-and-semantics.md)
1920
* [Variable Bindings](variable-bindings.md)
2021
* [Functions](functions.md)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
% Borrow and AsRef
2+
3+
The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but
4+
different. Here’s a quick refresher on what these two traits mean.
5+
6+
[borrow]: ../std/borrow/trait.Borrow.html
7+
[asref]: ../std/convert/trait.AsRef.html
8+
9+
# Borrow
10+
11+
The `Borrow` trait is used when you’re writing a datastructure, and you want to
12+
use either an owned or borrowed type as synonymous for some purpose.
13+
14+
For example, [`HashMap`][hashmap] has a [`get` method][get] which uses `Borrow`:
15+
16+
```rust,ignore
17+
fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
18+
where K: Borrow<Q>,
19+
Q: Hash + Eq
20+
```
21+
22+
[hashmap]: ../std/collections/struct.HashMap.html
23+
[get]: ../std/collections/struct.HashMap.html#method.get
24+
25+
This signature is pretty complicated. The `K` parameter is what we’re interested
26+
in here. It refers to a parameter of the `HashMap` itself:
27+
28+
```rust,ignore
29+
struct HashMap<K, V, S = RandomState> {
30+
```
31+
32+
The `K` parameter is the type of _key_ the `HashMap` uses. So, looking at
33+
the signature of `get()` again, we can use `get()` when the key implements
34+
`Borrow<Q>`. That way, we can make a `HashMap` which uses `String` keys,
35+
but use `&str`s when we’re searching:
36+
37+
```rust
38+
use std::collections::HashMap;
39+
40+
let mut map = HashMap::new();
41+
map.insert("Foo".to_string(), 42);
42+
43+
assert_eq!(map.get("Foo"), Some(&42));
44+
```
45+
46+
This is because the standard library has `impl Borrow<str> for String`.
47+
48+
For most types, when you want to take an owned or borrowed type, a `&T` is
49+
enough. But one area where `Borrow` is effective is when there’s more than one
50+
kind of borrowed value. Slices are an area where this is especially true: you
51+
can have both an `&[T]` or a `&mut [T]`. If we wanted to accept both of these
52+
types, `Borrow` is up for it:
53+
54+
```
55+
use std::borrow::Borrow;
56+
use std::fmt::Display;
57+
58+
fn foo<T: Borrow<i32> + Display>(a: T) {
59+
println!("a is borrowed: {}", a);
60+
}
61+
62+
let mut i = 5;
63+
64+
foo(&i);
65+
foo(&mut i);
66+
```
67+
68+
This will print out `a is borrowed: 5` twice.
69+
70+
# AsRef
71+
72+
The `AsRef` trait is a conversion trait. It’s used for converting some value to
73+
a reference in generic code. Like this:
74+
75+
```rust
76+
let s = "Hello".to_string();
77+
78+
fn foo<T: AsRef<str>>(s: T) {
79+
let slice = s.as_ref();
80+
}
81+
```
82+
83+
# Which should I use?
84+
85+
We can see how they’re kind of the same: they both deal with owned and borrowed
86+
versions of some type. However, they’re a bit different.
87+
88+
Choose `Borrow` when you want to abstract over different kinds of borrowing, or
89+
when you’re building a datastructure that treats owned and borrowed values in
90+
equivalent ways, such as hashing and comparison.
91+
92+
Choose `AsRef` when you want to convert something to a reference directly, and
93+
you’re writing generic code.

branches/snap-stage3/src/doc/trpl/compiler-plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ for a full example, the core of which is reproduced here:
176176

177177
```ignore
178178
declare_lint!(TEST_LINT, Warn,
179-
"Warn about items named 'lintme'")
179+
"Warn about items named 'lintme'");
180180
181181
struct Pass;
182182

branches/snap-stage3/src/doc/trpl/lifetimes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Rust’s most unique and compelling features, with which Rust developers should
55
become quite acquainted. Ownership is how Rust achieves its largest goal,
66
memory safety. There are a few distinct concepts, each with its own chapter:
77

8-
* [ownership][ownership], ownership, the key concept
8+
* [ownership][ownership], the key concept
99
* [borrowing][borrowing], and their associated feature ‘references’
1010
* lifetimes, which you’re reading now
1111

branches/snap-stage3/src/doc/trpl/method-syntax.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ fn grow(&self) -> Circle {
127127
We just say we’re returning a `Circle`. With this method, we can grow a new
128128
circle to any arbitrary size.
129129

130-
# Static methods
130+
# Associated functions
131131

132-
You can also define static methods that do not take a `self` parameter. Here’s a
133-
pattern that’s very common in Rust code:
132+
You can also define associated functions that do not take a `self` parameter.
133+
Here’s a pattern that’s very common in Rust code:
134134

135-
```
135+
```rust
136136
struct Circle {
137137
x: f64,
138138
y: f64,

branches/snap-stage3/src/doc/trpl/ownership.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ become quite acquainted. Ownership is how Rust achieves its largest goal,
66
memory safety. There are a few distinct concepts, each with its own
77
chapter:
88

9-
* ownership, which you’re reading now.
9+
* ownership, which you’re reading now
1010
* [borrowing][borrowing], and their associated feature ‘references’
1111
* [lifetimes][lifetimes], an advanced concept of borrowing
1212

@@ -23,7 +23,7 @@ Before we get to the details, two important notes about the ownership system.
2323
Rust has a focus on safety and speed. It accomplishes these goals through many
2424
‘zero-cost abstractions’, which means that in Rust, abstractions cost as little
2525
as possible in order to make them work. The ownership system is a prime example
26-
of a zero cost abstraction. All of the analysis we’ll talk about in this guide
26+
of a zero-cost abstraction. All of the analysis we’ll talk about in this guide
2727
is _done at compile time_. You do not pay any run-time cost for any of these
2828
features.
2929

@@ -41,7 +41,7 @@ With that in mind, let’s learn about ownership.
4141

4242
# Ownership
4343

44-
[`Variable bindings`][bindings] have a property in Rust: they ‘have ownership’
44+
[Variable bindings][bindings] have a property in Rust: they ‘have ownership’
4545
of what they’re bound to. This means that when a binding goes out of scope, the
4646
resource that they’re bound to are freed. For example:
4747

@@ -106,8 +106,8 @@ take(v);
106106
println!("v[0] is: {}", v[0]);
107107
```
108108

109-
Same error: use of moved value.” When we transfer ownership to something else,
110-
we say that we’ve ‘moved’ the thing we refer to. You don’t need some sort of
109+
Same error: use of moved value’. When we transfer ownership to something else,
110+
we say that we’ve ‘moved’ the thing we refer to. You don’t need any sort of
111111
special annotation here, it’s the default thing that Rust does.
112112

113113
## The details
@@ -121,19 +121,19 @@ let v = vec![1, 2, 3];
121121
let v2 = v;
122122
```
123123

124-
The first line creates some data for the vector on the [stack][sh], `v`. The
125-
vector’s data, however, is stored on the [heap][sh], and so it contains a
126-
pointer to that data. When we move `v` to `v2`, it creates a copy of that pointer,
127-
for `v2`. Which would mean two pointers to the contents of the vector on the
128-
heap. That would be a problem: it would violate Rust’s safety guarantees by
129-
introducing a data race. Therefore, Rust forbids using `v` after we’ve done the
130-
move.
124+
The first line allocates memory for the vector object, `v`, and for the data it
125+
contains. The vector object is stored on the [stack][sh] and contains a pointer
126+
to the content (`[1, 2, 3]`) stored on the [heap][sh]. When we move `v` to `v2`,
127+
it creates a copy of that pointer, for `v2`. Which means that there would be two
128+
pointers to the content of the vector on the heap. It would violate Rust’s
129+
safety guarantees by introducing a data race. Therefore, Rust forbids using `v`
130+
after we’ve done the move.
131131

132132
[sh]: the-stack-and-the-heap.html
133133

134134
It’s also important to note that optimizations may remove the actual copy of
135-
the bytes, depending on circumstances. So it may not be as inefficient as it
136-
initially seems.
135+
the bytes on the stack, depending on circumstances. So it may not be as
136+
inefficient as it initially seems.
137137

138138
## `Copy` types
139139

0 commit comments

Comments
 (0)