Skip to content

Commit

Permalink
tutorial: Discuss failure and asserts together
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Jul 7, 2012
1 parent 59355e9 commit 205b483
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,21 @@ handle the failure, allowing the program to continue running.
to access a vector out of bounds, or running a pattern match with no
matching clauses, both result in the equivalent of a `fail`.
## Assertions
The keyword `assert`, followed by an expression with boolean type,
will check that the given expression results in `true`, and cause a
failure otherwise. It is typically used to double-check things that
*should* hold at a certain point in a program. `assert` statements are
always active; there is no way to build Rust code with assertions
disabled.
~~~~
let mut x = 100;
while (x > 10) { x -= 10; }
assert x == 10;
~~~~
## Logging
Rust has a built-in logging mechanism, using the `log` statement.
Expand Down Expand Up @@ -861,21 +876,6 @@ and will log the formatted string:
Because the macros `#debug`, `#warn`, and `#error` expand to calls to `log`,
their arguments are also lazily evaluated.
## Assertions
The keyword `assert`, followed by an expression with boolean type,
will check that the given expression results in `true`, and cause a
failure otherwise. It is typically used to double-check things that
*should* hold at a certain point in a program. `assert` statements are
always active; there is no way to build Rust code with assertions
disabled.
~~~~
let mut x = 100;
while (x > 10) { x -= 10; }
assert x == 10;
~~~~
# Functions
Like all other static declarations, such as `type`, functions can be
Expand Down

0 comments on commit 205b483

Please sign in to comment.