Skip to content
Merged
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: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ srcs = $(filter-out $(WHITELIST),$(shell find examples -name '*.rs'))
.PHONY: all book clean test serve

all:
mkdir -p stage
ln -sf ../examples/README.md stage/README.md
./setup-stage.sh
$(RUSTC) src/update.rs
./update
rm update
Expand All @@ -32,7 +31,6 @@ clean:
rm -rf stage

test:
$(RUSTC) --test src/update.rs
$(foreach src,$(srcs),$(RUSTC_NT) $(src) || exit;)
./check-line-length.sh

Expand Down
6 changes: 6 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["rust-playpen"],
"pluginsConfig": {
"rust-playpen": {}
}
}
2 changes: 1 addition & 1 deletion check-line-length.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

WHITELIST=(
./examples/lifetime/lifetime.rs
./src/test.rs
./src/playpen.rs
)

echo "Checking if any rust file has a line longer than 79 characters"
Expand Down
4 changes: 0 additions & 4 deletions examples/array/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ Instead, a slice is two-word object, the first word is a pointer to the data,
and the second word is the length of the slice. Slices can be used to borrow a
section of an array, and have `&[T]` as type signature.

{array.rs}

{array.play}

{array.out}
2 changes: 0 additions & 2 deletions examples/attribute/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ Here's an example of conditional compilation.

{conditional.rs}

{conditional.play}

{conditional.out}

Some conditionals like `target_os` are implicitly provided by `rustc`, but
Expand Down
12 changes: 0 additions & 12 deletions examples/borrow/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,22 @@ passing objects by-value (`T`), objects can be passed by reference (`&T`). The
compiler statically guarantees that references *always* point to valid objects,
via its borrow checker.

{borrow.rs}

{borrow.play}

{borrow.out}

`&T` borrows the data via an immutable reference, and the borrower can read the
data but not modify it. Mutable data can be immutably borrowed, but can also be
mutably borrowed via a mutable reference `&mut T`, giving read/write access to
the borrower.

{mut.rs}

{mut.play}

{mut.out}

When data is borrowed, it also *freezes*. "Frozen" data can't be modified via
the original object, until all the references to it go out of scope.

{freeze.rs}

{freeze.play}

{freeze.out}

Data can be immutably borrowed any number of times, but once immutably
Expand All @@ -37,15 +29,11 @@ scope, the original data can't be borrowed again.

{re-borrow.rs}

{re-borrow.play}

{re-borrow.out}

When doing pattern matching or destructuring via the `let` binding, the `ref`
keyword can be used to take references to the fields of a struct.

{ref.rs}

{ref.play}

{ref.out}
4 changes: 0 additions & 4 deletions examples/bounds/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ When writing generic code, it's important to *bound* the generic data to
conform to some traits. This allows using the trait methods in these bounded
implementations.

{bounds.rs}

{bounds.play}

{bounds.out}
4 changes: 0 additions & 4 deletions examples/box/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ Boxed values can be dereferenced (unboxed) using the `*` operator, this removes
one layer of indirection. Alternatively, `let box x = y` can be used to unbox
`y` into `x`.

{box.rs}

{box.play}

{box.out}
4 changes: 0 additions & 4 deletions examples/channels/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ task number 4 reported

Albeit non obvious from the output above, channels are actually FIFO.

{fifo.rs}

{fifo.play}

The order is maintained, after removing the scheduler non-determinism.

{fifo.out}
4 changes: 0 additions & 4 deletions examples/clone/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ assignments or function calls. Sometimes the intention is to make a copy of the
resource, this can be accomplish by calling the `clone()` method, defined in
the `Clone` trait.

{clone.rs}

{clone.play}

{clone.out}
4 changes: 0 additions & 4 deletions examples/closures/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ surrounding scope. Closures consist of three parts:
inferred
* a block, the last expression is the return value

{closures.rs}

{closures.play}

{closures.out}
4 changes: 0 additions & 4 deletions examples/constants/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ allocated in read-only memory. `'static` is an special lifetime that outlives
all the other lifetimes, and indicates that the referenced data is available in
all the scopes.

{constants.rs}

{constants.play}

{constants.out}
6 changes: 0 additions & 6 deletions examples/enum/input.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
The `enum` keyword allows the creation of tagged unions, which can be used as
algebraic data types.

{enum.rs}

{enum.play}

{enum.out}

`enum` can also be used to represent C-like enums.

{c-like.rs}

{c-like.play}

{c-like.out}
4 changes: 0 additions & 4 deletions examples/expression/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ Block are expressions too, so they can be used as rvalues in assignments. The
last expression in the block will be assigned to the lvalue. If the last
expression of the block ends with a semicolon, the return value will be `()`.

{expression.rs}

{expression.play}

{expression.out}
2 changes: 0 additions & 2 deletions examples/for/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ function. `range(a, b)` will yield values from `a` (inclusive) to `b`

Let's write fizzbuzz using `for` instead of `while`.

