Skip to content

Commit b6de370

Browse files
committed
---
yaml --- r: 153564 b: refs/heads/try2 c: 5e0a597 h: refs/heads/master v: v3
1 parent b21bcc8 commit b6de370

File tree

134 files changed

+3638
-1227
lines changed

Some content is hidden

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

134 files changed

+3638
-1227
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 31eb00c022aba208921046cdec046f77a96ec98c
8+
refs/heads/try2: 5e0a597a1a15e36ac7d855ca400e5add9d86716b
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
% The Strings Guide
2+
3+
Strings are an important concept to master in any programming language. If you
4+
come from a managed language background, you may be surprised at the complexity
5+
of string handling in a systems programming language. Efficient access and
6+
allocation of memory for a dynamically sized structure involves a lot of
7+
details. Luckily, Rust has lots of tools to help us here.
8+
9+
A **string** is a sequence of unicode scalar values encoded as a stream of
10+
UTF-8 bytes. All strings are guaranteed to be validly-encoded UTF-8 sequences.
11+
Additionally, strings are not null-terminated and can contain null bytes.
12+
13+
Rust has two main types of strings: `&str` and `String`.
14+
15+
# &str
16+
17+
The first kind is a `&str`. This is pronounced a 'string slice.' String literals
18+
are of the type `&str`:
19+
20+
```{rust}
21+
let string = "Hello there.";
22+
```
23+
24+
Like any Rust type, string slices have an associated lifetime. A string literal
25+
is a `&'static str`. A string slice can be written without an explicit
26+
lifetime in many cases, such as in function arguments. In these cases the
27+
lifetime will be inferred:
28+
29+
```{rust}
30+
fn takes_slice(slice: &str) {
31+
println!("Got: {}", slice);
32+
}
33+
```
34+
35+
Like vector slices, string slices are simply a pointer plus a length. This
36+
means that they're a 'view' into an already-allocated string, such as a
37+
`&'static str` or a `String`.
38+
39+
# String
40+
41+
A `String` is a heap-allocated string. This string is growable, and is also
42+
guaranteed to be UTF-8.
43+
44+
```{rust}
45+
let mut s = "Hello".to_string();
46+
println!("{}", s);
47+
48+
s.push_str(", world.");
49+
println!("{}", s);
50+
```
51+
52+
You can coerce a `String` into a `&str` with the `as_slice()` method:
53+
54+
```{rust}
55+
fn takes_slice(slice: &str) {
56+
println!("Got: {}", slice);
57+
}
58+
59+
fn main() {
60+
let s = "Hello".to_string();
61+
takes_slice(s.as_slice());
62+
}
63+
```
64+
65+
You can also get a `&str` from a stack-allocated array of bytes:
66+
67+
```{rust}
68+
use std::str;
69+
70+
let x: &[u8] = &[b'a', b'b'];
71+
let stack_str: &str = str::from_utf8(x).unwrap();
72+
```
73+
74+
# Best Practices
75+
76+
## `String` vs. `&str`
77+
78+
In general, you should prefer `String` when you need ownership, and `&str` when
79+
you just need to borrow a string. This is very similar to using `Vec<T>` vs. `&[T]`,
80+
and `T` vs `&T` in general.
81+
82+
This means starting off with this:
83+
84+
```{rust,ignore}
85+
fn foo(s: &str) {
86+
```
87+
88+
and only moving to this:
89+
90+
```{rust,ignore}
91+
fn foo(s: String) {
92+
```
93+
94+
If you have good reason. It's not polite to hold on to ownership you don't
95+
need, and it can make your lifetimes more complex. Furthermore, you can pass
96+
either kind of string into `foo` by using `.as_slice()` on any `String` you
97+
need to pass in, so the `&str` version is more flexible.
98+
99+
## Comparisons
100+
101+
To compare a String to a constant string, prefer `as_slice()`...
102+
103+
```{rust}
104+
fn compare(string: String) {
105+
if string.as_slice() == "Hello" {
106+
println!("yes");
107+
}
108+
}
109+
```
110+
111+
... over `to_string()`:
112+
113+
```{rust}
114+
fn compare(string: String) {
115+
if string == "Hello".to_string() {
116+
println!("yes");
117+
}
118+
}
119+
```
120+
121+
Converting a `String` to a `&str` is cheap, but converting the `&str` to a
122+
`String` involves an allocation.
123+
124+
# Other Documentation
125+
126+
* [the `&str` API documentation](/std/str/index.html)
127+
* [the `String` API documentation](std/string/index.html)

