Skip to content

Commit ee4bc2e

Browse files
committed
---
yaml --- r: 126623 b: refs/heads/snap-stage3 c: 50481f5 h: refs/heads/master i: 126621: 742f4e0 126619: 1dec331 126615: f00e6ce 126607: 6f1fd0c 126591: a095654 v: v3
1 parent 3b0c4c1 commit ee4bc2e

File tree

188 files changed

+3738
-1437
lines changed

Some content is hidden

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

188 files changed

+3738
-1437
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 7be8f0af0393dcdb077c2f6b1653836fd3fba235
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: d368ffdb26144fd1f451d3d8ba4344b0a8e82f99
4+
refs/heads/snap-stage3: 50481f55030f02543e1b3b6ae008d77b1cef3e98
55
refs/heads/try: 502e4c045236682e9728539dc0d2b3d0b237f55c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/mk/docs.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ DOCS := index intro tutorial guide guide-ffi guide-macros guide-lifetimes \
3030
guide-tasks guide-container guide-pointers guide-testing \
3131
guide-runtime complement-bugreport \
3232
complement-lang-faq complement-design-faq complement-project-faq rust \
33-
rustdoc guide-unsafe
33+
rustdoc guide-unsafe guide-strings
3434

3535
PDF_DOCS := tutorial rust
3636

branches/snap-stage3/src/doc/guide-strings.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
% The Strings Guide
22

3-
# Strings
4-
53
Strings are an important concept to master in any programming language. If you
64
come from a managed language background, you may be surprised at the complexity
75
of string handling in a systems programming language. Efficient access and
@@ -14,7 +12,7 @@ Additionally, strings are not null-terminated and can contain null bytes.
1412

1513
Rust has two main types of strings: `&str` and `String`.
1614

17-
## &str
15+
# &str
1816

1917
The first kind is a `&str`. This is pronounced a 'string slice.' String literals
2018
are of the type `&str`:
@@ -38,7 +36,7 @@ Like vector slices, string slices are simply a pointer plus a length. This
3836
means that they're a 'view' into an already-allocated string, such as a
3937
`&'static str` or a `String`.
4038

41-
## String
39+
# String
4240