{for.rs}

{for.play}
2 changes: 0 additions & 2 deletions examples/functions/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ return a value earlier from the function, even from inside loops or ifs.

Let's rewrite fizzbuzz using functions!

{functions.rs}

{functions.play}
2 changes: 0 additions & 2 deletions examples/generics/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ annotation is usually not required. When that's not the case, structs can be
specialized via type annotation, and functions can be specialized passing the
generic arguments using this syntax `::<T, U, ..>`.

{generics.rs}

{generics.play}
2 changes: 0 additions & 2 deletions examples/hello/input.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
This is the source code of the traditional Hello World program.

{hello.rs}

{hello.play}

`println!` is a *macro* (we'll cover them later) that prints text to the
Expand Down
4 changes: 0 additions & 4 deletions examples/hof/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ Rust provides Higher Order Functions (HOF), these are functions that take a
closure as argument to produce a more useful function. HOFs and lazy iterators
give Rust its functional flavor.

{hof.rs}

{hof.play}

{hof.out}

[Option](http://static.rust-lang.org/doc/master/core/option/type.Option.html)
and
[Iterator](http://static.rust-lang.org/doc/master/core/iter/trait.Iterator.html)
Expand Down
6 changes: 0 additions & 6 deletions examples/if-else/input.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
Branching with if-else is similar to C. Unlike C, the boolean condition doesn't
need to be surrounded by parentheses. Each condition is followed by a block.

{if-else.rs}

{if-else.play}

{if-else.out}

If-else conditionals are expressions too. Because of Rust type safety, all the
branches must return the same type.

{if-else-expr.rs}

{if-else-expr.play}

{if-else-expr.out}
4 changes: 0 additions & 4 deletions examples/iter/input.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
The `Iterator` trait is used to implement iterators over collections (like
arrays) and lazy value generators.

{iter.rs}

{iter.play}

{iter.out}

The `Iterator` trait gives access to
[several methods](http://static.rust-lang.org/doc/master/core/iter/trait.Iterator.html).
6 changes: 0 additions & 6 deletions examples/lifetime/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,13 @@ Let's illustrate with an example: we want a function that returns a reference
to the title field of a Book struct. The most generic function that we could
write would look like this:

{reference-bad.rs}

{reference-bad.play}

{reference-bad.out}

The compiler can't tell how `'a` and `'b` are related, so we must supply this
information. The answer here is that `'a = 'b`, the reason is that the title
field will be destroyed when the book gets destroyed (same way with the
creation time), therefore the title field has the same lifetime as the book.

{reference-good.rs}

{reference-good.play}

{reference-good.out}
4 changes: 0 additions & 4 deletions examples/literals/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ The operators available and operator precedence are similar to other [C-like
languages
](https://en.wikipedia.org/wiki/Operator_precedence#Programming_languages)

{literals.rs}

{literals.play}

{literals.out}
8 changes: 0 additions & 8 deletions examples/loops/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@ Rust provides a `loop` keyword to indicate an infinite loop.
The `break` keyword can be used to exit a loop at anytime, whereas the
`continue` can be used to skip the rest of the iteration and start a new one.

{loop.rs}

{loop.play}

{loop.out}

It's possible to `break` or `continue` outer loops when dealing with nested
loops. In these cases, the loops must be annotated with some `'label` and the
label must be passed to the `break`/`continue` statement.

{nested.rs}

{nested.play}

{nested.out}

The `while` keyword can be used for looping until a condition is met.

Let's write the infamous fizzbuzz using a `while` loop.

{while.rs}

{while.play}
4 changes: 0 additions & 4 deletions examples/match/input.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
Rust provides pattern matching via the `match` keyword, the syntax will be
covered in the code below.

{match.rs}

{match.play}

{match.out}
4 changes: 0 additions & 4 deletions examples/methods/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ Methods are functions attached to objects, these methods have access to the
data of the object and its other methods via the `self` keyword. These methods
are grouped under a `impl` block.

{methods.rs}

{methods.play}

{methods.out}
12 changes: 0 additions & 12 deletions examples/mod/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,35 @@ between them.
A module is collection of items like: functions, structs, traits, impl blocks,
and even other modules.

{nested.rs}

{nested.play}

{nested.out}

By default, the items in a module have private visibility, but this can be
overridden using the `pub` modifier. Only the public items of a module can be
accessed from outside the module scope.

{visibility.rs}

{visibility.play}

{visibility.out}

The `use` declaration can be used to bind a full path to a new name, for easier
access.

{use.rs}

{use.play}

{use.out}

The `super` and `self` keywords can be used in the path, to remove ambiguity
when accessing items.

{super.rs}

{super.play}

{super.out}

Structs have an extra level of visibility, their fields can be public or
private. This allows encapsulation.

{structs.rs}

{structs.play}

{structs.out}

Modules can be mapped to a file/directory hierarchy. Let's break down the
Expand Down
Loading