Skip to content

Commit db47aa5

Browse files
committed
auto merge of #16895 : alexcrichton/rust/rollup, r=alexcrichton
Let's try this again!
2 parents 499a40e + 33029c5 commit db47aa5

22 files changed

+385
-228
lines changed

src/doc/guide-pointers.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,31 @@ This part is coming soon.
729729

730730
This part is coming soon.
731731

732+
# Patterns and `ref`
733+
734+
When you're trying to match something that's stored in a pointer, there may be
735+
a situation where matching directly isn't the best option available. Let's see
736+
how to properly handle this:
737+
738+
```{rust,ignore}
739+
fn possibly_print(x: &Option<String>) {
740+
match *x {
741+
// BAD: cannot move out of a `&`
742+
Some(s) => println!("{}", s)
743+
744+
// GOOD: instead take a reference into the memory of the `Option`
745+
Some(ref s) => println!("{}", *s),
746+
None => {}
747+
}
748+
}
749+
```
750+
751+
The `ref s` here means that `s` will be of type `&String`, rather than type
752+
`String`.
753+
754+
This is important when the type you're trying to get access to has a destructor
755+
and you don't want to move it, you just want a reference to it.
756+
732757
# Cheat Sheet
733758

734759
Here's a quick rundown of Rust's pointer types:

src/doc/guide.md

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ $ mv hello_world.rs src/hello_world.rs
297297
```
298298

299299
Cargo expects your source files to live inside a `src` directory. That leaves
300-
the top level for other things, like READMEs, licence information, and anything
300+
the top level for other things, like READMEs, license information, and anything
301301
not related to your code. Cargo helps us keep our projects nice and tidy. A
302302
place for everything, and everything in its place.
303303

@@ -315,7 +315,7 @@ Put this inside:
315315
[package]
316316
317317
name = "hello_world"
318-
version = "0.1.0"
318+
version = "0.0.1"
319319
authors = [ "Your name <[email protected]>" ]
320320
321321
[[bin]]
@@ -630,7 +630,7 @@ In Rust, however, using `let` to introduce a binding is _not_ an expression. The
630630
following will produce a compile-time error:
631631

632632
```{ignore}
633-
let x = (let y = 5i); // found `let` in ident position
633+
let x = (let y = 5i); // expected identifier, found keyword `let`
634634
```
635635

636636
The compiler is telling us here that it was expecting to see the beginning of
@@ -1743,7 +1743,7 @@ fn main() {
17431743
}
17441744
```
17451745

1746-
Sometimes, this makes things more readable. Sometimes, less. Use your judgement
1746+
Sometimes, this makes things more readable. Sometimes, less. Use your judgment
17471747
here.
17481748

17491749
That's all you need to get basic input from the standard input! It's not too
@@ -1813,7 +1813,7 @@ Try it out:
18131813

18141814
```{notrust,ignore}
18151815
$ cargo run
1816-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
1816+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
18171817
Running `target/guessing_game`
18181818
Hello, world!
18191819
```
@@ -1959,7 +1959,7 @@ Try running our new program a few times:
19591959

19601960
```{notrust,ignore}
19611961
$ cargo run
1962-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
1962+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
19631963
Running `target/guessing_game`
19641964
Guess the number!
19651965
The secret number is: 7
@@ -2012,7 +2012,7 @@ And trying it out:
20122012

20132013
```{notrust,ignore}
20142014
$ cargo run
2015-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
2015+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
20162016
Running `target/guessing_game`
20172017
Guess the number!
20182018
The secret number is: 57
@@ -2283,7 +2283,7 @@ print an error message and return. Let's give this a shot:
22832283

22842284
```{notrust,ignore}
22852285
$ cargo run
2286-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
2286+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
22872287
Running `target/guessing_game`
22882288
Guess the number!
22892289
The secret number is: 17
@@ -2348,7 +2348,7 @@ Let's try it!
23482348

23492349
```{notrust,ignore}
23502350
$ cargo run
2351-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
2351+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
23522352
Running `target/guessing_game`
23532353
Guess the number!
23542354
The secret number is: 58
@@ -2425,7 +2425,7 @@ that `return`? If we give a non-number answer, we'll `return` and quit. Observe:
24252425

