Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
6fec871
update.rs: better support for structuring the book
Jun 7, 2014
927b470
update.rs: Add more characters to the HTML escaping function
Jun 9, 2014
0fbc03d
editor: don't optimze code (reduce chances of a timeout)
Jun 10, 2014
9a959b6
editor: set output font to monospace
Jun 10, 2014
93fbc87
hello: introduce the live code editor
Jun 11, 2014
a0874f8
print: fix style and a FIXME
Jun 11, 2014
a50cf6a
literals: Fix style, simplify (remove section about types)
Jun 11, 2014
7b63d6c
variables: split, talk about scope and shadowing
Jun 11, 2014
485ba95
type: split, talk about literal suffixes
Jun 11, 2014
28a5910
expression: remove type annotations and the definition of a block
Jun 11, 2014
07c67ef
if-else: merge source files in one
Jun 11, 2014
71d95d2
loops: rename to loop and split
Jun 11, 2014
7d20330
for: fix style, clarify that range takes steps of one
Jun 11, 2014
c2d23d0
functions: rename to fn, more details about the `()` return type
Jun 11, 2014
7ca724d
tuples: fix style
Jun 11, 2014
b80e642
match: split and fix style
Jun 11, 2014
9f03eaa
structs: fix style
Jun 11, 2014
9268d86
generics: fix style
Jun 11, 2014
aa2abb2
box: fix style, drop imports
Jun 11, 2014
d4eaf65
raii: fix style
Jun 11, 2014
7601daf
move: fix style and split
Jun 11, 2014
c62fc44
borrow: fix style and split
Jun 11, 2014
3b8e3a8
constants: fix style, add FIXME
Jun 11, 2014
bb1bde6
enum: fix style, simpler example, split
Jun 11, 2014
f326a7d
option: split in two sections: fail and option
Jun 11, 2014
5d1f073
array: fix style
Jun 11, 2014
ba5256f
traits: fix style, shorter example, split, remove stub about trait ob…
Jun 11, 2014
1907a57
operator: rename to ops, fix style
Jun 11, 2014
59bdc0e
bounds: fix style, reword
Jun 12, 2014
1c68ac1
drop: fix style, simpler example
Jun 12, 2014
d62b92f
iter: fix style
Jun 12, 2014
0b59fd9
closures: fix style
Jun 12, 2014
dd4c515
hof: fix style
Jun 12, 2014
a670e2d
vec: fix style
Jun 12, 2014
7519371
str: fix style
Jun 12, 2014
7753487
tasks: fix style
Jun 12, 2014
007bacc
mod: fix style, split, remove use of `String`
Jun 12, 2014
60917b2
crates: fix style
Jun 12, 2014
807757b
fn: add sub-section on unused functions
Jun 12, 2014
631ccd2
attribute: style, split
Jun 12, 2014
63f5da2
structs: add visibility sub-section
Jun 12, 2014
39fc0d9
variables/mut: add prints
Jun 12, 2014
ef74101
type/literals: clarify unsuffixed literals
Jun 12, 2014
03e3b01
expression: style
Jun 12, 2014
24aa25c
box: import `std::mem`
Jun 12, 2014
2ccae85
array: import `std::mem`
Jun 12, 2014
216a87e
iter: import `std::mem`
Jun 12, 2014
a778c04
hof: import `std::iter`
Jun 12, 2014
5efb2c5
channels: style, reduce number of tasks to reduce chance of timeout
Jun 12, 2014
f75b61d
timers: style
Jun 12, 2014
127da7e
sockets: style
Jun 13, 2014
4d55840
result: style, split
Jun 13, 2014
0e502a3
path: remove binary, fix style
Jun 13, 2014
0201903
file: fix style, split, use the editor
Jun 13, 2014
06a0f81
process: fix style, split
Jun 13, 2014
5f26c41
fs: fix style
Jun 13, 2014
d86cc6e
tasks: tweak comments
Jun 13, 2014
159c72d
type/literals: briefly explain not yet covered concepts
Jun 13, 2014
776789a
crates: fix style, split
Jun 13, 2014
9b27d71
clone: better reflect interaction between cloning and resources
Jun 13, 2014
30a7eb9
methods: fix style, don't use `String`
Jun 13, 2014
76c0d71
type: new sub-section on aliasing
Jun 13, 2014
b2792a9
mod: fix error message
Jun 14, 2014
0ac6293
structs: suppress warnings
Jun 14, 2014
6ba9671
box: suppress warnings
Jun 14, 2014
73092aa
move: change FIXMEs by TODOs
Jun 14, 2014
a9212c5
borrrow: suppress warnings, change FIXMEs by TODOs
Jun 14, 2014
e313d35
trait: replace FIXMEs by TODOs
Jun 14, 2014
bdc6df6
drop: show how to explicitly `drop` variables
Jun 14, 2014
3cab607
Restructure the sections of the book
Jun 14, 2014
8f3db45
borrow: copies of mutably borrowed variables are no longer allowed
Jun 14, 2014
f1b9bde
update.rs: update to latest nightly
Jun 14, 2014
77e1a46
make test: update whitelist
Jun 14, 2014
c18ed30
setup-stage: fix travis
Jun 14, 2014
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/*
stage/*
36 changes: 22 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@ GITBOOK = gitbook
RUSTC = rustc
QUIET = -A unused-variable -A dead-code -A dead-assignment -A experimental
RUSTC_NT = $(RUSTC) --no-trans --test $(QUIET)
WHITELIST = examples/borrow/freeze.rs \
examples/attribute/custom.rs \
examples/crates/executable.rs \
examples/lifetime/lifetime.rs \
examples/lifetime/reference-bad.rs \
examples/mod/nested.rs \
examples/move/assignment.rs \
examples/move/pass-by-value.rs \
examples/variables/declare.rs \
examples/variables/variables.rs
WHITELIST = examples/attribute/cfg/custom/custom.rs \
examples/borrow/borrow.rs \
examples/borrow/freeze/freeze.rs \
examples/borrow/mut/mut.rs \
examples/bounds/bounds.rs \
examples/constants/constants.rs \
examples/crates/link/executable.rs \
examples/lifetime/lifetime.rs \
examples/mod/mod.rs \
examples/print/print.rs \
examples/type/cast/cast.rs \
examples/type/type.rs \
examples/variables/declare/declare.rs \
examples/variables/mut/mut.rs \
examples/variables/scope/scope.rs \
examples/vec/vec.rs \
temporary-whitelist-below-this-point-remove-later \
examples/lifetime/reference-bad.rs

srcs = $(filter-out $(WHITELIST),$(shell find examples -name '*.rs'))

.PHONY: all book clean test serve

all:
./setup-stage.sh
$(RUSTC) src/update.rs
./update
rm update
$(RUSTC) src/update.rs --out-dir bin
bin/update

book:
cd stage && $(GITBOOK) build
./fix-edit-button.sh
./add-relinks.sh

clean:
rm -rf stage
rm -rf {bin,stage}

test:
$(foreach src,$(srcs),$(RUSTC_NT) $(src) || exit;)
Expand Down
18 changes: 9 additions & 9 deletions examples/array/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem::size_of_val;
use std::mem;

// This function borrows a slice
fn analyze_slice(slice: &[int]) {
Expand All @@ -7,27 +7,27 @@ fn analyze_slice(slice: &[int]) {
}

fn main() {
// fixed-size array (type signature is superfluous)
// Fixed-size array (type signature is superfluous)
let xs: [int, ..5] = [1, 2, 3, 4, 5];

// indexing starts at 0
// Indexing starts at 0
println!("first element of the array: {}", xs[0]);
println!("second element of the array: {}", xs[1]);

// len() returns the size of the array
// `len` returns the size of the array
println!("array size: {}", xs.len());

// arrays are stack allocated
println!("array occupies {} bytes", size_of_val(&xs));
// Arrays are stack allocated
println!("array occupies {} bytes", mem::size_of_val(&xs));

// arrays can be automatically borrowed as slices
// Arrays can be automatically borrowed as slices
println!("borrow the whole array as a slice");
analyze_slice(xs);

// slices can point to a section of an array
// Slices can point to a section of an array
println!("borrow a section of the array as a slice");
analyze_slice(xs.slice(1, 4));

// out of bound indexing yields a runtime failure
// Out of bound indexing yields a task failure
println!("{}", xs[5]);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// this function only gets compiled if the target OS is linux
// This function only gets compiled if the target OS is linux
#[cfg(target_os = "linux")]
fn are_you_on_linux() {
println!("You are running linux!")
}

// this function only gets compiled if the target OS is *not* linux
// And this function only gets compiled if the target OS is *not* linux
#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
println!("You are *not* running linux!")
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions examples/attribute/cfg/custom/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Some conditionals like `target_os` are implicitly provided by `rustc`, but
custom conditionals must be passed to `rustc` using the `--cfg` flag.

{custom.rs}

Without the custom `cfg` flag:

{custom.out}

With the custom `cfg` flag:

```
$ rustc --cfg some_condition custom.rs && ./custom
condition met!
```
3 changes: 3 additions & 0 deletions examples/attribute/cfg/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The `cfg` attribute can be use to achieve conditional compilation.

{cfg.play}
14 changes: 14 additions & 0 deletions examples/attribute/crate/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
The `crate_type` attribute can be used to tell the compiler whether a crate is
a binary or a library (and even which type of library). And the `crate_id`
attribute can be used to set the name and the version of the crate.

{lib.rs}

When the `crate_type` attribute is used, we no longer need to pass the
`--crate-type` flag to `rustc`.

```
$ rustc lib.rs
$ ls lib*
liberty-a1e7dc98-0.1.rlib
```
18 changes: 18 additions & 0 deletions examples/attribute/crate/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This crate is a library
#![crate_type = "lib"]
// The library is named "erty", and its version is 0.1
#![crate_id = "erty#0.1"]

pub fn public_function() {
println!("called erty's `public_function()`");
}

fn private_function() {
println!("called erty's `private_function()`");
}

pub fn indirect_access() {
print!("called erty's `indirect_access()`, that\n> ");

private_function();
}
44 changes: 9 additions & 35 deletions examples/attribute/input.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,22 @@
An attribute is metadata applied to some module, crate or item. This metadata
can be used to/for:

* conditional compilation of code
<!-- TODO: Link these to their respective examples -->
* [conditional compilation of code](/attribute/cfg.html)
* [set crate name, version and type (binary or library)](/attribute/crate.html)
* disable lints (warnings)
* enable compiler features (macros, glob imports, etc.)
* link to a foreign library
* mark functions as unit tests
* mark functions that will be part of a benchmark
* select crate type
* set library name and version
* set name and version of a crate

When attributes apply to a whole crate, their syntax is `#![crate_attribute]`,
and when they apply to a module or item, the syntax is `#[item_attribute]`
(notice the missing bang `!`). Some attributes can take key-value arguments
like `#[attribute(key = "value")]`, or string arguments like
`#[attribute = "value"]`.
(notice the missing bang `!`).

Let's add metadata to the library we created in the previous chapter.
Attributes can take arguments with different syntaxes:

{lib.rs}

```
# we don't need the --crate-type flag this time
$ rustc lib.rs
$ ls lib*
liberty-a1e7dc98-0.1.rlib
```

Here's an example of conditional compilation.

{conditional.rs}

{conditional.out}

Some conditionals like `target_os` are implicitly provided by `rustc`, but
custom conditionals must be passed to `rustc` using the `--cfg` flag.

{custom.rs}

{custom.out}

```
$ rustc --cfg some_condition custom.rs && ./custom
condition met!
```

More usages of attributes will be covered in other chapters.
* `#[attribute = "value"]`
* `#[attribute(key = "value")]`
* `#[attribute(value)]`
18 changes: 0 additions & 18 deletions examples/attribute/lib.rs

This file was deleted.

43 changes: 43 additions & 0 deletions examples/borrow/alias/alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
struct Point { x: int, y: int, z: int }

fn main() {
let mut point = Point { x: 0, y: 0, z: 0 };

{
let borrowed_point = &point;
let another_borrow = &point;

// Data can be accessed via the references and the original owner
println!("Point has coordinates: ({}, {}, {})",
borrowed_point.x, another_borrow.y, point.z);

// Error! Can't borrow point as mutable because it's also borrowed as
// immutable
//let mutable_borrow = &mut point;
// TODO ^ Try uncommenting this line

// Immutable references go out of scope
}

{
let mutable_borrow = &mut point;

// Change data via mutable reference
mutable_borrow.x = 5;

// Error! Can't borrow `point` as immutable because it's also borrowed
// as mutable
//let y = &point.y;
// TODO ^ Try uncommenting this line

// Error! Can't print, because println! takes an immutable reference
//println!("Point Z coordinate is {}", point.z);
// TODO ^ Try uncommenting this line

// Mutable reference goes out of scope
}

// Immutable references to point are allowed again
println!("Point now has coordinates: ({}, {}, {})",
point.x, point.y, point.z);
}
6 changes: 6 additions & 0 deletions examples/borrow/alias/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Data can be immutably borrowed any number of times, but once immutably
borrowed, the original data can't be mutably borrowed. On the other side, data
can be mutably borrowed *once*, and until the mutable reference goes out of
scope, the original data can't be borrowed again.

{alias.play}
29 changes: 21 additions & 8 deletions examples/borrow/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
use std::owned::Box;

// this function takes ownership of the box
// This function takes ownership of the box
fn eat_box(boxed_int: Box<int>) {
// the box gets destroyed in this scope
println!("destroying box that contains {}", boxed_int);
}

// this function borrows the box
// This function borrows the box
fn peep_inside_box(borrowed_box: &Box<int>) {
println!("This box contains {}", borrowed_box);
}

fn main() {
// boxed integer
// A boxed integer
let boxed_int = box 5;

// borrow the box, ownership is not taken
// Borrow the box, ownership is not taken
peep_inside_box(&boxed_int);

// The box can be borrowed again
peep_inside_box(&boxed_int);

// give up ownership of the box
{
// Take a reference to the data contained inside the box
let _ref_to_int: &int = &*boxed_int;

// Error! Can't destroy boxed_int, while the inner value has been
// borrowed
eat_box(boxed_int);
// FIXME ^ Comment out this line

// `_ref_to_int` goes out of scope
}

// Give up ownership of the box
eat_box(boxed_int);
}
16 changes: 0 additions & 16 deletions examples/borrow/freeze.rs

This file was deleted.

17 changes: 17 additions & 0 deletions examples/borrow/freeze/freeze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn main() {
let mut _integer = 5;

{
// Borrow `integer`
let _ref_to_integer = &_integer;

// Error! `integer` is frozen in this scope
_integer = 4;
// FIXME ^ Comment out this line

// `ref_to_integer` goes out of scope
}

// Ok! `integer` is not frozen in this scope
_integer = 4;
}
4 changes: 4 additions & 0 deletions examples/borrow/freeze/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.play}
Loading