Skip to content

Commit e403abb

Browse files
committed
---
yaml --- r: 130932 b: refs/heads/auto c: c8e5068 h: refs/heads/master v: v3
1 parent d922d7f commit e403abb

File tree

11 files changed

+93
-344
lines changed

11 files changed

+93
-344
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 036f38033f2f94ce5b706ada2871054e9b8b56ce
16+
refs/heads/auto: c8e5068ec93ff7af550bcd0276f10c1321e4b337
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/doc/guide-strings.md

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,9 @@ fn foo(s: String) {
9292
```
9393

9494
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.
96-
97-
## Generic functions
98-
99-
To write a function that's generic over types of strings, use [the `Str`
100-
trait](http://doc.rust-lang.org/std/str/trait.Str.html):
101-
102-
```{rust}
103-
fn some_string_length<T: Str>(x: T) -> uint {
104-
x.as_slice().len()
105-
}
106-
107-
fn main() {
108-
let s = "Hello, world";
109-
110-
println!("{}", some_string_length(s));
111-
112-
let s = "Hello, world".to_string();
113-
114-
println!("{}", some_string_length(s));
115-
}
116-
```
117-
118-
Both of these lines will print `12`.
119-
120-
The only method that the `Str` trait has is `as_slice()`, which gives you
121-
access to a `&str` value from the underlying string.
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.
12298

12399
## Comparisons
124100

@@ -145,65 +121,6 @@ fn compare(string: String) {
145121
Converting a `String` to a `&str` is cheap, but converting the `&str` to a
146122
`String` involves an allocation.
147123

148-
## Indexing strings
149-
150-
You may be tempted to try to access a certain character of a `String`, like
151-
this:
152-
153-
```{rust,ignore}
154-
let s = "hello".to_string();
155-
156-
println!("{}", s[0]);
157-
```
158-
159-
This does not compile. This is on purpose. In the world of UTF-8, direct
160-
indexing is basically never what you want to do. The reason is that each
161-
character can be a variable number of bytes. This means that you have to iterate
162-
through the characters anyway, which is a O(n) operation.
163-
164-
To iterate over a string, use the `graphemes()` method on `&str`:
165-
166-
```{rust}
167-
let s = "αἰθήρ";
168-
169-
for l in s.graphemes(true) {
170-
println!("{}", l);
171-
}
172-
```
173-
174-
Note that `l` has the type `&str` here, since a single grapheme can consist of
175-
multiple codepoints, so a `char` wouldn't be appropriate.
176-
177-
This will print out each character in turn, as you'd expect: first "α", then
178-
"ἰ", etc. You can see that this is different than just the individual bytes.
179-
Here's a version that prints out each byte:
180-
181-
```{rust}
182-
let s = "αἰθήρ";
183-
184-
for l in s.bytes() {
185-
println!("{}", l);
186-
}
187-
```
188-
189-
This will print:
190-
191-
```{notrust,ignore}
192-
206
193-
177
194-
225
195-
188
196-
176
197-
206
198-
184
199-
206
200-
174
201-
207
202-
129
203-
```
204-
205-
Many more bytes than graphemes!
206-
207124
# Other Documentation
208125

209126
* [the `&str` API documentation](/std/str/index.html)

branches/auto/src/doc/guide.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ in your file name, use an underscore. `hello_world.rs` versus `goodbye.rs`.
150150

151151
Now that you've got your file open, type this in:
152152

153-
```
153+
```{rust}
154154
fn main() {
155155
println!("Hello, world!");
156156
}
@@ -166,7 +166,7 @@ Hello, world!
166166

167167
Success! Let's go over what just happened in detail.
168168

169-
```
169+
```{rust}
170170
fn main() {
171171
172172
}
@@ -186,7 +186,7 @@ declaration, with one space in between.
186186

187187
Next up is this line:
188188

189-
```
189+
```{rust}
190190
println!("Hello, world!");
191191
```
192192

@@ -562,7 +562,7 @@ the block is executed. If it's `false`, then it is not.
562562

563563
If you want something to happen in the `false` case, use an `else`:
564564

565-
```
565+
```{rust}
566566
let x = 5i;
567567
568568
if x == 5i {
@@ -574,7 +574,8 @@ if x == 5i {
574574

575575
This is all pretty standard. However, you can also do this:
576576

577-
```
577+
578+
```{rust}
578579
let x = 5i;
579580
580581
let y = if x == 5i {
@@ -586,7 +587,7 @@ let y = if x == 5i {
586587

587588
Which we can (and probably should) write like this:
588589

589-
```
590+
```{rust}
590591
let x = 5i;
591592
592593
let y = if x == 5i { 10i } else { 15i };
@@ -643,7 +644,7 @@ every line of Rust code you see.
643644
What is this exception that makes us say 'almost?' You saw it already, in this
644645
code:
645646

646-
```
647+
```{rust}
647648
let x = 5i;
648649
649650
let y: int = if x == 5i { 10i } else { 15i };
@@ -989,7 +990,7 @@ notation: `origin.x`.
989990
The values in structs are immutable, like other bindings in Rust. However, you
990991
can use `mut` to make them mutable:
991992

992-
```rust
993+
```{rust}
993994
struct Point {
994995
x: int,
995996
y: int,
@@ -1013,7 +1014,7 @@ called a **tuple struct**. Tuple structs do have a name, but their fields
10131014
don't:
10141015

10151016

1016-
```
1017+
```{rust}
10171018
struct Color(int, int, int);
10181019
struct Point(int, int, int);
10191020
```
@@ -1028,7 +1029,7 @@ let origin = Point(0, 0, 0);
10281029
It is almost always better to use a struct than a tuple struct. We would write
10291030
`Color` and `Point` like this instead:
10301031

1031-
```rust
1032+
```{rust}
10321033
struct Color {
10331034
red: int,
10341035
blue: int,
@@ -1049,7 +1050,7 @@ There _is_ one case when a tuple struct is very useful, though, and that's a
10491050
tuple struct with only one element. We call this a 'newtype,' because it lets
10501051
you create a new type that's a synonym for another one:
10511052

1052-
```
1053+
```{rust}
10531054
struct Inches(int);
10541055
10551056
let length = Inches(10);
@@ -1166,7 +1167,7 @@ what's the solution?
11661167
Rust has a keyword, `match`, that allows you to replace complicated `if`/`else`
11671168
groupings with something more powerful. Check it out:
11681169

1169-
```rust
1170+
```{rust}
11701171
let x = 5i;
11711172
11721173
match x {
@@ -1407,7 +1408,7 @@ We now loop forever with `loop`, and use `break` to break out early.
14071408
`continue` is similar, but instead of ending the loop, goes to the next
14081409
iteration: This will only print the odd numbers:
14091410

1410-
```
1411+
```{rust}
14111412
for x in range(0i, 10i) {
14121413
if x % 2 == 0 { continue; }
14131414
@@ -4122,7 +4123,7 @@ the ability to use this **method call syntax** via the `impl` keyword.
41224123

41234124
Here's how it works:
41244125

4125-
```
4126+
```{rust}
41264127
struct Circle {
41274128
x: f64,
41284129
y: f64,
@@ -4161,7 +4162,7 @@ multiplications later, and we have our area.
41614162
You can also define methods that do not take a `self` parameter. Here's a
41624163
pattern that's very common in Rust code:
41634164

4164-
```
4165+
```{rust}
41654166
struct Circle {
41664167
x: f64,
41674168
y: f64,

0 commit comments

Comments
 (0)