Skip to content

Commit 464d1d0

Browse files
committed
auto merge of #11405 : huonw/rust/moredocs, r=huonw
Various documentation changes, change the 'borrowed pointer' terminology to 'reference', fix a problem with 'make dist' on windows.
2 parents fda71f2 + 9dc44c7 commit 464d1d0

Some content is hidden

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

54 files changed

+337
-286
lines changed

Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ export CFG_SRC_DIR
410410
export CFG_BUILD_DIR
411411
export CFG_VERSION
412412
export CFG_VERSION_WIN
413+
export CFG_RELEASE
413414
export CFG_BUILD
414415
export CFG_LLVM_ROOT
415416
export CFG_ENABLE_MINGW_CROSS

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ do
806806
make_dir $h/test/doc-guide-container
807807
make_dir $h/test/doc-guide-tasks
808808
make_dir $h/test/doc-guide-conditions
809+
make_dir $h/test/doc-complement-cheatsheet
809810
make_dir $h/test/doc-rust
810811
done
811812

doc/complement-cheatsheet.md

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,113 +6,116 @@
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
41-
let x: Option<int> = from_str_radix("deadbeef", 16);
42-
let y: int = x.unwrap();
43-
```
41+
let x: Option<i64> = from_str_radix("deadbeef", 16);
42+
let y: i64 = x.unwrap();
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+
~~~ {.xfail-test}
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;
66+
# use std::io::mem::MemReader;
67+
68+
# let reader = MemReader::new(~[]);
6669
6770
let mut reader = BufferedReader::new(reader);
6871
for line in reader.lines() {
6972
print!("line: {}", line);
7073
}
71-
```
74+
~~~
7275

7376
# String operations
7477

7578
## How do I search for a substring?
7679

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

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

8487
# Containers
8588

8689
## How do I get the length of a vector?
8790

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

90-
```rust
93+
~~~
9194
let u: ~[u32] = ~[0, 1, 2];
9295
let v: &[u32] = &[0, 1, 2, 3];
9396
let w: [u32, .. 5] = [0, 1, 2, 3, 4];
9497
9598
println!("u: {}, v: {}, w: {}", u.len(), v.len(), w.len()); // 3, 4, 5
96-
```
99+
~~~
97100

98101
## How do I iterate over a vector?
99102

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

102-
```rust
105+
~~~
103106
let values: ~[int] = ~[1, 2, 3, 4, 5];
104107
for value in values.iter() { // value: &int
105108
println!("{}", *value);
106109
}
107-
```
110+
~~~
108111

109112
(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.)
110113

111114
# Type system
112115

113116
## How do I store a function in a struct?
114117

115-
```rust
118+
~~~
116119
struct Foo {
117120
myfunc: fn(int, uint) -> i32
118121
}
@@ -131,24 +134,27 @@ fn main() {
131134
println!("{}", (f.myfunc)(1, 2));
132135
println!("{}", (g.myfunc)(3, 4));
133136
}
134-
```
137+
~~~
135138

136139
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.
137140

138141
## How do I express phantom types?
139142

