Skip to content

Commit 3b4e96c

Browse files
committed
---
yaml --- r: 272479 b: refs/heads/master c: c584283 h: refs/heads/master i: 272477: 78d9eef 272475: f2a2d0d 272471: 98f366c 272463: a4edd3f 272447: 26311af
1 parent c2b138f commit 3b4e96c

File tree

14 files changed

+284
-37
lines changed

14 files changed

+284
-37
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: b2db97347bc4373deea24eb7b7c6ecffb117fd8c
2+
refs/heads/master: c5842837b86abc720446055118bf714dabe30730
33
refs/heads/snap-stage3: 235d77457d80b549dad3ac36d94f235208a1eafb
44
refs/heads/try: 49312a405e14a449b98fe0056b12a40ac128be4a
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

trunk/src/doc/book/concurrency.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ The same [ownership system](ownership.html) that helps prevent using pointers
162162
incorrectly also helps rule out data races, one of the worst kinds of
163163
concurrency bugs.
164164

165-
As an example, here is a Rust program that could have a data race in many
165+
As an example, here is a Rust program that would have a data race in many
166166
languages. It will not compile:
167167

168168
```ignore
@@ -174,7 +174,7 @@ fn main() {
174174
175175
for i in 0..3 {
176176
thread::spawn(move || {
177-
data[i] += 1;
177+
data[0] += i;
178178
});
179179
}
180180
@@ -186,7 +186,7 @@ This gives us an error:
186186

187187
```text
188188
8:17 error: capture of moved value: `data`
189-
data[i] += 1;
189+
data[0] += i;
190190
^~~~
191191
```
192192

@@ -195,11 +195,6 @@ thread, and the thread takes ownership of the reference, we'd have three owners!
195195
`data` gets moved out of `main` in the first call to `spawn()`, so subsequent
196196
calls in the loop cannot use this variable.
197197

198-
Note that this specific example will not cause a data race since different array
199-
indices are being accessed. But this can't be determined at compile time, and in
200-
a similar situation where `i` is a constant or is random, you would have a data
201-
race.
202-
203198
So, we need some type that lets us have more than one owning reference to a
204199
value. Usually, we'd use `Rc<T>` for this, which is a reference counted type
205200
that provides shared ownership. It has some runtime bookkeeping that keeps track
@@ -223,7 +218,7 @@ fn main() {
223218
224219
// use it in a thread
225220
thread::spawn(move || {
226-
data_ref[i] += 1;
221+
data_ref[0] += i;
227222
});
228223
}
229224
@@ -266,7 +261,7 @@ fn main() {
266261
for i in 0..3 {
267262
let data = data.clone();
268263
thread::spawn(move || {
269-
data[i] += 1;
264+
data[0] += i;
270265
});
271266
}
272267
@@ -281,7 +276,7 @@ And... still gives us an error.
281276

282277
```text
283278
<anon>:11:24 error: cannot borrow immutable borrowed content as mutable
284-
<anon>:11 data[i] += 1;
279+
<anon>:11 data[0] += i;
285280
^~~~
286281
```
287282

@@ -317,7 +312,7 @@ fn main() {
317312
let data = data.clone();
318313
thread::spawn(move || {
319314
let mut data = data.lock().unwrap();
320-
data[i] += 1;
315+
data[0] += i;
321316
});
322317
}
323318

@@ -360,7 +355,7 @@ Let's examine the body of the thread more closely:
360355
# let data = data.clone();
361356
thread::spawn(move || {
362357
let mut data = data.lock().unwrap();
363-
data[i] += 1;
358+
data[0] += i;
364359
});
365360
# }
366361
# thread::sleep(Duration::from_millis(50));

trunk/src/libcore/result.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! # #[allow(dead_code)]
2020
//! enum Result<T, E> {
2121
//! Ok(T),
22-
//! Err(E)
22+
//! Err(E),
2323
//! }
2424
//! ```
2525
//!
@@ -39,7 +39,7 @@
3939
//! None => Err("invalid header length"),
4040
//! Some(&1) => Ok(Version::Version1),
4141
//! Some(&2) => Ok(Version::Version2),
42-
//! Some(_) => Err("invalid version")
42+
//! Some(_) => Err("invalid version"),
4343
//! }
4444
//! }
4545
//!
@@ -254,7 +254,7 @@ pub enum Result<T, E> {
254254

255255
/// Contains the error value
256256
#[stable(feature = "rust1", since = "1.0.0")]
257-
Err(#[stable(feature = "rust1", since = "1.0.0")] E)
257+
Err(#[stable(feature = "rust1", since = "1.0.0")] E),
258258
}
259259

260260
/////////////////////////////////////////////////////////////////////////////
@@ -270,6 +270,8 @@ impl<T, E> Result<T, E> {
270270
///
271271
/// # Examples
272272
///
273+
/// Basic usage:
274+
///
273275
/// ```
274276
/// let x: Result<i32, &str> = Ok(-3);
275277
/// assert_eq!(x.is_ok(), true);
@@ -290,6 +292,8 @@ impl<T, E> Result<T, E> {
290292
///
291293
/// # Examples
292294
///
295+
/// Basic usage:
296+
///
293297
/// ```
294298
/// let x: Result<i32, &str> = Ok(-3);
295299
/// assert_eq!(x.is_err(), false);
@@ -314,6 +318,8 @@ impl<T, E> Result<T, E> {
314318
///
315319
/// # Examples
316320
///
321+
/// Basic usage:
322+
///
317323
/// ```
318324
/// let x: Result<u32, &str> = Ok(2);
319325
/// assert_eq!(x.ok(), Some(2));
@@ -337,6 +343,8 @@ impl<T, E> Result<T, E> {
337343
///
338344
/// # Examples
339345
///
346+
/// Basic usage:
347+
///
340348
/// ```
341349
/// let x: Result<u32, &str> = Ok(2);
342350
/// assert_eq!(x.err(), None);
@@ -362,6 +370,10 @@ impl<T, E> Result<T, E> {
362370
/// Produces a new `Result`, containing a reference
363371
/// into the original, leaving the original in place.
364372
///
373+
/// # Examples
374+
///
375+
/// Basic usage:
376+
///
365377
/// ```
366378
/// let x: Result<u32, &str> = Ok(2);
367379
/// assert_eq!(x.as_ref(), Ok(&2));
@@ -380,6 +392,10 @@ impl<T, E> Result<T, E> {
380392

381393
/// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`
382394
///
395+
/// # Examples
396+
///
397+
/// Basic usage:
398+
///
383399
/// ```
384400
/// fn mutate(r: &mut Result<i32, i32>) {
385401
/// match r.as_mut() {
@@ -445,6 +461,8 @@ impl<T, E> Result<T, E> {
445461
///
446462
/// # Examples
447463
///
464+
/// Basic usage:
465+
///
448466
/// ```
449467
/// fn stringify(x: u32) -> String { format!("error code: {}", x) }
450468
///
@@ -471,6 +489,8 @@ impl<T, E> Result<T, E> {
471489
///
472490
/// # Examples
473491
///
492+
/// Basic usage:
493+
///
474494
/// ```
475495
/// let x: Result<u32, &str> = Ok(7);
476496
/// assert_eq!(x.iter().next(), Some(&7));
@@ -488,6 +508,8 @@ impl<T, E> Result<T, E> {
488508
///
489509
/// # Examples
490510
///
511+
/// Basic usage:
512+
///
491513
/// ```
492514
/// let mut x: Result<u32, &str> = Ok(7);
493515
/// match x.iter_mut().next() {
@@ -513,6 +535,8 @@ impl<T, E> Result<T, E> {
513535
///
514536
/// # Examples
515537
///
538+
/// Basic usage:
539+
///
516540
/// ```
517541
/// let x: Result<u32, &str> = Ok(2);
518542
/// let y: Result<&str, &str> = Err("late error");
@@ -545,6 +569,8 @@ impl<T, E> Result<T, E> {
545569
///
546570
/// # Examples
547571
///
572+
/// Basic usage:
573+
///
548574
/// ```
549575
/// fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
550576
/// fn err(x: u32) -> Result<u32, u32> { Err(x) }
@@ -567,6 +593,8 @@ impl<T, E> Result<T, E> {
567593
///
568594
/// # Examples
569595
///
596+
/// Basic usage:
597+
///
570598
/// ```
571599
/// let x: Result<u32, &str> = Ok(2);
572600
/// let y: Result<u32, &str> = Err("late error");
@@ -599,6 +627,8 @@ impl<T, E> Result<T, E> {
599627
///
600628
/// # Examples
601629
///
630+
/// Basic usage:
631+
///
602632
/// ```
603633
/// fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
604634
/// fn err(x: u32) -> Result<u32, u32> { Err(x) }
@@ -622,6 +652,8 @@ impl<T, E> Result<T, E> {
622652
///
623653
/// # Examples
624654
///
655+
/// Basic usage:
656+
///
625657
/// ```
626658
/// let optb = 2;
627659
/// let x: Result<u32, &str> = Ok(9);
@@ -644,6 +676,8 @@ impl<T, E> Result<T, E> {
644676
///
645677
/// # Examples
646678
///
679+
/// Basic usage:
680+
///
647681
/// ```
648682
/// fn count(x: &str) -> usize { x.len() }
649683
///
@@ -670,6 +704,8 @@ impl<T, E: fmt::Debug> Result<T, E> {
670704
///
671705
/// # Examples
672706
///
707+
/// Basic usage:
708+
///
673709
/// ```
674710
/// let x: Result<u32, &str> = Ok(2);
675711
/// assert_eq!(x.unwrap(), 2);
@@ -696,6 +732,9 @@ impl<T, E: fmt::Debug> Result<T, E> {
696732
/// passed message, and the content of the `Err`.
697733
///
698734
/// # Examples
735+
///
736+
/// Basic usage:
737+
///
699738
/// ```{.should_panic}
700739
/// let x: Result<u32, &str> = Err("emergency failure");
701740
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
@@ -759,6 +798,8 @@ impl<T, E> IntoIterator for Result<T, E> {
759798
///
760799
/// # Examples
761800
///
801+
/// Basic usage:
802+
///
762803
/// ```
763804
/// let x: Result<u32, &str> = Ok(5);
764805
/// let v: Vec<u32> = x.into_iter().collect();

trunk/src/libcore/slice.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,20 @@ macro_rules! make_mut_slice {
871871
}
872872

873873
/// Immutable slice iterator
874+
///
875+
/// # Examples
876+
///
877+
/// Basic usage:
878+
///
879+
/// ```
880+
/// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]):
881+
/// let slice = &[1, 2, 3];
882+
///
883+
/// // Then, we iterate over it:
884+
/// for element in slice.iter() {
885+
/// println!("{}", element);
886+
/// }
887+
/// ```
874888
#[stable(feature = "rust1", since = "1.0.0")]
875889
pub struct Iter<'a, T: 'a> {
876890
ptr: *const T,
@@ -897,6 +911,26 @@ impl<'a, T> Iter<'a, T> {
897911
///
898912
/// This has the same lifetime as the original slice, and so the
899913
/// iterator can continue to be used while this exists.
914+
///
915+
/// # Examples
916+
///
917+
/// Basic usage:
918+
///
919+
/// ```
920+
/// // First, we declare a type which has the `iter` method to get the `Iter`
921+
/// // struct (&[usize here]):
922+
/// let slice = &[1, 2, 3];
923+
///
924+
/// // Then, we get the iterator:
925+
/// let mut iter = slice.iter();
926+
/// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]":
927+
/// println!("{:?}", iter.as_slice());
928+
///
929+
/// // Next, we move to the second element of the slice:
930+
/// iter.next();
931+
/// // Now `as_slice` returns "[2, 3]":
932+
/// println!("{:?}", iter.as_slice());
933+
/// ```
900934
#[stable(feature = "iter_to_slice", since = "1.4.0")]
901935
pub fn as_slice(&self) -> &'a [T] {
902936
make_slice!(self.ptr, self.end)
@@ -928,6 +962,24 @@ impl<'a, T> Clone for Iter<'a, T> {
928962
}
929963

930964
/// Mutable slice iterator.
965+
///
966+
/// # Examples
967+
///
968+
/// Basic usage:
969+
///
970+
/// ```
971+
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
972+
/// // struct (&[usize here]):
973+
/// let mut slice = &mut [1, 2, 3];
974+
///
975+
/// // Then, we iterate over it and increment each element value:
976+
/// for element in slice.iter_mut() {
977+
/// *element += 1;
978+
/// }
979+
///
980+
/// // We now have "[2, 3, 4]":
981+
/// println!("{:?}", slice);
982+
/// ```
931983
#[stable(feature = "rust1", since = "1.0.0")]
932984
pub struct IterMut<'a, T: 'a> {
933985
ptr: *mut T,
@@ -956,6 +1008,35 @@ impl<'a, T> IterMut<'a, T> {
9561008
/// to consume the iterator. Consider using the `Slice` and
9571009
/// `SliceMut` implementations for obtaining slices with more
9581010
/// restricted lifetimes that do not consume the iterator.
1011+
///
1012+
/// # Examples
1013+
///
1014+
/// Basic usage:
1015+
///
1016+
/// ```
1017+
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
1018+
/// // struct (&[usize here]):
1019+
/// let mut slice = &mut [1, 2, 3];
1020+
///
1021+
/// {
1022+
/// // Then, we get the iterator:
1023+
/// let mut iter = slice.iter_mut();
1024+
/// // We move to next element:
1025+
/// iter.next();
1026+
/// // So if we print what `into_slice` method returns here, we have "[2, 3]":
1027+
/// println!("{:?}", iter.into_slice());
1028+
/// }
1029+
///
1030+
/// // Now let's modify a value of the slice:
1031+
/// {
1032+
/// // First we get back the iterator:
1033+
/// let mut iter = slice.iter_mut();
1034+
/// // We change the value of the first element of the slice returned by the `next` method:
1035+
/// *iter.next().unwrap() += 1;
1036+
/// }
1037+
/// // Now slice is "[2, 2, 3]":
1038+
/// println!("{:?}", slice);
1039+
/// ```
9591040
#[stable(feature = "iter_to_slice", since = "1.4.0")]
9601041
pub fn into_slice(self) -> &'a mut [T] {
9611042
make_mut_slice!(self.ptr, self.end)

0 commit comments

Comments
 (0)