branches/try2/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/try2/src/libcollections/btree.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ impl<K: Clone + Ord, V: Clone> Leaf<K, V> {
365365
return (Node::new_leaf(self.clone().elts), false);
366366
}
367367
//If there is an index, insert at that index.
368-
_ => {
369-
if index.unwrap() >= self.elts.len() {
368+
Some(i) => {
369+
if i >= self.elts.len() {
370370
self.elts.push(to_insert.clone());
371371
}
372372
else {
373-
self.elts.insert(index.unwrap(), to_insert.clone());
373+
self.elts.insert(i, to_insert.clone());
374374
}
375375
}
376376
}
@@ -526,16 +526,16 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
526526
self.clone().rightmost_child),
527527
outcome);
528528
}
529-
_ => {
530-
if index.unwrap() == self.elts.len() {
529+
Some(i) => {
530+
if i == self.elts.len() {
531531
let new_outcome = self.clone().rightmost_child.insert(k.clone(),
532532
v.clone(),
533533
ub.clone());
534534
new_branch = new_outcome.clone().val0();
535535
outcome = new_outcome.val1();
536536
}
537537
else {
538-
let new_outcome = self.elts.get(index.unwrap()).left.clone().insert(k.clone(),
538+
let new_outcome = self.elts.get(i).left.clone().insert(k.clone(),
539539
v.clone(),
540540
ub.clone());
541541
new_branch = new_outcome.clone().val0();
@@ -547,11 +547,11 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
547547
//If we have a leaf, we do not need to resize the tree,
548548
//so we can return false.
549549
LeafNode(..) => {
550-
if index.unwrap() == self.elts.len() {
550+
if i == self.elts.len() {
551551
self.rightmost_child = box new_branch.clone();
552552
}
553553
else {
554-
self.elts.get_mut(index.unwrap()).left = box new_branch.clone();
554+
self.elts.get_mut(i).left = box new_branch.clone();
555555
}
556556
return (Node::new_branch(self.clone().elts,
557557
self.clone().rightmost_child),
@@ -589,13 +589,13 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
589589
self.clone().rightmost_child),
590590
false);
591591
}
592-
_ => {
593-
self.elts.insert(new_elt_index.unwrap(), new_elt);
594-
if new_elt_index.unwrap() + 1 >= self.elts.len() {
592+
Some(i) => {
593+
self.elts.insert(i, new_elt);
594+
if i + 1 >= self.elts.len() {
595595
self.rightmost_child = branch.clone().rightmost_child;
596596
}
597597
else {
598-
self.elts.get_mut(new_elt_index.unwrap() + 1).left =
598+
self.elts.get_mut(i + 1).left =
599599
branch.clone().rightmost_child;
600600
}
601601
}

branches/try2/src/libcollections/dlist.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ impl<T> DList<T> {
278278
/// Move the last element to the front of the list.
279279
///
280280
/// If the list is empty, do nothing.
281+
///
282+
/// # Example
283+
///
284+
/// ```rust
285+
/// use std::collections::{DList, Deque};
286+
///
287+
/// let mut dl = DList::new();
288+
/// dl.push_back(1i);
289+
/// dl.push_back(2);
290+
/// dl.push_back(3);
291+
///
292+
/// dl.rotate_forward();
293+
///
294+
/// for e in dl.iter() {
295+
/// println!("{}", e); // prints 3, then 1, then 2
296+
/// }
297+
/// ```
281298
#[inline]
282299
pub fn rotate_forward(&mut self) {
283300
self.pop_back_node().map(|tail| {
@@ -288,6 +305,23 @@ impl<T> DList<T> {
288305
/// Move the first element to the back of the list.
289306
///
290307
/// If the list is empty, do nothing.
308+
///
309+
/// # Example
310+
///
311+
/// ```rust
312+
/// use std::collections::{DList, Deque};
313+
///
314+
/// let mut dl = DList::new();
315+
/// dl.push_back(1i);
316+
/// dl.push_back(2);
317+
/// dl.push_back(3);
318+
///
319+
/// dl.rotate_backward();
320+
///
321+
/// for e in dl.iter() {
322+
/// println!("{}", e); // prints 2, then 3, then 1
323+
/// }
324+
/// ```
291325
#[inline]
292326
pub fn rotate_backward(&mut self) {
293327
self.pop_front_node().map(|head| {
@@ -298,6 +332,25 @@ impl<T> DList<T> {
298332
/// Add all elements from `other` to the end of the list
299333
///
300334
/// O(1)
335+
///
336+
/// # Example
337+
///
338+
/// ```rust
339+
/// use std::collections::{DList, Deque};
340+
///
341+
/// let mut a = DList::new();
342+
/// let mut b = DList::new();
343+
/// a.push_back(1i);
344+
/// a.push_back(2);
345+
/// b.push_back(3i);
346+
/// b.push_back(4);
347+
///
348+
/// a.append(b);
349+
///
350+
/// for e in a.iter() {
351+
/// println!("{}", e); // prints 1, then 2, then 3, then 4
352+
/// }
353+
/// ```
301354
pub fn append(&mut self, mut other: DList<T>) {
302355
match self.list_tail.resolve() {
303356
None => *self = other,
@@ -320,6 +373,25 @@ impl<T> DList<T> {
320373
/// Add all elements from `other` to the beginning of the list
321374
///
322375
/// O(1)
376+
///
377+
/// # Example
378+
///
379+
/// ```rust
380+
/// use std::collections::{DList, Deque};
381+
///
382+
/// let mut a = DList::new();
383+
/// let mut b = DList::new();
384+
/// a.push_back(1i);
385+
/// a.push_back(2);
386+
/// b.push_back(3i);
387+
/// b.push_back(4);
388+
///
389+
/// a.prepend(b);
390+
///
391+
/// for e in a.iter() {
392+
/// println!("{}", e); // prints 3, then 4, then 1, then 2
393+
/// }
394+
/// ```
323395
#[inline]
324396
pub fn prepend(&mut self, mut other: DList<T>) {
325397
mem::swap(self, &mut other);
@@ -330,6 +402,25 @@ impl<T> DList<T> {
330402
/// or at the end.
331403
///
332404
/// O(N)
405+
///
406+
/// # Example
407+
///
408+
/// ```rust
409+
/// use std::collections::{DList, Deque};
410+
///
411+
/// let mut a: DList<int> = DList::new();
412+
/// a.push_back(2i);
413+
/// a.push_back(4);
414+
/// a.push_back(7);
415+
/// a.push_back(8);
416+
///
417+
/// // insert 11 before the first odd number in the list
418+
/// a.insert_when(11, |&e, _| e % 2 == 1);
419+
///
420+
/// for e in a.iter() {
421+
/// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
422+
/// }
423+
/// ```
333424
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
334425
{
335426
let mut it = self.mut_iter();

0 commit comments

Comments
 (0)