Skip to content

Commit c2b138f

Browse files
committed
---
yaml --- r: 272478 b: refs/heads/master c: b2db973 h: refs/heads/master
1 parent 78d9eef commit c2b138f

File tree

15 files changed

+39
-285
lines changed

15 files changed

+39
-285
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: 7ba7e02b5ed025d1271d3cae0c475a526510b321
2+
refs/heads/master: b2db97347bc4373deea24eb7b7c6ecffb117fd8c
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: 13 additions & 8 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 would have a data race in many
165+
As an example, here is a Rust program that could 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[0] += i;
177+
data[i] += 1;
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[0] += i;
189+
data[i] += 1;
190190
^~~~
191191
```
192192

@@ -195,6 +195,11 @@ 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+
198203
So, we need some type that lets us have more than one owning reference to a
199204
value. Usually, we'd use `Rc<T>` for this, which is a reference counted type
200205
that provides shared ownership. It has some runtime bookkeeping that keeps track
@@ -218,7 +223,7 @@ fn main() {
218223
219224
// use it in a thread
220225
thread::spawn(move || {
221-
data_ref[0] += i;
226+
data_ref[i] += 1;
222227
});
223228
}
224229
@@ -261,7 +266,7 @@ fn main() {
261266
for i in 0..3 {
262267
let data = data.clone();
263268
thread::spawn(move || {
264-
data[0] += i;
269+
data[i] += 1;
265270
});
266271
}
267272
@@ -276,7 +281,7 @@ And... still gives us an error.
276281

277282
```text
278283
<anon>:11:24 error: cannot borrow immutable borrowed content as mutable
279-
<anon>:11 data[0] += i;
284+
<anon>:11 data[i] += 1;
280285
^~~~
281286
```
282287

@@ -312,7 +317,7 @@ fn main() {
312317
let data = data.clone();
313318
thread::spawn(move || {
314319
let mut data = data.lock().unwrap();
315-
data[0] += i;
320+
data[i] += 1;
316321
});
317322
}
318323

@@ -355,7 +360,7 @@ Let's examine the body of the thread more closely:
355360
# let data = data.clone();
356361
thread::spawn(move || {
357362
let mut data = data.lock().unwrap();
358-
data[0] += i;
363+
data[i] += 1;
359364
});
360365
# }
361366
# thread::sleep(Duration::from_millis(50));

trunk/src/libcore/result.rs

Lines changed: 3 additions & 44 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,8 +270,6 @@ impl<T, E> Result<T, E> {
270270
///
271271
/// # Examples
272272
///
273-
/// Basic usage:
274-
///
275273
/// ```
276274
/// let x: Result<i32, &str> = Ok(-3);
277275
/// assert_eq!(x.is_ok(), true);
@@ -292,8 +290,6 @@ impl<T, E> Result<T, E> {
292290
///
293291
/// # Examples
294292
///
295-
/// Basic usage:
296-
///
297293
/// ```
298294
/// let x: Result<i32, &str> = Ok(-3);
299295
/// assert_eq!(x.is_err(), false);
@@ -318,8 +314,6 @@ impl<T, E> Result<T, E> {
318314
///
319315
/// # Examples
320316
///
321-
/// Basic usage:
322-
///
323317
/// ```
324318
/// let x: Result<u32, &str> = Ok(2);
325319
/// assert_eq!(x.ok(), Some(2));
@@ -343,8 +337,6 @@ impl<T, E> Result<T, E> {
343337
///
344338
/// # Examples
345339
///
346-
/// Basic usage:
347-
///
348340
/// ```
349341
/// let x: Result<u32, &str> = Ok(2);
350342
/// assert_eq!(x.err(), None);
@@ -370,10 +362,6 @@ impl<T, E> Result<T, E> {
370362
/// Produces a new `Result`, containing a reference
371363
/// into the original, leaving the original in place.
372364
///
373-
/// # Examples
374-
///
375-
/// Basic usage:
376-
///
377365
/// ```
378366
/// let x: Result<u32, &str> = Ok(2);
379367
/// assert_eq!(x.as_ref(), Ok(&2));
@@ -392,10 +380,6 @@ impl<T, E> Result<T, E> {
392380

393381
/// Converts from `Result<T, E>` to `Result<&mut T, &mut E>`
394382
///
395-
/// # Examples
396-
///
397-
/// Basic usage:
398-
///
399383
/// ```
400384
/// fn mutate(r: &mut Result<i32, i32>) {
401385
/// match r.as_mut() {
@@ -461,8 +445,6 @@ impl<T, E> Result<T, E> {
461445
///
462446
/// # Examples
463447
///
464-
/// Basic usage:
465-
///
466448
/// ```
467449
/// fn stringify(x: u32) -> String { format!("error code: {}", x) }
468450
///
@@ -489,8 +471,6 @@ impl<T, E> Result<T, E> {
489471
///
490472
/// # Examples
491473
///
492-
/// Basic usage:
493-
///
494474
/// ```
495475
/// let x: Result<u32, &str> = Ok(7);
496476
/// assert_eq!(x.iter().next(), Some(&7));
@@ -508,8 +488,6 @@ impl<T, E> Result<T, E> {
508488
///
509489
/// # Examples
510490
///
511-
/// Basic usage:
512-
///
513491
/// ```
514492
/// let mut x: Result<u32, &str> = Ok(7);
515493
/// match x.iter_mut().next() {
@@ -535,8 +513,6 @@ impl<T, E> Result<T, E> {
535513
///
536514
/// # Examples
537515
///
538-
/// Basic usage:
539-
///
540516
/// ```
541517
/// let x: Result<u32, &str> = Ok(2);
542518
/// let y: Result<&str, &str> = Err("late error");
@@ -569,8 +545,6 @@ impl<T, E> Result<T, E> {
569545
///
570546
/// # Examples
571547
///
572-
/// Basic usage:
573-
///
574548
/// ```
575549
/// fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
576550
/// fn err(x: u32) -> Result<u32, u32> { Err(x) }
@@ -593,8 +567,6 @@ impl<T, E> Result<T, E> {
593567
///
594568
/// # Examples
595569
///
596-
/// Basic usage:
597-
///
598570
/// ```
599571
/// let x: Result<u32, &str> = Ok(2);
600572
/// let y: Result<u32, &str> = Err("late error");
@@ -627,8 +599,6 @@ impl<T, E> Result<T, E> {
627599
///
628600
/// # Examples
629601
///
630-
/// Basic usage:
631-
///
632602
/// ```
633603
/// fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
634604
/// fn err(x: u32) -> Result<u32, u32> { Err(x) }
@@ -652,8 +622,6 @@ impl<T, E> Result<T, E> {
652622
///
653623
/// # Examples
654624
///
655-
/// Basic usage:
656-
///
657625
/// ```
658626
/// let optb = 2;
659627
/// let x: Result<u32, &str> = Ok(9);
@@ -676,8 +644,6 @@ impl<T, E> Result<T, E> {
676644
///
677645
/// # Examples
678646
///
679-
/// Basic usage:
680-
///
681647
/// ```
682648
/// fn count(x: &str) -> usize { x.len() }
683649
///
@@ -704,8 +670,6 @@ impl<T, E: fmt::Debug> Result<T, E> {
704670
///
705671
/// # Examples
706672
///
707-
/// Basic usage:
708-
///
709673
/// ```
710674
/// let x: Result<u32, &str> = Ok(2);
711675
/// assert_eq!(x.unwrap(), 2);
@@ -732,9 +696,6 @@ impl<T, E: fmt::Debug> Result<T, E> {
732696
/// passed message, and the content of the `Err`.
733697
///
734698
/// # Examples
735-
///
736-
/// Basic usage:
737-
///
738699
/// ```{.should_panic}
739700
/// let x: Result<u32, &str> = Err("emergency failure");
740701
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
@@ -798,8 +759,6 @@ impl<T, E> IntoIterator for Result<T, E> {
798759
///
799760
/// # Examples
800761
///
801-
/// Basic usage:
802-
///
803762
/// ```
804763
/// let x: Result<u32, &str> = Ok(5);
805764
/// let v: Vec<u32> = x.into_iter().collect();

trunk/src/libcore/slice.rs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -871,20 +871,6 @@ 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-
/// ```
888874
#[stable(feature = "rust1", since = "1.0.0")]
889875
pub struct Iter<'a, T: 'a> {
890876
ptr: *const T,
@@ -911,26 +897,6 @@ impl<'a, T> Iter<'a, T> {
911897
///
912898
/// This has the same lifetime as the original slice, and so the
913899
/// 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-
/// ```
934900
#[stable(feature = "iter_to_slice", since = "1.4.0")]
935901
pub fn as_slice(&self) -> &'a [T] {
936902
make_slice!(self.ptr, self.end)
@@ -962,24 +928,6 @@ impl<'a, T> Clone for Iter<'a, T> {
962928
}
963929

964930
/// 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-
/// ```
983931
#[stable(feature = "rust1", since = "1.0.0")]
984932
pub struct IterMut<'a, T: 'a> {
985933
ptr: *mut T,
@@ -1008,35 +956,6 @@ impl<'a, T> IterMut<'a, T> {
1008956
/// to consume the iterator. Consider using the `Slice` and
1009957
/// `SliceMut` implementations for obtaining slices with more
1010958
/// 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-
/// ```
1040959
#[stable(feature = "iter_to_slice", since = "1.4.0")]
1041960
pub fn into_slice(self) -> &'a mut [T] {
1042961
make_mut_slice!(self.ptr, self.end)

0 commit comments

Comments
 (0)