1
Fork 0

tutorial: Simplify the first example. Misc

This commit is contained in:
Brian Anderson 2012-09-22 23:11:24 -07:00
parent 3b89dcbdf2
commit 2e7ddee823

View file

@ -204,16 +204,13 @@ paretheses, while their bodies *must* be wrapped in
brackets. Single-statement, bracket-less bodies are not allowed. brackets. Single-statement, bracket-less bodies are not allowed.
~~~~ ~~~~
# fn calibrate_universe() -> bool { false } # fn recalibrate_universe() -> bool { true }
# fn party_on() {}
# fn panic() {}
fn main() { fn main() {
while calibrate_universe() { /* A simple loop */
/* Ensure that basic math still operates is expected */ loop {
if 2*20 > 30 { // A tricky calculation
party_on(); // That's a relief if recalibrate_universe() {
} else { return;
panic();
} }
} }
} }
@ -438,10 +435,9 @@ The nil literal is written just like the type: `()`. The keywords
Character literals are written between single quotes, as in `'x'`. Just as in Character literals are written between single quotes, as in `'x'`. Just as in
C, Rust understands a number of character escapes, using the backslash C, Rust understands a number of character escapes, using the backslash
character, `\n`, `\r`, and `\t` being the most common. character, `\n`, `\r`, and `\t` being the most common. String literals,
written between double quotes, allow the same escape sequences. Rust strings
String literals allow the same escape sequences. They are written may contain newlines.
between double quotes (`"hello"`). Rust strings may contain newlines.
## Operators ## Operators
@ -482,14 +478,19 @@ a syntax extension is being used, the names of all syntax extensions end with
which is `fmt!`, a `sprintf`-style text formatter that is expanded at compile which is `fmt!`, a `sprintf`-style text formatter that is expanded at compile
time. time.
~~~~
io::println(fmt!("%s is %d", ~"the answer", 42));
~~~~
`fmt!` supports most of the directives that [printf][pf] supports, but `fmt!` supports most of the directives that [printf][pf] supports, but
will give you a compile-time error when the types of the directives will give you a compile-time error when the types of the directives
don't match the types of the arguments. don't match the types of the arguments.
~~~~
# let mystery_object = ();
io::println(fmt!("%s is %d", "the answer", 43));
// %? will conveniently print any type
io::println(fmt!("what is this thing: %?", mystery_object));
~~~~
[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf [pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
You can define your own syntax extensions with the macro system, which is out You can define your own syntax extensions with the macro system, which is out
@ -505,11 +506,11 @@ compulsory, an optional `else` clause can be appended, and multiple
~~~~ ~~~~
if false { if false {
io::println(~"that's odd"); io::println("that's odd");
} else if true { } else if true {
io::println(~"right"); io::println("right");
} else { } else {
io::println(~"neither true nor false"); io::println("neither true nor false");
} }
~~~~ ~~~~