Skip to content

Commit 202f722

Browse files
committed
---
yaml --- r: 97517 b: refs/heads/snap-stage3 c: a61d7d6 h: refs/heads/master i: 97515: 934b9f8 v: v3
1 parent fc15798 commit 202f722

File tree

27 files changed

+266
-692
lines changed

27 files changed

+266
-692
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: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: fda71f26301d153ca8d9489281d382af79792d63
4+
refs/heads/snap-stage3: a61d7d6dbec3b35abdd78ff424824e745cb4f0cb
55
refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/doc/complement-cheatsheet.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,113 +6,113 @@
66

77
Use [`ToStr`](http://static.rust-lang.org/doc/master/std/to_str/trait.ToStr.html).
88

9-
```rust
9+
~~~
1010
let x: int = 42;
1111
let y: ~str = x.to_str();
12-
```
12+
~~~
1313

1414
**String to int**
1515

1616
Use [`FromStr`](http://static.rust-lang.org/doc/master/std/from_str/trait.FromStr.html), and its helper function, [`from_str`](http://static.rust-lang.org/doc/master/std/from_str/fn.from_str.html).
1717

18-
```rust
18+
~~~
1919
let x: Option<int> = from_str("42");
2020
let y: int = x.unwrap();
21-
```
21+
~~~
2222

2323
**Int to string, in non-base-10**
2424

2525
Use [`ToStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.ToStrRadix.html).
2626

27-
```rust
27+
~~~
2828
use std::num::ToStrRadix;
2929
3030
let x: int = 42;
3131
let y: ~str = x.to_str_radix(16);
32-
```
32+
~~~
3333

3434
**String to int, in non-base-10**
3535

3636
Use [`FromStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.FromStrRadix.html), and its helper function, [`from_str_radix`](http://static.rust-lang.org/doc/master/std/num/fn.from_str_radix.html).
3737

38-
```rust
38+
~~~
3939
use std::num::from_str_radix;
4040
4141
let x: Option<int> = from_str_radix("deadbeef", 16);
4242
let y: int = x.unwrap();
43-
```
43+
~~~
4444

4545
# File operations
4646

4747
## How do I read from a file?
4848

4949
Use [`File::open`](http://static.rust-lang.org/doc/master/std/io/fs/struct.File.html#method.open) to create a [`File`](http://static.rust-lang.org/doc/master/std/io/fs/struct.File.html) struct, which implements the [`Reader`](http://static.rust-lang.org/doc/master/std/io/trait.Reader.html) trait.
5050

51-
```rust
51+
~~~
5252
use std::path::Path;
5353
use std::io::fs::File;
5454
5555
let path : Path = Path::new("Doc-FAQ-Cheatsheet.md");
5656
let on_error = || fail!("open of {:?} failed", path);
5757
let reader : File = File::open(&path).unwrap_or_else(on_error);
58-
```
58+
~~~
5959

6060
## How do I iterate over the lines in a file?
6161

6262
Use the [`lines`](http://static.rust-lang.org/doc/master/std/io/trait.Buffer.html#method.lines) method on a [`BufferedReader`](http://static.rust-lang.org/doc/master/std/io/buffered/struct.BufferedReader.html).
6363

64-
```rust
64+
~~~
6565
use std::io::buffered::BufferedReader;
6666
6767
let mut reader = BufferedReader::new(reader);
6868
for line in reader.lines() {
6969
print!("line: {}", line);
7070
}
71-
```
71+
~~~
7272

7373
# String operations
7474

7575
## How do I search for a substring?
7676

7777
Use the [`find_str`](http://static.rust-lang.org/doc/master/std/str/trait.StrSlice.html#tymethod.find_str) method.
7878

79-
```rust
79+
~~~
8080
let str = "Hello, this is some random string";
8181
let index: Option<uint> = str.find_str("rand");
82-
```
82+
~~~
8383

8484
# Containers
8585

8686
## How do I get the length of a vector?
8787

8888
The [`Container`](http://static.rust-lang.org/doc/master/std/container/trait.Container.html) trait provides the `len` method.
8989

90-
```rust
90+
~~~
9191
let u: ~[u32] = ~[0, 1, 2];
9292
let v: &[u32] = &[0, 1, 2, 3];
9393
let w: [u32, .. 5] = [0, 1, 2, 3, 4];
9494
9595
println!("u: {}, v: {}, w: {}", u.len(), v.len(), w.len()); // 3, 4, 5
96-
```
96+
~~~
9797

9898
## How do I iterate over a vector?
9999

100100
Use the [`iter`](http://static.rust-lang.org/doc/master/std/vec/trait.ImmutableVector.html#tymethod.iter) method.
101101

102-
```rust
102+
~~~
103103
let values: ~[int] = ~[1, 2, 3, 4, 5];
104104
for value in values.iter() { // value: &int
105105
println!("{}", *value);
106106
}
107-
```
107+
~~~
108108

109109
(See also [`mut_iter`](http://static.rust-lang.org/doc/master/std/vec/trait.MutableVector.html#tymethod.mut_iter) which yields `&mut int` and [`move_iter`](http://static.rust-lang.org/doc/master/std/vec/trait.OwnedVector.html#tymethod.move_iter) which yields `int` while consuming the `values` vector.)
110110

111111
# Type system
112112

113113
## How do I store a function in a struct?
114114

115-
```rust
115+
~~~
116116
struct Foo {
117117
myfunc: fn(int, uint) -> i32
118118
}
@@ -131,22 +131,22 @@ fn main() {
131131
println!("{}", (f.myfunc)(1, 2));
132132
println!("{}", (g.myfunc)(3, 4));
133133
}
134-
```
134+
~~~
135135

136136
Note that the parenthesis surrounding `f.myfunc` are necessary: they are how Rust disambiguates field lookup and method call. The `'a` on `FooClosure` is the lifetime of the closure's environment pointer.
137137

138138
## How do I express phantom types?
139139

140140
[Phantom types](http://www.haskell.org/haskellwiki/Phantom_type) are those that cannot be constructed at compile time. To express these in Rust, zero-variant `enum`s can be used:
141141

142-
```rust
142+
~~~
143143
enum Open {}
144144
enum Closed {}
145-
```
145+
~~~
146146

147147
Phantom types are useful for enforcing state at compile time. For example:
148148

149-
```rust
149+
~~~
150150
struct Door<State>(~str);
151151
152152
fn close(Door(name): Door<Open>) -> Door<Closed> {
@@ -159,7 +159,7 @@ fn open(Door(name): Door<Closed>) -> Door<Open> {
159159
160160
let _ = close(Door::<Open>(~"front")); // ok
161161
let _ = close(Door::<Closed>(~"front")); // error: mismatched types: expected `main::Door<main::Open>` but found `main::Door<main::Closed>`
162-
```
162+
~~~
163163

164164
# FFI (Foreign Function Interface)
165165

@@ -178,19 +178,19 @@ Note: The Rust signatures should be wrapped in an `extern "ABI" { ... }` block.
178178

179179
You might see things like this in C APIs:
180180

181-
```c
181+
~~~ {.notrust}
182182
typedef struct Window Window;
183183
Window* createWindow(int width, int height);
184-
```
184+
~~~
185185

186186
You can use a zero-element `enum` ([phantom type](#how-do-i-express-phantom-types)) to represent the opaque object handle. The FFI would look like this:
187187

188-
```rust
188+
~~~
189189
enum Window {}
190190
extern "C" {
191191
fn createWindow(width: c_int, height: c_int) -> *Window;
192192
}
193-
```
193+
~~~
194194

195195
Using a phantom type ensures that the handles cannot be (safely) constructed in client code.
196196

0 commit comments

Comments
 (0)