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: branches/snap-stage3/doc/complement-cheatsheet.md
+38-49Lines changed: 38 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -6,116 +6,113 @@
6
6
7
7
Use [`ToStr`](http://static.rust-lang.org/doc/master/std/to_str/trait.ToStr.html).
8
8
9
-
~~~
9
+
```rust
10
10
letx:int=42;
11
11
lety: ~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
-
~~~
18
+
```rust
19
19
letx:Option<int> =from_str("42");
20
20
lety: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
-
~~~
27
+
```rust
28
28
usestd::num::ToStrRadix;
29
29
30
30
letx:int=42;
31
31
lety: ~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
-
~~~
38
+
```rust
39
39
usestd::num::from_str_radix;
40
40
41
-
let x: Option<i64> = from_str_radix("deadbeef", 16);
42
-
let y: i64 = x.unwrap();
43
-
~~~
41
+
letx:Option<int> =from_str_radix("deadbeef", 16);
42
+
lety:int=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.
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
-
~~~
64
+
```rust
65
65
usestd::io::buffered::BufferedReader;
66
-
# use std::io::mem::MemReader;
67
-
68
-
# let reader = MemReader::new(~[]);
69
66
70
67
letmutreader=BufferedReader::new(reader);
71
68
forlineinreader.lines() {
72
69
print!("line: {}", line);
73
70
}
74
-
~~~
71
+
```
75
72
76
73
# String operations
77
74
78
75
## How do I search for a substring?
79
76
80
77
Use the [`find_str`](http://static.rust-lang.org/doc/master/std/str/trait.StrSlice.html#tymethod.find_str) method.
81
78
82
-
~~~
79
+
```rust
83
80
letstr="Hello, this is some random string";
84
81
letindex:Option<uint> =str.find_str("rand");
85
-
~~~
82
+
```
86
83
87
84
# Containers
88
85
89
86
## How do I get the length of a vector?
90
87
91
88
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.
104
101
105
-
~~~
102
+
```rust
106
103
letvalues: ~[int] = ~[1, 2, 3, 4, 5];
107
104
forvalueinvalues.iter() { // value: &int
108
105
println!("{}", *value);
109
106
}
110
-
~~~
107
+
```
111
108
112
109
(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.)
113
110
114
111
# Type system
115
112
116
113
## How do I store a function in a struct?
117
114
118
-
~~~
115
+
```rust
119
116
structFoo {
120
117
myfunc:fn(int, uint) ->i32
121
118
}
@@ -134,27 +131,24 @@ fn main() {
134
131
println!("{}", (f.myfunc)(1, 2));
135
132
println!("{}", (g.myfunc)(3, 4));
136
133
}
137
-
~~~
134
+
```
138
135
139
136
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.
140
137
141
138
## How do I express phantom types?
142
139
143
140
[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:
144
141
145
-
~~~
142
+
```rust
146
143
enumOpen {}
147
144
enumClosed {}
148
-
~~~
145
+
```
149
146
150
147
Phantom types are useful for enforcing state at compile time. For example:
Note: The Rust signatures should be wrapped in an `extern "ABI" { ... }` block.
187
176
188
177
### Representing opaque handles
189
178
190
179
You might see things like this in C APIs:
191
180
192
-
~~~{.notrust}
181
+
```c
193
182
typedefstruct Window Window;
194
183
Window* createWindow(int width, int height);
195
-
~~~
184
+
```
196
185
197
186
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