Skip to content

Commit c628197

Browse files
committed
---
yaml --- r: 216541 b: refs/heads/stable c: ce1150b h: refs/heads/master i: 216539: ce0a04e v: v3
1 parent 40c7f68 commit c628197

File tree

49 files changed

+849
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+849
-408
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ refs/heads/tmp: 378a370ff2057afeb1eae86eb6e78c476866a4a6
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: a5286998df566e736b32f6795bfc3803bdaf453d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: e32dab78b8a6deaf973de6c23c509780533e8fd7
32+
refs/heads/stable: ce1150b9f02b6433955e9998f5a4099127edc362
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375

branches/stable/src/compiletest/compiletest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
269269
run_ignored: config.run_ignored,
270270
logfile: config.logfile.clone(),
271271
run_tests: true,
272-
run_benchmarks: true,
272+
bench_benchmarks: true,
273273
nocapture: env::var("RUST_TEST_NOCAPTURE").is_ok(),
274274
color: test::AutoColor,
275275
}

branches/stable/src/doc/trpl/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,5 @@ fn main() {
190190
We created an inner scope with an additional set of curly braces. `y` will go out of
191191
scope before we call `push()`, and so we’re all good.
192192

193-
This concept of ownership isn’t just good for preventing danging pointers, but an
193+
This concept of ownership isn’t just good for preventing dangling pointers, but an
194194
entire set of related problems, like iterator invalidation, concurrency, and more.

branches/stable/src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@
6464
* [Benchmark Tests](benchmark-tests.md)
6565
* [Box Syntax and Patterns](box-syntax-and-patterns.md)
6666
* [Slice Patterns](slice-patterns.md)
67+
* [Associated Constants](associated-constants.md)
6768
* [Glossary](glossary.md)
6869
* [Academic Research](academic-research.md)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
% Associated Constants
2+
3+
With the `associated_consts` feature, you can define constants like this:
4+
5+
```rust
6+
#![feature(associated_consts)]
7+
8+
trait Foo {
9+
const ID: i32;
10+
}
11+
12+
impl Foo for i32 {
13+
const ID: i32 = 1;
14+
}
15+
16+
fn main() {
17+
assert_eq!(1, i32::ID);
18+
}
19+
```
20+
21+
Any implementor of `Foo` will have to define `ID`. Without the definition:
22+
23+
```rust,ignore
24+
#![feature(associated_consts)]
25+
26+
trait Foo {
27+
const ID: i32;
28+
}
29+
30+
impl Foo for i32 {
31+
}
32+
```
33+
34+
gives
35+
36+
```text
37+
error: not all trait items implemented, missing: `ID` [E0046]
38+
impl Foo for i32 {
39+
}
40+
```
41+
42+
A default value can be implemented as well:
43+
44+
```rust
45+
#![feature(associated_consts)]
46+
47+
trait Foo {
48+
const ID: i32 = 1;
49+
}
50+
51+
impl Foo for i32 {
52+
}
53+
54+
impl Foo for i64 {
55+
const ID: i32 = 5;
56+
}
57+
58+
fn main() {
59+
assert_eq!(1, i32::ID);
60+
assert_eq!(5, i64::ID);
61+
}
62+
```
63+
64+
As you can see, when implementing `Foo`, you can leave it unimplemented, as
65+
with `i32`. It will then use the default value. But, as in `i64`, we can also
66+
add our own definition.
67+
68+
Associated constants don’t have to be associated with a trait. An `impl` block
69+
for a `struct` works fine too:
70+
71+
```rust
72+
#![feature(associated_consts)]
73+
74+
struct Foo;
75+
76+
impl Foo {
77+
pub const FOO: u32 = 3;
78+
}
79+
```

branches/stable/src/doc/trpl/concurrency.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ use std::thread;
116116
fn main() {
117117
let mut data = vec![1u32, 2, 3];
118118
119-
for i in 0..2 {
119+
for i in 0..3 {
120120
thread::spawn(move || {
121121
data[i] += 1;
122122
});
@@ -154,7 +154,7 @@ use std::sync::Mutex;
154154
fn main() {
155155
let mut data = Mutex::new(vec![1u32, 2, 3]);
156156
157-
for i in 0..2 {
157+
for i in 0..3 {
158158
let data = data.lock().unwrap();
159159
thread::spawn(move || {
160160
data[i] += 1;
@@ -196,7 +196,7 @@ use std::thread;
196196
fn main() {
197197
let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
198198
199-
for i in 0..2 {
199+
for i in 0..3 {
200200
let data = data.clone();
201201
thread::spawn(move || {
202202
let mut data = data.lock().unwrap();
@@ -217,7 +217,7 @@ thread more closely:
217217
# use std::thread;
218218
# fn main() {
219219
# let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
220-
# for i in 0..2 {
220+
# for i in 0..3 {
221221
# let data = data.clone();
222222
thread::spawn(move || {
223223
let mut data = data.lock().unwrap();

branches/stable/src/doc/trpl/patterns.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ This prints `something else`
7070

7171
# Bindings
7272

73-
If you’re matching multiple things, via a `|` or a `...`, you can bind
74-
the value to a name with `@`:
73+
You can bind values to names with `@`:
7574

7675
```rust
7776
let x = 1;
@@ -82,7 +81,36 @@ match x {
8281
}
8382
```
8483

85-
This prints `got a range element 1`.
84+
This prints `got a range element 1`. This is useful when you want to
85+
do a complicated match of part of a data structure:
86+
87+
```rust
88+
#[derive(Debug)]
89+
struct Person {
90+
name: Option<String>,
91+
}
92+
93+
let name = "Steve".to_string();
94+
let mut x: Option<Person> = Some(Person { name: Some(name) });
95+
match x {
96+
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
97+
_ => {}
98+
}
99+
```
100+
101+
This prints `Some("Steve")`: We’ve bound the inner `name` to `a`.
102+
103+
If you use `@` with `|`, you need to make sure the name is bound in each part
104+
of the pattern:
105+
106+
```rust
107+
let x = 5;
108+
109+
match x {
110+
e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),
111+
_ => println!("anything"),
112+
}
113+
```
86114

87115
# Ignoring variants
88116

branches/stable/src/libcollections/slice.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,7 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10041004
/// # Examples
10051005
///
10061006
/// ```
1007-
/// let v = vec!["hello", "world"];
1008-
///
1009-
/// let s: String = v.concat();
1010-
///
1011-
/// println!("{}", s); // prints "helloworld"
1007+
/// assert_eq!(["hello", "world"].concat(), "helloworld");
10121008
/// ```
10131009
#[stable(feature = "rust1", since = "1.0.0")]
10141010
fn concat(&self) -> U;
@@ -1018,11 +1014,7 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10181014
/// # Examples
10191015
///
10201016
/// ```
1021-
/// let v = vec!["hello", "world"];
1022-
///
1023-
/// let s: String = v.connect(" ");
1024-
///
1025-
/// println!("{}", s); // prints "hello world"
1017+
/// assert_eq!(["hello", "world"].connect(" "), "hello world");
10261018
/// ```
10271019
#[stable(feature = "rust1", since = "1.0.0")]
10281020
fn connect(&self, sep: &T) -> U;

branches/stable/src/libcollections/string.rs

Lines changed: 119 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use rustc_unicode::str as unicode_str;
2626
use rustc_unicode::str::Utf16Item;
2727

2828
use borrow::{Cow, IntoCow};
29-
use str::{self, FromStr, Utf8Error};
29+
use range::RangeArgument;
30+
use str::{self, FromStr, Utf8Error, Chars};
3031
use vec::{DerefVec, Vec, as_vec};
3132

3233
/// A growable string stored as a UTF-8 encoded buffer.
@@ -695,6 +696,59 @@ impl String {
695696
pub fn clear(&mut self) {
696697
self.vec.clear()
697698
}
699+
700+
/// Create a draining iterator that removes the specified range in the string
701+
/// and yields the removed chars from start to end. The element range is
702+
/// removed even if the iterator is not consumed until the end.
703+
///
704+
/// # Panics
705+
///
706+
/// Panics if the starting point or end point are not on character boundaries,
707+
/// or if they are out of bounds.
708+
///
709+
/// # Examples
710+
///
711+
/// ```
712+
/// # #![feature(collections_drain)]
713+
///
714+
/// let mut s = String::from("α is alpha, β is beta");
715+
/// let beta_offset = s.find('β').unwrap_or(s.len());
716+
///
717+
/// // Remove the range up until the β from the string
718+
/// let t: String = s.drain(..beta_offset).collect();
719+
/// assert_eq!(t, "α is alpha, ");
720+
/// assert_eq!(s, "β is beta");
721+
///
722+
/// // A full range clears the string
723+
/// s.drain(..);
724+
/// assert_eq!(s, "");
725+
/// ```
726+
#[unstable(feature = "collections_drain",
727+
reason = "recently added, matches RFC")]
728+
pub fn drain<R>(&mut self, range: R) -> Drain where R: RangeArgument<usize> {
729+
// Memory safety
730+
//
731+
// The String version of Drain does not have the memory safety issues
732+
// of the vector version. The data is just plain bytes.
733+
// Because the range removal happens in Drop, if the Drain iterator is leaked,
734+
// the removal will not happen.
735+
let len = self.len();
736+
let start = *range.start().unwrap_or(&0);
737+
let end = *range.end().unwrap_or(&len);
738+
739+
// Take out two simultaneous borrows. The &mut String won't be accessed
740+
// until iteration is over, in Drop.
741+
let self_ptr = self as *mut _;
742+
// slicing does the appropriate bounds checks
743+
let chars_iter = self[start..end].chars();
744+
745+
Drain {
746+
start: start,
747+
end: end,
748+
iter: chars_iter,
749+
string: self_ptr,
750+
}
751+
}
698752
}
699753

700754
impl FromUtf8Error {
@@ -740,8 +794,7 @@ impl<'a> FromIterator<&'a str> for String {
740794
}
741795
}
742796

743-
#[unstable(feature = "collections",
744-
reason = "waiting on Extend stabilization")]
797+
#[stable(feature = "rust1", since = "1.0.0")]
745798
impl Extend<char> for String {
746799
fn extend<I: IntoIterator<Item=char>>(&mut self, iterable: I) {
747800
let iterator = iterable.into_iter();
@@ -753,8 +806,7 @@ impl Extend<char> for String {
753806
}
754807
}
755808

756-
#[unstable(feature = "collections",
757-
reason = "waiting on Extend stabilization")]
809+
#[stable(feature = "rust1", since = "1.0.0")]
758810
impl<'a> Extend<&'a str> for String {
759811
fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
760812
let iterator = iterable.into_iter();
@@ -869,8 +921,7 @@ impl hash::Hash for String {
869921
}
870922
}
871923

872-
#[unstable(feature = "collections",
873-
reason = "recent addition, needs more experience")]
924+
#[stable(feature = "rust1", since = "1.0.0")]
874925
impl<'a> Add<&'a str> for String {
875926
type Output = String;
876927

@@ -964,11 +1015,17 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
9641015
DerefString { x: as_vec(x.as_bytes()) }
9651016
}
9661017

967-
#[unstable(feature = "collections", reason = "associated error type may change")]
1018+
/// Error returned from `String::from_str`
1019+
#[unstable(feature = "str_parse_error", reason = "may want to be replaced with \
1020+
Void if it ever exists")]
1021+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1022+
pub struct ParseError(());
1023+
1024+
#[stable(feature = "rust1", since = "1.0.0")]
9681025
impl FromStr for String {
969-
type Err = ();
1026+
type Err = ParseError;
9701027
#[inline]
971-
fn from_str(s: &str) -> Result<String, ()> {
1028+
fn from_str(s: &str) -> Result<String, ParseError> {
9721029
Ok(String::from_str(s))
9731030
}
9741031
}
@@ -1072,3 +1129,55 @@ impl fmt::Write for String {
10721129
Ok(())
10731130
}
10741131
}
1132+
1133+
/// A draining iterator for `String`.
1134+
#[unstable(feature = "collections_drain", reason = "recently added")]
1135+
pub struct Drain<'a> {
1136+
/// Will be used as &'a mut String in the destructor
1137+
string: *mut String,
1138+
/// Start of part to remove
1139+
start: usize,
1140+
/// End of part to remove
1141+
end: usize,
1142+
/// Current remaining range to remove
1143+
iter: Chars<'a>,
1144+
}
1145+
1146+
unsafe impl<'a> Sync for Drain<'a> {}
1147+
unsafe impl<'a> Send for Drain<'a> {}
1148+
1149+
#[unstable(feature = "collections_drain", reason = "recently added")]
1150+
impl<'a> Drop for Drain<'a> {
1151+
fn drop(&mut self) {
1152+
unsafe {
1153+
// Use Vec::drain. "Reaffirm" the bounds checks to avoid
1154+
// panic code being inserted again.
1155+
let self_vec = (*self.string).as_mut_vec();
1156+
if self.start <= self.end && self.end <= self_vec.len() {
1157+
self_vec.drain(self.start..self.end);
1158+
}
1159+
}
1160+
}
1161+
}
1162+
1163+
#[unstable(feature = "collections_drain", reason = "recently added")]
1164+
impl<'a> Iterator for Drain<'a> {
1165+
type Item = char;
1166+
1167+
#[inline]
1168+
fn next(&mut self) -> Option<char> {
1169+
self.iter.next()
1170+
}
1171+
1172+
fn size_hint(&self) -> (usize, Option<usize>) {
1173+
self.iter.size_hint()
1174+
}
1175+
}
1176+
1177+
#[unstable(feature = "collections_drain", reason = "recently added")]
1178+
impl<'a> DoubleEndedIterator for Drain<'a> {
1179+
#[inline]
1180+
fn next_back(&mut self) -> Option<char> {
1181+
self.iter.next_back()
1182+
}
1183+
}

0 commit comments

Comments
 (0)