Skip to content

Commit ff1e44d

Browse files
llogiqsimonsan
authored andcommitted
doctest the examples
1 parent 1d606a5 commit ff1e44d

10 files changed

+36
-11
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: rust
2+
3+
rust: stable
4+
5+
sudo: false
6+
7+
script:
8+
|
9+
set -e;
10+
for name in idioms/*.md patterns/*.md anti_patterns/*.md intro.md
11+
do
12+
echo "## $name"
13+
rustdoc --test-args "-q" --test $name
14+
done
15+
set +e;

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ You should start with [the template](template.md). Copy it into the appropriate
7474
directory, edit it, and submit a PR. You might not want every section, and you
7575
might want to add extra sections.
7676

77+
When writing examples, please try to make them compile. This allows us to test
78+
them. If you fail to write an example that is both complete and readable,
79+
please at least mark your example code with `ignore`.
80+
7781
We suggest leaving a comment on the [issue tracker](https://github.com/rust-unofficial/patterns/issues)
7882
so that other people don't start working on the same topic.
7983

idioms/ctor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ is to use a static `new` method to create an object.
88

99
## Example
1010

11-
```rust
11+
```rust,ignore
1212
// A Rust vector, see liballoc/vec.rs
1313
pub struct Vec<T> {
1414
buf: RawVec<T>,

idioms/deref.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ and borrowed views of data.
88

99
## Example
1010

11-
```rust
11+
```rust,ignore
1212
struct Vec<T> {
13-
...
13+
//..
1414
}
1515
1616
impl<T> Deref for Vec<T> {
1717
type Target = [T];
1818
1919
fn deref(&self) -> &[T] {
20-
...
20+
unimplemented!()
2121
}
2222
}
2323
```

idioms/dtor-finally.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ be used to run code that must be run before exit.
1010
## Example
1111

1212
```rust
13+
fn baz() -> Result<(), ()> { Ok(()) }
14+
1315
fn bar() -> Result<(), ()> {
1416
// These don't need to be defined inside the function.
1517
struct Foo;

idioms/pass-var-to-closure.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Use variable rebinding in separate scope for that.
1414
Use
1515

1616
```rust
17+
use std::rc::Rc;
18+
1719
let num1 = Rc::new(1);
1820
let num2 = Rc::new(2);
1921
let num3 = Rc::new(3);
@@ -30,6 +32,8 @@ let closure = {
3032
instead of
3133

3234
```rust
35+
use std::rc::Rc;
36+
3337
let num1 = Rc::new(1);
3438
let num2 = Rc::new(2);
3539
let num3 = Rc::new(3);

idioms/priv-extend.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod a {
1818
}
1919
}
2020

21-
fn main(s: a::S) {
21+
fn any(s: a::S) {
2222
// Because S::bar is private, it cannot be named here and we must use `..`
2323
// in the pattern.
2424
let a::S { foo: _, ..} = s;

patterns/builder.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Construct an object with calls to a builder helper.
77

88
## Example
99

10-
```rust
10+
```rust,ignore
1111
struct Foo {
1212
// Lots of complicated fields.
1313
}
@@ -85,7 +85,7 @@ The example takes and returns the builder by value. It is often more ergonomic
8585
borrow checker makes this work naturally. This approach has the advantage that
8686
one can write code like
8787

88-
```
88+
```rust,ignore
8989
let mut fb = FooBuilder::new();
9090
fb.a();
9191
fb.b();

patterns/compose-structs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pattern often reveals smaller units of functionality.
2020
Here is a contrived example of where the borrow checker foils us in our plan to
2121
use a struct:
2222

23-
```rust
23+
```rust,ignore
2424
struct A {
2525
f1: u32,
2626
f2: u32,
@@ -30,7 +30,7 @@ struct A {
3030
fn foo(a: &mut A) -> &u32 { &a.f2 }
3131
fn bar(a: &mut A) -> u32 { a.f1 + a.f3 }
3232
33-
fn main(a: &mut A) {
33+
fn baz(a: &mut A) {
3434
// x causes a to be borrowed for the rest of the function.
3535
let x = foo(a);
3636
// Borrow check error
@@ -59,7 +59,7 @@ struct C {
5959
fn foo(b: &mut B) -> &u32 { &b.f2 }
6060
fn bar(c: &mut C) -> u32 { c.f1 + c.f3 }
6161

62-
fn main(a: &mut A) {
62+
fn baz(a: &mut A) {
6363
let x = foo(&mut a.b);
6464
// Now it's OK!
6565
let y = bar(&mut a.c);

patterns/fold.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fold in the usual sense. See the discussion below for more details.
1111

1212
## Example
1313

14-
```rust
14+
```rust,ignore
1515
// The data we will fold, a simple AST.
1616
mod ast {
1717
pub enum Stmt {

0 commit comments

Comments
 (0)