24262426
```{notrust,ignore}
24272427
$ cargo run
2428-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
2428+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
24292429
Running `target/guessing_game`
24302430
Guess the number!
24312431
The secret number is: 59
@@ -2557,7 +2557,7 @@ Now we should be good! Let's try:
25572557

25582558
```{notrust,ignore}
25592559
$ cargo run
2560-
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
2560+
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
25612561
Running `target/guessing_game`
25622562
Guess the number!
25632563
The secret number is: 61
@@ -2659,7 +2659,7 @@ modules, which can contain other modules, as deeply as you'd like.
26592659
Note that we haven't mentioned anything about files yet. Rust does not impose a
26602660
particular relationship between your filesystem structure and your module
26612661
structure. That said, there is a conventional approach to how Rust looks for
2662-
modules on the file system, but it's also overrideable.
2662+
modules on the file system, but it's also overridable.
26632663

26642664
Enough talk, let's build something! Let's make a new project called `modules`.
26652665

@@ -2670,10 +2670,10 @@ $ cargo new modules --bin
26702670

26712671
Let's double check our work by compiling:
26722672

2673-
```{bash,ignore}
2674-
$ cargo build
2673+
```{bash,notrust}
2674+
$ cargo run
26752675
Compiling modules v0.0.1 (file:///home/you/projects/modules)
2676-
$ ./target/modules
2676+
Running `target/modules`
26772677
Hello, world!
26782678
```
26792679

@@ -3011,7 +3011,7 @@ markers.
30113011
Rust provides six attributes to indicate the stability level of various
30123012
parts of your library. The six levels are:
30133013

3014-
* deprecated: this item should no longer be used. No guarantee of backwards
3014+
* deprecated: This item should no longer be used. No guarantee of backwards
30153015
compatibility.
30163016
* experimental: This item was only recently introduced or is otherwise in a
30173017
state of flux. It may change significantly, or even be removed. No guarantee
@@ -3300,7 +3300,7 @@ To learn more, run the command again with --verbose.
33003300

33013301
Rust can't find this function. That makes sense, as we didn't write it yet!
33023302

3303-
In order to share this codes with our tests, we'll need to make a library crate.
3303+
In order to share this code with our tests, we'll need to make a library crate.
33043304
This is also just good software design: as we mentioned before, it's a good idea
33053305
to put most of your functionality into a library crate, and have your executable
33063306
crate use that library. This allows for code re-use.
@@ -3511,7 +3511,7 @@ exporting the name again, somewhere else.
35113511

35123512
We've now covered the basics of testing. Rust's tools are primitive, but they
35133513
work well in the simple cases. There are some Rustaceans working on building
3514-
more complicated frameworks on top of all of this, but thery're just starting
3514+
more complicated frameworks on top of all of this, but they're just starting
35153515
out.
35163516

35173517
# Pointers
@@ -3668,15 +3668,20 @@ because it's easy. And if you need precise control over when something is
36683668
deallocated, leaving it up to your runtime can make this difficult.
36693669

36703670
Rust chooses a different path, and that path is called **ownership**. Any
3671-
binding that creates a resource is the **owner** of that resource. Being an
3672-
owner gives you three privileges, with two restrictions:
3671+
binding that creates a resource is the **owner** of that resource.
3672+
3673+
Being an owner affords you some privileges:
36733674

36743675
1. You control when that resource is deallocated.
36753676
2. You may lend that resource, immutably, to as many borrowers as you'd like.
3676-
3. You may lend that resource, mutably, to a single borrower. **BUT**
3677-
4. Once you've done so, you may not also lend it out otherwise, mutably or
3678-
immutably.
3679-
5. You may not lend it out mutably if you're currently lending it to someone.
3677+
3. You may lend that resource, mutably, to a single borrower.
3678+
3679+
But it also comes with some restrictions:
3680+
3681+
1. If someone is borrowing your resource (either mutably or immutably), you may
3682+
not mutate the resource or mutably lend it to someone.
3683+
2. If someone is mutably borrowing your resource, you may not lend it out at
3684+
all (mutably or immutably) or access it in any way.
36803685

36813686
What's up with all this 'lending' and 'borrowing'? When you allocate memory,
36823687
you get a pointer to that memory. This pointer allows you to manipulate said
@@ -4063,7 +4068,7 @@ match x {
40634068
}
40644069
```
40654070

4066-
If you have a struct, you can desugar it inside of a pattern:
4071+
If you have a struct, you can destructure it inside of a pattern:
40674072

40684073
```{rust}
40694074
struct Point {
@@ -4223,7 +4228,7 @@ don't need to declare one. This is different from named functions, which
42234228
default to returning unit (`()`).
42244229

42254230
There's one big difference between a closure and named functions, and it's in
4226-
the name: a function "closes over its environment." What's that mean? It means
4231+
the name: a closure "closes over its environment." What's that mean? It means
42274232
this:
42284233

42294234
```{rust}
@@ -5494,7 +5499,7 @@ fn main() {
54945499

54955500
Whew! This isn't too terrible. You can see that we still `let x = 5i`,
54965501
but then things get a little bit hairy. Three more bindings get set: a
5497-
static format string, an argument vector, and the aruments. We then
5502+
static format string, an argument vector, and the arguments. We then
54985503
invoke the `println_args` function with the generated arguments.
54995504

55005505
This is the code (well, the full version) that Rust actually compiles. You can
@@ -5510,7 +5515,7 @@ Guide can help you if you want to write your own.
55105515

55115516
# Unsafe
55125517

5513-
Finally, there's one more concept that you should be aware in Rust: `unsafe`.
5518+
Finally, there's one more Rust concept that you should be aware of: `unsafe`.
55145519
There are two circumstances where Rust's safety provisions don't work well.
55155520
The first is when interfacing with C code, and the second is when building
55165521
certain kinds of abstractions.

src/doc/rust.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,14 +2215,14 @@ These types help drive the compiler's analysis
22152215

22162216
* `begin_unwind`
22172217
: ___Needs filling in___
2218+
* `managed_bound`
2219+
: This type implements "managed"
22182220
* `no_copy_bound`
22192221
: This type does not implement "copy", even if eligible
22202222
* `no_send_bound`
22212223
: This type does not implement "send", even if eligible
2222-
* `no_sync_bound`
2223-
: This type does not implement "sync", even if eligible
2224-
* `managed_bound`
2225-
: This type implements "managed"
2224+
* `no_share_bound`
2225+
: This type does not implement "share", even if eligible
22262226
* `eh_personality`
22272227
: ___Needs filling in___
22282228
* `exchange_free`

src/liballoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! This is the lowest level library through which allocation in Rust can be
1414
//! performed where the allocation is assumed to succeed. This library will
15-
//! trigger a task failure when allocation fails.
15+
//! abort the process when allocation fails.
1616
//!
1717
//! This library, like libcore, is not intended for general usage, but rather as
1818
//! a building block of other libraries. The types and interfaces in this

src/libcore/num/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ pub trait Signed: Num + Neg<Self> {
160160
/// Computes the absolute value.
161161
///
162162
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
163+
///
164+
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
163165
fn abs(&self) -> Self;
164166

165167
/// The positive difference of two numbers.
@@ -176,7 +178,7 @@ pub trait Signed: Num + Neg<Self> {
176178
/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
177179
/// * `NaN` if the number is `NaN`
178180
///
179-
/// For `int`:
181+
/// For signed integers:
180182
///
181183
/// * `0` if the number is zero
182184
/// * `1` if the number is positive
@@ -272,6 +274,8 @@ signed_float_impl!(f64, f64::NAN, f64::INFINITY, f64::NEG_INFINITY,
272274
/// Computes the absolute value.
273275
///
274276
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`
277+
///
278+
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
275279
#[inline(always)]
276280
pub fn abs<T: Signed>(value: T) -> T {
277281
value.abs()
@@ -294,7 +298,7 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
294298
/// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
295299
/// * `NaN` if the number is `NaN`
296300
///
297-
/// For int:
301+
/// For signed integers:
298302
///
299303
/// * `0` if the number is zero
300304
/// * `1` if the number is positive

0 commit comments

Comments
 (0)