140143
[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:
141144

142-
```rust
145+
~~~
143146
enum Open {}
144147
enum Closed {}
145-
```
148+
~~~
146149

147150
Phantom types are useful for enforcing state at compile time. For example:
148151

149-
```rust
152+
~~~
150153
struct Door<State>(~str);
151154
155+
struct Open;
156+
struct Closed;
157+
152158
fn close(Door(name): Door<Open>) -> Door<Closed> {
153159
Door::<Closed>(name)
154160
}
@@ -157,40 +163,45 @@ fn open(Door(name): Door<Closed>) -> Door<Open> {
157163
Door::<Open>(name)
158164
}
159165
160-
let _ = close(Door::<Open>(~"front")); // ok
166+
let _ = close(Door::<Open>(~"front"));
167+
~~~
168+
169+
Attempting to close a closed door is prevented statically:
170+
171+
~~~ {.xfail-test}
161172
let _ = close(Door::<Closed>(~"front")); // error: mismatched types: expected `main::Door<main::Open>` but found `main::Door<main::Closed>`
162-
```
173+
~~~
163174

164175
# FFI (Foreign Function Interface)
165176

166177
## C function signature conversions
167178

168-
Description | C signature | Equivalent Rust signature
169-
----------------------|----------------------------------------------|------------------------------------------
170-
no parameters | `void foo(void);` | `fn foo();`
171-
return value | `int foo(void);` | `fn foo() -> c_int;`
172-
function parameters | `void foo(int x, int y);` | `fn foo(x: int, y: int);`
173-
in-out pointers | `void foo(const int* in_ptr, int* out_ptr);` | `fn foo(in_ptr: *c_int, out_ptr: *mut c_int);`
179+
Description C signature Equivalent Rust signature
180+
---------------------- ---------------------------------------------- ------------------------------------------
181+
no parameters `void foo(void);` `fn foo();`
182+
return value `int foo(void);` `fn foo() -> c_int;`
183+
function parameters `void foo(int x, int y);` `fn foo(x: int, y: int);`
184+
in-out pointers `void foo(const int* in_ptr, int* out_ptr);` `fn foo(in_ptr: *c_int, out_ptr: *mut c_int);`
174185

175186
Note: The Rust signatures should be wrapped in an `extern "ABI" { ... }` block.
176187

177188
### Representing opaque handles
178189

179190
You might see things like this in C APIs:
180191

181-
```c
192+
~~~ {.notrust}
182193
typedef struct Window Window;
183194
Window* createWindow(int width, int height);
184-
```
195+
~~~
185196

186197
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:
187198

188-
```rust
199+
~~~ {.xfail-test}
189200
enum Window {}
190201
extern "C" {
191202
fn createWindow(width: c_int, height: c_int) -> *Window;
192203
}
193-
```
204+
~~~
194205

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

@@ -200,4 +211,4 @@ For small examples, have full type annotations, as much as is reasonable, to kee
200211

201212
Similar documents for other programming languages:
202213

203-
* [http://pleac.sourceforge.net/](http://pleac.sourceforge.net)
214+
* [http://pleac.sourceforge.net/](http://pleac.sourceforge.net)

doc/complement-lang-faq.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ They start small (ideally in the hundreds of bytes) and expand dynamically by ca
218218
* Managed boxes may not be shared between tasks
219219
* Owned boxes may be transferred (moved) between tasks
220220

221-
## What is the difference between a borrowed pointer (`&`) and managed and owned boxes?
221+
## What is the difference between a reference (`&`) and managed and owned boxes?
222222

223-
* Borrowed pointers point to the interior of a stack _or_ heap allocation
224-
* Borrowed pointers can only be formed when it will provably be outlived by the referent
225-
* Borrowed pointers to managed box pointers keep the managed boxes alive
226-
* Borrowed pointers to owned boxes prevent their ownership from being transferred
227-
* Borrowed pointers employ region-based alias analysis to ensure correctness
223+
* References point to the interior of a stack _or_ heap allocation
224+
* References can only be formed when it will provably be outlived by the referent
225+
* References to managed box pointers keep the managed boxes alive
226+
* References to owned boxes prevent their ownership from being transferred
227+
* References employ region-based alias analysis to ensure correctness
228228

229229
## Why aren't function signatures inferred? Why only local slots?
230230

doc/guide-conditions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% Rust Condition and Error-handling Guide
1+
% The Rust Condition and Error-handling Guide
22

33
# Introduction
44

doc/guide-container.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% Containers and Iterators Guide
1+
% The Rust Containers and Iterators Guide
22

33
# Containers
44

doc/guide-ffi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% Rust Foreign Function Interface Guide
1+
% The Rust Foreign Function Interface Guide
22

33
# Introduction
44

@@ -417,7 +417,7 @@ However, there are currently no guarantees about the layout of an `enum`.
417417

418418
Rust's owned and managed boxes use non-nullable pointers as handles which point to the contained
419419
object. However, they should not be manually created because they are managed by internal
420-
allocators. Borrowed pointers can safely be assumed to be non-nullable pointers directly to the
420+
allocators. References can safely be assumed to be non-nullable pointers directly to the
421421
type. However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so
422422
prefer using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions
423423
about them.

0 commit comments

Comments
 (0)