4341
A `String` is a heap-allocated string. This string is growable, and is also
4442
guaranteed to be UTF-8.
@@ -73,9 +71,9 @@ let x: &[u8] = &[b'a', b'b'];
7371
let stack_str: &str = str::from_utf8(x).unwrap();
7472
```
7573

76-
## Best Practices
74+
# Best Practices
7775

78-
### `String` vs. `&str`
76+
## `String` vs. `&str`
7977

8078
In general, you should prefer `String` when you need ownership, and `&str` when
8179
you just need to borrow a string. This is very similar to using `Vec<T>` vs. `&[T]`,
@@ -98,7 +96,7 @@ need, and it can make your lifetimes more complex. Furthermore, you can pass
9896
either kind of string into `foo` by using `.as_slice()` on any `String` you
9997
need to pass in, so the `&str` version is more flexible.
10098

101-
### Comparisons
99+
## Comparisons
102100

103101
To compare a String to a constant string, prefer `as_slice()`...
104102

@@ -123,7 +121,7 @@ fn compare(string: String) {
123121
Converting a `String` to a `&str` is cheap, but converting the `&str` to a
124122
`String` involves an allocation.
125123

126-
## Other Documentation
124+
# Other Documentation
127125

128126
* [the `&str` API documentation](/std/str/index.html)
129127
* [the `String` API documentation](std/string/index.html)

branches/snap-stage3/src/doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ li {list-style-type: none; }
1313

1414
# Guides
1515

16+
* [Strings](guide-strings.html)
1617
* [Pointers](guide-pointers.html)
1718
* [References and Lifetimes](guide-lifetimes.html)
1819
* [Containers and Iterators](guide-container.html)

branches/snap-stage3/src/doc/tutorial.md

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ When an enum has simple integer discriminators, you can apply the `as` cast
716716
operator to convert a variant to its discriminator value as an `int`:
717717

718718
~~~~
719-
# #[deriving(Show)] enum Direction { North }
720-
println!( "{} => {}", North, North as int );
719+
# enum Direction { North, East, South, West }
720+
println!( "North => {}", North as int );
721721
~~~~
722722

723723
It is possible to set the discriminator values to chosen constant values:
@@ -748,15 +748,23 @@ includes an identifier of the actual form that it holds, much like the
748748

749749
This declaration defines a type `Shape` that can refer to such shapes, and two
750750
functions, `Circle` and `Rectangle`, which can be used to construct values of
751-
the type. To create a new Circle, write `Circle(Point { x: 0.0, y: 0.0 },
752-
10.0)`.
751+
the type.
752+
753+
To create a new `Circle`, write:
754+
755+
~~~~
756+
# struct Point { x: f64, y: f64 }
757+
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
758+
let circle = Circle(Point { x: 0.0, y: 0.0 }, 10.0);
759+
~~~~
753760

754761
All of these variant constructors may be used as patterns. The only way to
755762
access the contents of an enum instance is the destructuring of a match. For
756763
example:
757764

758765
~~~~
759766
use std::f64;
767+
760768
# struct Point {x: f64, y: f64}
761769
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
762770
fn area(sh: Shape) -> f64 {
@@ -765,6 +773,9 @@ fn area(sh: Shape) -> f64 {
765773
Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
766774
}
767775
}
776+
777+
let rect = Rectangle(Point { x: 0.0, y: 0.0 }, Point { x: 2.0, y: 2.0 });
778+
println!("area: {}", area(rect));
768779
~~~~
769780

770781
Use a lone `_` to ignore an individual field. Ignore all fields of a variant
@@ -786,8 +797,9 @@ fn point_from_direction(dir: Direction) -> Point {
786797
Enum variants may also be structs. For example:
787798

788799
~~~~
789-
# #![feature(struct_variant)]
800+
#![feature(struct_variant)]
790801
use std::f64;
802+
791803
# struct Point { x: f64, y: f64 }
792804
# fn square(x: f64) -> f64 { x * x }
793805
enum Shape {
@@ -802,7 +814,14 @@ fn area(sh: Shape) -> f64 {
802814
}
803815
}
804816
}
805-
# fn main() {}
817+
818+
fn main() {
819+
let rect = Rectangle {
820+
top_left: Point { x: 0.0, y: 0.0 },
821+
bottom_right: Point { x: 2.0, y: -2.0 }
822+
};
823+
println!("area: {}", area(rect));
824+
}
806825
~~~~
807826

808827
> *Note:* This feature of the compiler is currently gated behind the
@@ -986,6 +1005,10 @@ that are `Send`, but non-`Send` types can still *contain* types with custom
9861005
destructors. Example of types which are not `Send` are [`Gc<T>`][gc] and
9871006
[`Rc<T>`][rc], the shared-ownership types.
9881007

1008+
> *Note:* See a [later chapter](#ownership-escape-hatches) for a discussion about
1009+
> [`Gc<T>`][gc] and [`Rc<T>`][rc], and the [chapter about traits](#traits) for
1010+
> a discussion about `Send`.
1011+
9891012
[gc]: http://doc.rust-lang.org/std/gc/struct.Gc.html
9901013
[rc]: http://doc.rust-lang.org/std/rc/struct.Rc.html
9911014

@@ -1645,6 +1668,13 @@ let string = "foobar";
16451668
let view: &str = string.slice(0, 3);
16461669
~~~
16471670

1671+
Square brackets denote indexing into a slice or fixed-size vector:
1672+
1673+
~~~~
1674+
let crayons: [&str, ..3] = ["BananaMania", "Beaver", "Bittersweet"];
1675+
println!("Crayon 2 is '{}'", crayons[2]);
1676+
~~~~
1677+
16481678
Mutable slices also exist, just as there are mutable references. However, there
16491679
are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
16501680
Unicode code points, so they cannot be freely mutated without the ability to
@@ -1659,20 +1689,6 @@ view[0] = 5;
16591689
let ys: &mut [int] = &mut [1i, 2i, 3i];
16601690
~~~
16611691

1662-
Square brackets denote indexing into a slice or fixed-size vector:
1663-
1664-
~~~~
1665-
# enum Crayon { Almond, AntiqueBrass, Apricot,
1666-
# Aquamarine, Asparagus, AtomicTangerine,
1667-
# BananaMania, Beaver, Bittersweet };
1668-
# fn draw_scene(c: Crayon) { }
1669-
let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1670-
match crayons[0] {
1671-
Bittersweet => draw_scene(crayons[0]),
1672-
_ => ()
1673-
}
1674-
~~~~
1675-
16761692
A slice or fixed-size vector can be destructured using pattern matching:
16771693

16781694
~~~~
@@ -1738,6 +1754,8 @@ via dynamic checks and can fail at runtime.
17381754
The `Rc` and `Gc` types are not sendable, so they cannot be used to share memory between tasks. Safe
17391755
immutable and mutable shared memory is provided by the `sync::arc` module.
17401756

1757+
> *Note:* See a [later chapter](#traits) for a discussion about `Send` and sendable types.
1758+
17411759
# Closures
17421760

17431761
Named functions, like those we've seen so far, may not refer to local
@@ -2609,7 +2627,7 @@ let nonsense = mycircle.radius() * mycircle.area();
26092627
26102628
## Deriving implementations for traits
26112629

2612-
A small number of traits in `std` and `extra` can have implementations
2630+
A small number of traits in can have implementations
26132631
that can be automatically derived. These instances are specified by
26142632
placing the `deriving` attribute on a data type declaration. For
26152633
example, the following will mean that `Circle` has an implementation
@@ -2618,6 +2636,7 @@ of type `ABC` can be randomly generated and converted to a string:
26182636

26192637
~~~
26202638
extern crate rand;
2639+
use std::rand::{task_rng, Rng};
26212640
26222641
#[deriving(PartialEq)]
26232642
struct Circle { radius: f64 }
@@ -2628,6 +2647,13 @@ enum ABC { A, B, C }
26282647
fn main() {
26292648
// Use the Show trait to print "A, B, C."
26302649
println!("{}, {}, {}", A, B, C);
2650+
2651+
let mut rng = task_rng();
2652+
2653+
// Use the Rand trait to generate a random variants.
2654+
for _ in range(0i, 10) {
2655+
println!("{}", rng.gen::<ABC>());
2656+
}
26312657
}
26322658
~~~
26332659

@@ -3136,8 +3162,8 @@ In Rust terminology, we need a way to refer to other crates.
31363162
For that, Rust offers you the `extern crate` declaration:
31373163

31383164
~~~
3165+
// `num` ships with Rust.
31393166
extern crate num;
3140-
// `num` ships with Rust (much like `extra`; more details further down).
31413167
31423168
fn main() {
31433169
// The rational number '1/2':

branches/snap-stage3/src/libcollections/dlist.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ impl<T> Rawlink<T> {
8787
}
8888

8989
/// Convert the `Rawlink` into an Option value
90-
fn resolve_immut(&self) -> Option<&T> {
91-
unsafe { self.p.to_option() }
90+
fn resolve_immut<'a>(&self) -> Option<&'a T> {
91+
unsafe {
92+
mem::transmute(self.p.to_option())
93+
}
9294
}
9395

9496
/// Convert the `Rawlink` into an Option value
95-
fn resolve(&mut self) -> Option<&mut T> {
97+
fn resolve<'a>(&mut self) -> Option<&'a mut T> {
9698
if self.p.is_null() {
9799
None
98100
} else {
@@ -278,6 +280,23 @@ impl<T> DList<T> {
278280
/// Move the last element to the front of the list.
279281
///
280282
/// If the list is empty, do nothing.
283+
///
284+
/// # Example
285+
///
286+
/// ```rust
287+
/// use std::collections::{DList, Deque};
288+
///
289+
/// let mut dl = DList::new();
290+
/// dl.push_back(1i);
291+
/// dl.push_back(2);
292+
/// dl.push_back(3);
293+
///
294+
/// dl.rotate_forward();
295+
///
296+
/// for e in dl.iter() {
297+
/// println!("{}", e); // prints 3, then 1, then 2
298+
/// }
299+
/// ```
281300
#[inline]
282301
pub fn rotate_forward(&mut self) {
283302
self.pop_back_node().map(|tail| {
@@ -288,6 +307,23 @@ impl<T> DList<T> {
288307
/// Move the first element to the back of the list.
289308
///
290309
/// If the list is empty, do nothing.
310+
///
311+
/// # Example
312+
///
313+
/// ```rust
314+
/// use std::collections::{DList, Deque};
315+
///
316+
/// let mut dl = DList::new();
317+
/// dl.push_back(1i);
318+
/// dl.push_back(2);
319+
/// dl.push_back(3);
320+
///
321+
/// dl.rotate_backward();
322+
///
323+
/// for e in dl.iter() {
324+
/// println!("{}", e); // prints 2, then 3, then 1
325+
/// }
326+
/// ```
291327
#[inline]
292328
pub fn rotate_backward(&mut self) {
293329
self.pop_front_node().map(|head| {
@@ -298,6 +334,25 @@ impl<T> DList<T> {
298334
/// Add all elements from `other` to the end of the list
299335
///
300336
/// O(1)
337+
///
338+
/// # Example
339+
///
340+
/// ```rust
341+
/// use std::collections::{DList, Deque};
342+
///
343+
/// let mut a = DList::new();
344+
/// let mut b = DList::new();
345+
/// a.push_back(1i);
346+
/// a.push_back(2);
347+
/// b.push_back(3i);
348+
/// b.push_back(4);
349+
///
350+
/// a.append(b);
351+
///
352+
/// for e in a.iter() {
353+
/// println!("{}", e); // prints 1, then 2, then 3, then 4
354+
/// }
355+
/// ```
301356
pub fn append(&mut self, mut other: DList<T>) {
302357
match self.list_tail.resolve() {
303358
None => *self = other,
@@ -320,6 +375,25 @@ impl<T> DList<T> {
320375
/// Add all elements from `other` to the beginning of the list
321376
///
322377
/// O(1)
378+
///
379+
/// # Example
380+
///
381+
/// ```rust
382+
/// use std::collections::{DList, Deque};
383+
///
384+
/// let mut a = DList::new();
385+
/// let mut b = DList::new();
386+
/// a.push_back(1i);
387+
/// a.push_back(2);
388+
/// b.push_back(3i);
389+
/// b.push_back(4);
390+
///
391+
/// a.prepend(b);
392+
///
393+
/// for e in a.iter() {
394+
/// println!("{}", e); // prints 3, then 4, then 1, then 2
395+
/// }
396+
/// ```
323397
#[inline]
324398
pub fn prepend(&mut self, mut other: DList<T>) {
325399
mem::swap(self, &mut other);
@@ -330,6 +404,25 @@ impl<T> DList<T> {
330404
/// or at the end.
331405
///
332406
/// O(N)
407+
///
408+
/// # Example
409+
///
410+
/// ```rust
411+
/// use std::collections::{DList, Deque};
412+
///
413+
/// let mut a: DList<int> = DList::new();
414+
/// a.push_back(2i);
415+
/// a.push_back(4);
416+
/// a.push_back(7);
417+
/// a.push_back(8);
418+
///
419+
/// // insert 11 before the first odd number in the list
420+
/// a.insert_when(11, |&e, _| e % 2 == 1);
421+
///
422+
/// for e in a.iter() {
423+
/// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
424+
/// }
425+
/// ```
333426
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
334427
{
335428
let mut it = self.mut_iter();

0 commit comments

Comments
 (0)