Skip to content

Commit c2b30b8

Browse files
committed
Auto merge of #25340 - Manishearth:rollup, r=Manishearth
- Successful merges: #24996, #25220, #25221, #25267, #25322, #25327, #25329, #25330, #25331, #25335 - Failed merges: #25334
2 parents 3ca008d + 2581565 commit c2b30b8

File tree

16 files changed

+443
-89
lines changed

16 files changed

+443
-89
lines changed

src/doc/index.md

+16-12
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.
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.
1111

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.
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
@@ -24,7 +23,7 @@ series of small examples.
2423
# Community & Getting Help
2524

2625
If you need help with something, or just want to talk about Rust with others,
27-
there's a few places you can do that:
26+
there are a few places you can do that:
2827

2928
The Rust IRC channels on [irc.mozilla.org](http://irc.mozilla.org/) are the
3029
fastest way to get help.
@@ -59,7 +58,7 @@ the language in as much detail as possible is in [the reference](reference.html)
5958

6059
# Tools
6160

62-
Rust's still a young language, so there isn't a ton of tooling yet, but the
61+
Rust is still a young language, so there isn't a ton of tooling yet, but the
6362
tools we have are really nice.
6463

6564
[Cargo](http://crates.io) is Rust's package manager, and its website contains
@@ -69,16 +68,21 @@ lots of good documentation.
6968

7069
# FAQs
7170

72-
There are questions that are asked quite often, and so we've made FAQs for them:
71+
There are questions that are asked quite often, so we've made FAQs for them:
7372

7473
* [Language Design FAQ](complement-design-faq.html)
7574
* [Language FAQ](complement-lang-faq.html)
7675
* [Project FAQ](complement-project-faq.html)
7776
* [How to submit a bug report](https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports)
7877

79-
# The standard library
78+
# The Standard Library
8079

8180
We have [API documentation for the entire standard
8281
library](std/index.html). There's a list of crates on the left with more
8382
specific sections, or you can use the search bar at the top to search for
8483
something if you know its name.
84+
85+
# The Error Index
86+
87+
If you encounter an error while compiling your code you may be able to look it
88+
up in the [Rust Compiler Error Index](error-index.html).

src/doc/reference.md

+151-52
Large diffs are not rendered by default.

src/doc/trpl/SUMMARY.md

+1
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)

src/doc/trpl/borrow-and-asref.md

+93
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.

src/doc/trpl/lifetimes.md

+1-1
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

src/doc/trpl/ownership.md

+14-14
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

src/doc/trpl/primitive-types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let x = true;
1515
let y: bool = false;
1616
```
1717

18-
A common use of booleans is in [`if` statements][if].
18+
A common use of booleans is in [`if` conditionals][if].
1919

2020
[if]: if.html
2121

src/doc/trpl/while-loops.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% while loops
1+
% while Loops
22

33
Rust also has a `while` loop. It looks like this:
44

src/libcollections/borrow.rs

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ use self::Cow::*;
3737
/// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
3838
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
3939
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
40+
///
41+
/// `Borrow` is very similar to, but different than, `AsRef`. See
42+
/// [the book][book] for more.
43+
///
44+
/// [book]: ../../book/borrow-and-asref.html
4045
#[stable(feature = "rust1", since = "1.0.0")]
4146
pub trait Borrow<Borrowed: ?Sized> {
4247
/// Immutably borrows from an owned value.

src/libcore/convert.rs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ use marker::Sized;
2424

2525
/// A cheap, reference-to-reference conversion.
2626
///
27+
/// `AsRef` is very similar to, but different than, `Borrow`. See
28+
/// [the book][book] for more.
29+
///
30+
/// [book]: ../../book/borrow-and-asref.html
31+
///
2732
/// # Examples
2833
///
2934
/// Both `String` and `&str` implement `AsRef<str>`:

src/liblibc/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -5722,6 +5722,9 @@ pub mod funcs {
57225722
pub fn tcgetpgrp(fd: c_int) -> pid_t;
57235723
pub fn ttyname(fd: c_int) -> *mut c_char;
57245724
pub fn unlink(c: *const c_char) -> c_int;
5725+
pub fn wait(status: *const c_int) -> pid_t;
5726+
pub fn waitpid(pid: pid_t, status: *const c_int, options: c_int)
5727+
-> pid_t;
57255728
pub fn write(fd: c_int, buf: *const c_void, count: size_t)
57265729
-> ssize_t;
57275730
pub fn pread(fd: c_int, buf: *mut c_void, count: size_t,
@@ -5773,6 +5776,9 @@ pub mod funcs {
57735776
pub fn sysconf(name: c_int) -> c_long;
57745777
pub fn ttyname(fd: c_int) -> *mut c_char;
57755778
pub fn unlink(c: *const c_char) -> c_int;
5779+
pub fn wait(status: *const c_int) -> pid_t;
5780+
pub fn waitpid(pid: pid_t, status: *const c_int, options: c_int)
5781+
-> pid_t;
57765782
pub fn write(fd: c_int, buf: *const c_void, count: size_t)
57775783
-> ssize_t;
57785784
pub fn pread(fd: c_int, buf: *mut c_void, count: size_t,

src/librustc/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ See also http://doc.rust-lang.org/book/unsafe.html
273273

274274
E0137: r##"
275275
This error indicates that the compiler found multiple functions with the
276-
#[main] attribute. This is an error because there must be a unique entry point
277-
into a Rust program.
276+
`#[main]` attribute. This is an error because there must be a unique entry
277+
point into a Rust program.
278278
"##,
279279

280280
E0152: r##"

0 commit comments

Comments
 (0)