You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/complement-cheatsheet.md
+49-38Lines changed: 49 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -6,113 +6,116 @@
6
6
7
7
Use [`ToStr`](http://static.rust-lang.org/doc/master/std/to_str/trait.ToStr.html).
8
8
9
-
```rust
9
+
~~~
10
10
let x: int = 42;
11
11
let y: ~str = x.to_str();
12
-
```
12
+
~~~
13
13
14
14
**String to int**
15
15
16
16
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).
17
17
18
-
```rust
18
+
~~~
19
19
let x: Option<int> = from_str("42");
20
20
let y: int = x.unwrap();
21
-
```
21
+
~~~
22
22
23
23
**Int to string, in non-base-10**
24
24
25
25
Use [`ToStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.ToStrRadix.html).
26
26
27
-
```rust
27
+
~~~
28
28
use std::num::ToStrRadix;
29
29
30
30
let x: int = 42;
31
31
let y: ~str = x.to_str_radix(16);
32
-
```
32
+
~~~
33
33
34
34
**String to int, in non-base-10**
35
35
36
36
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).
37
37
38
-
```rust
38
+
~~~
39
39
use std::num::from_str_radix;
40
40
41
-
letx:Option<int> =from_str_radix("deadbeef", 16);
42
-
lety:int=x.unwrap();
43
-
```
41
+
let x: Option<i64> = from_str_radix("deadbeef", 16);
42
+
let y: i64 = x.unwrap();
43
+
~~~
44
44
45
45
# File operations
46
46
47
47
## How do I read from a file?
48
48
49
49
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.
50
50
51
-
```rust
51
+
~~~{.xfail-test}
52
52
use std::path::Path;
53
53
use std::io::fs::File;
54
54
55
55
let path : Path = Path::new("Doc-FAQ-Cheatsheet.md");
56
56
let on_error = || fail!("open of {:?} failed", path);
57
57
let reader : File = File::open(&path).unwrap_or_else(on_error);
58
-
```
58
+
~~~
59
59
60
60
## How do I iterate over the lines in a file?
61
61
62
62
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).
63
63
64
-
```rust
64
+
~~~
65
65
use std::io::buffered::BufferedReader;
66
+
# use std::io::mem::MemReader;
67
+
68
+
# let reader = MemReader::new(~[]);
66
69
67
70
let mut reader = BufferedReader::new(reader);
68
71
for line in reader.lines() {
69
72
print!("line: {}", line);
70
73
}
71
-
```
74
+
~~~
72
75
73
76
# String operations
74
77
75
78
## How do I search for a substring?
76
79
77
80
Use the [`find_str`](http://static.rust-lang.org/doc/master/std/str/trait.StrSlice.html#tymethod.find_str) method.
78
81
79
-
```rust
82
+
~~~
80
83
let str = "Hello, this is some random string";
81
84
let index: Option<uint> = str.find_str("rand");
82
-
```
85
+
~~~
83
86
84
87
# Containers
85
88
86
89
## How do I get the length of a vector?
87
90
88
91
The [`Container`](http://static.rust-lang.org/doc/master/std/container/trait.Container.html) trait provides the `len` method.
Use the [`iter`](http://static.rust-lang.org/doc/master/std/vec/trait.ImmutableVector.html#tymethod.iter) method.
101
104
102
-
```rust
105
+
~~~
103
106
let values: ~[int] = ~[1, 2, 3, 4, 5];
104
107
for value in values.iter() { // value: &int
105
108
println!("{}", *value);
106
109
}
107
-
```
110
+
~~~
108
111
109
112
(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.)
110
113
111
114
# Type system
112
115
113
116
## How do I store a function in a struct?
114
117
115
-
```rust
118
+
~~~
116
119
struct Foo {
117
120
myfunc: fn(int, uint) -> i32
118
121
}
@@ -131,24 +134,27 @@ fn main() {
131
134
println!("{}", (f.myfunc)(1, 2));
132
135
println!("{}", (g.myfunc)(3, 4));
133
136
}
134
-
```
137
+
~~~
135
138
136
139
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.
137
140
138
141
## How do I express phantom types?
139
142
140
143
[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:
141
144
142
-
```rust
145
+
~~~
143
146
enum Open {}
144
147
enum Closed {}
145
-
```
148
+
~~~
146
149
147
150
Phantom types are useful for enforcing state at compile time. For example:
Note: The Rust signatures should be wrapped in an `extern "ABI" { ... }` block.
176
187
177
188
### Representing opaque handles
178
189
179
190
You might see things like this in C APIs:
180
191
181
-
```c
192
+
~~~{.notrust}
182
193
typedef struct Window Window;
183
194
Window* createWindow(int width, int height);
184
-
```
195
+
~~~
185
196
186
197
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:
0 commit comments