diff --git a/README.md b/README.md index 03093c66..6503f387 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ You should start with [the template](template.md). Copy it into the appropriate directory, edit it, and submit a PR. You might not want every section, and you might want to add extra sections. +When writing examples, please try to make them compile. This allows us to test +them. If you fail to write an example that is both complete and readable, +please at least mark your example code with `ignore`. + We suggest leaving a comment on the [issue tracker](https://github.com/rust-unofficial/patterns/issues) so that other people don't start working on the same topic. diff --git a/idioms/ctor.md b/idioms/ctor.md index 7ff38bcf..17684b76 100644 --- a/idioms/ctor.md +++ b/idioms/ctor.md @@ -8,7 +8,7 @@ is to use a static `new` method to create an object. ## Example -```rust +```rust,ignore // A Rust vector, see liballoc/vec.rs pub struct Vec { buf: RawVec, diff --git a/idioms/deref.md b/idioms/deref.md index 86d67d1b..9f0f8d44 100644 --- a/idioms/deref.md +++ b/idioms/deref.md @@ -8,16 +8,16 @@ and borrowed views of data. ## Example -```rust +```rust,ignore struct Vec { - ... + //.. } impl Deref for Vec { type Target = [T]; fn deref(&self) -> &[T] { - ... + unimplemented!() } } ``` diff --git a/idioms/dtor-finally.md b/idioms/dtor-finally.md index b7edf52f..230ebbb0 100644 --- a/idioms/dtor-finally.md +++ b/idioms/dtor-finally.md @@ -10,6 +10,8 @@ be used to run code that must be run before exit. ## Example ```rust +fn baz() -> Result<(), ()> { Ok(()) } + fn bar() -> Result<(), ()> { // These don't need to be defined inside the function. struct Foo; diff --git a/idioms/pass-var-to-closure.md b/idioms/pass-var-to-closure.md index 2df610d0..4d82767d 100644 --- a/idioms/pass-var-to-closure.md +++ b/idioms/pass-var-to-closure.md @@ -14,6 +14,8 @@ Use variable rebinding in separate scope for that. Use ```rust +use std::rc::Rc; + let num1 = Rc::new(1); let num2 = Rc::new(2); let num3 = Rc::new(3); @@ -30,6 +32,8 @@ let closure = { instead of ```rust +use std::rc::Rc; + let num1 = Rc::new(1); let num2 = Rc::new(2); let num3 = Rc::new(3); diff --git a/idioms/priv-extend.md b/idioms/priv-extend.md index 492bec1e..dddfcee5 100644 --- a/idioms/priv-extend.md +++ b/idioms/priv-extend.md @@ -18,7 +18,7 @@ mod a { } } -fn main(s: a::S) { +fn any(s: a::S) { // Because S::bar is private, it cannot be named here and we must use `..` // in the pattern. let a::S { foo: _, ..} = s; diff --git a/patterns/builder.md b/patterns/builder.md index 4b6ce12d..e589901a 100644 --- a/patterns/builder.md +++ b/patterns/builder.md @@ -7,7 +7,7 @@ Construct an object with calls to a builder helper. ## Example -```rust +```rust,ignore struct Foo { // Lots of complicated fields. } @@ -85,7 +85,7 @@ The example takes and returns the builder by value. It is often more ergonomic borrow checker makes this work naturally. This approach has the advantage that one can write code like -``` +```rust,ignore let mut fb = FooBuilder::new(); fb.a(); fb.b(); diff --git a/patterns/compose-structs.md b/patterns/compose-structs.md index 449fdc3e..d9b9f72e 100644 --- a/patterns/compose-structs.md +++ b/patterns/compose-structs.md @@ -20,7 +20,7 @@ pattern often reveals smaller units of functionality. Here is a contrived example of where the borrow checker foils us in our plan to use a struct: -```rust +```rust,ignore struct A { f1: u32, f2: u32, @@ -30,7 +30,7 @@ struct A { fn foo(a: &mut A) -> &u32 { &a.f2 } fn bar(a: &mut A) -> u32 { a.f1 + a.f3 } -fn main(a: &mut A) { +fn baz(a: &mut A) { // x causes a to be borrowed for the rest of the function. let x = foo(a); // Borrow check error @@ -59,7 +59,7 @@ struct C { fn foo(b: &mut B) -> &u32 { &b.f2 } fn bar(c: &mut C) -> u32 { c.f1 + c.f3 } -fn main(a: &mut A) { +fn baz(a: &mut A) { let x = foo(&mut a.b); // Now it's OK! let y = bar(&mut a.c); diff --git a/patterns/fold.md b/patterns/fold.md index 4d70d4f9..6b649b82 100644 --- a/patterns/fold.md +++ b/patterns/fold.md @@ -11,7 +11,7 @@ fold in the usual sense. See the discussion below for more details. ## Example -```rust +```rust,ignore // The data we will fold, a simple AST. mod ast { pub enum Stmt {