Skip to content

Fixing code examples, statement about working tests on code examples #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion idioms/ctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
buf: RawVec<T>,
Expand Down
6 changes: 3 additions & 3 deletions idioms/deref.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ and borrowed views of data.

## Example

```rust
```rust,ignore
struct Vec<T> {
...
//..
}

impl<T> Deref for Vec<T> {
type Target = [T];

fn deref(&self) -> &[T] {
...
unimplemented!()
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions idioms/dtor-finally.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions idioms/pass-var-to-closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion idioms/priv-extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions patterns/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Construct an object with calls to a builder helper.

## Example

```rust
```rust,ignore
struct Foo {
// Lots of complicated fields.
}
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions patterns/compose-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion patterns/fold.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down