tutorial: Remove usage of fmt!
This commit is contained in:
parent
73c6c9109f
commit
9ce31f6dd9
48
doc/rust.md
48
doc/rust.md
|
@ -683,15 +683,15 @@ mod math {
|
|||
type complex = (f64, f64);
|
||||
fn sin(f: f64) -> f64 {
|
||||
...
|
||||
# fail!();
|
||||
# fail2!();
|
||||
}
|
||||
fn cos(f: f64) -> f64 {
|
||||
...
|
||||
# fail!();
|
||||
# fail2!();
|
||||
}
|
||||
fn tan(f: f64) -> f64 {
|
||||
...
|
||||
# fail!();
|
||||
# fail2!();
|
||||
}
|
||||
}
|
||||
~~~~~~~~
|
||||
|
@ -817,12 +817,14 @@ An example of `use` declarations:
|
|||
use std::num::sin;
|
||||
use std::option::{Some, None};
|
||||
|
||||
fn main() {
|
||||
// Equivalent to 'info!(std::num::sin(1.0));'
|
||||
info!(sin(1.0));
|
||||
# fn foo<T>(_: T){}
|
||||
|
||||
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'
|
||||
info!(~[Some(1.0), None]);
|
||||
fn main() {
|
||||
// Equivalent to 'std::num::sin(1.0);'
|
||||
sin(1.0);
|
||||
|
||||
// Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);'
|
||||
foo(~[Some(1.0), None]);
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -1040,8 +1042,8 @@ output slot type would normally be. For example:
|
|||
|
||||
~~~~
|
||||
fn my_err(s: &str) -> ! {
|
||||
info!(s);
|
||||
fail!();
|
||||
info2!("{}", s);
|
||||
fail2!();
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -1059,7 +1061,7 @@ were declared without the `!` annotation, the following code would not
|
|||
typecheck:
|
||||
|
||||
~~~~
|
||||
# fn my_err(s: &str) -> ! { fail!() }
|
||||
# fn my_err(s: &str) -> ! { fail2!() }
|
||||
|
||||
fn f(i: int) -> int {
|
||||
if i == 42 {
|
||||
|
@ -2382,7 +2384,7 @@ fn ten_times(f: &fn(int)) {
|
|||
}
|
||||
}
|
||||
|
||||
ten_times(|j| println(fmt!("hello, %d", j)));
|
||||
ten_times(|j| println!("hello, {}", j));
|
||||
|
||||
~~~~
|
||||
|
||||
|
@ -2594,9 +2596,9 @@ enum List<X> { Nil, Cons(X, @List<X>) }
|
|||
let x: List<int> = Cons(10, @Cons(11, @Nil));
|
||||
|
||||
match x {
|
||||
Cons(_, @Nil) => fail!("singleton list"),
|
||||
Cons(_, @Nil) => fail2!("singleton list"),
|
||||
Cons(*) => return,
|
||||
Nil => fail!("empty list")
|
||||
Nil => fail2!("empty list")
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -2633,7 +2635,7 @@ match x {
|
|||
return;
|
||||
}
|
||||
_ => {
|
||||
fail!();
|
||||
fail2!();
|
||||
}
|
||||
}
|
||||
~~~~
|
||||
|
@ -2687,7 +2689,7 @@ guard may refer to the variables bound within the pattern they follow.
|
|||
let message = match maybe_digit {
|
||||
Some(x) if x < 10 => process_digit(x),
|
||||
Some(x) => process_other(x),
|
||||
None => fail!()
|
||||
None => fail2!()
|
||||
};
|
||||
~~~~
|
||||
|
||||
|
@ -3472,10 +3474,10 @@ that demonstrates all four of them:
|
|||
|
||||
```rust
|
||||
fn main() {
|
||||
error!("This is an error log")
|
||||
warn!("This is a warn log")
|
||||
info!("this is an info log")
|
||||
debug!("This is a debug log")
|
||||
error2!("This is an error log")
|
||||
warn2!("This is a warn log")
|
||||
info2!("this is an info log")
|
||||
debug2!("This is a debug log")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -3483,9 +3485,9 @@ These four log levels correspond to levels 1-4, as controlled by `RUST_LOG`:
|
|||
|
||||
```bash
|
||||
$ RUST_LOG=rust=3 ./rust
|
||||
rust: ~"\"This is an error log\""
|
||||
rust: ~"\"This is a warn log\""
|
||||
rust: ~"\"this is an info log\""
|
||||
This is an error log
|
||||
This is a warn log
|
||||
this is an info log
|
||||
```
|
||||
|
||||
# Appendix: Rationales and design tradeoffs
|
||||
|
|
|
@ -66,7 +66,7 @@ use std::int;
|
|||
fn main() {
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -281,7 +281,7 @@ fn main() {
|
|||
// The protected logic.
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -387,7 +387,7 @@ condition! {
|
|||
fn main() {
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -462,7 +462,7 @@ fn main() {
|
|||
// The protected logic.
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -540,7 +540,7 @@ fn main() {
|
|||
// The protected logic.
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -636,7 +636,7 @@ fn main() {
|
|||
// The protected logic.
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -766,7 +766,7 @@ fn main() {
|
|||
// The protected logic.
|
||||
let pairs = read_int_pairs();
|
||||
for &(a,b) in pairs.iter() {
|
||||
println(fmt!("%4.4d, %4.4d", a, b));
|
||||
println!("{:4.4d}, {:4.4d}", a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -226,7 +226,7 @@ match x {
|
|||
// complicated stuff goes here
|
||||
return result + val;
|
||||
},
|
||||
_ => fail!("Didn't get good_2")
|
||||
_ => fail2!("Didn't get good_2")
|
||||
}
|
||||
}
|
||||
_ => return 0 // default value
|
||||
|
@ -268,7 +268,7 @@ macro_rules! biased_match (
|
|||
biased_match!((x) ~ (good_1(g1, val)) else { return 0 };
|
||||
binds g1, val )
|
||||
biased_match!((g1.body) ~ (good_2(result) )
|
||||
else { fail!("Didn't get good_2") };
|
||||
else { fail2!("Didn't get good_2") };
|
||||
binds result )
|
||||
// complicated stuff goes here
|
||||
return result + val;
|
||||
|
@ -369,7 +369,7 @@ macro_rules! biased_match (
|
|||
# fn f(x: t1) -> uint {
|
||||
biased_match!(
|
||||
(x) ~ (good_1(g1, val)) else { return 0 };
|
||||
(g1.body) ~ (good_2(result) ) else { fail!("Didn't get good_2") };
|
||||
(g1.body) ~ (good_2(result) ) else { fail2!("Didn't get good_2") };
|
||||
binds val, result )
|
||||
// complicated stuff goes here
|
||||
return result + val;
|
||||
|
|
|
@ -99,7 +99,6 @@ execution. Like any closure, the function passed to `spawn` may capture
|
|||
an environment that it carries across tasks.
|
||||
|
||||
~~~
|
||||
# use std::io::println;
|
||||
# use std::task::spawn;
|
||||
# fn generate_task_number() -> int { 0 }
|
||||
// Generate some state locally
|
||||
|
@ -107,7 +106,7 @@ let child_task_number = generate_task_number();
|
|||
|
||||
do spawn {
|
||||
// Capture it in the remote task
|
||||
println(fmt!("I am child number %d", child_task_number));
|
||||
println!("I am child number {}", child_task_number);
|
||||
}
|
||||
~~~
|
||||
|
||||
|
@ -282,7 +281,7 @@ fn fib(n: uint) -> uint {
|
|||
|
||||
let mut delayed_fib = extra::future::Future::spawn (|| fib(50) );
|
||||
make_a_sandwich();
|
||||
println(fmt!("fib(50) = %?", delayed_fib.get()))
|
||||
println!("fib(50) = {:?}", delayed_fib.get())
|
||||
~~~
|
||||
|
||||
The call to `future::spawn` returns immediately a `future` object regardless of how long it
|
||||
|
@ -310,7 +309,7 @@ fn main() {
|
|||
for ft in futures.mut_iter() {
|
||||
final_res += ft.get();
|
||||
}
|
||||
println(fmt!("π^2/6 is not far from : %?", final_res));
|
||||
println!("π^2/6 is not far from : {}", final_res);
|
||||
}
|
||||
~~~
|
||||
|
||||
|
@ -338,7 +337,7 @@ fn pnorm(nums: &~[float], p: uint) -> float {
|
|||
|
||||
fn main() {
|
||||
let numbers = vec::from_fn(1000000, |_| rand::random::<float>());
|
||||
println(fmt!("Inf-norm = %?", *numbers.iter().max().unwrap()));
|
||||
println!("Inf-norm = {}", *numbers.iter().max().unwrap());
|
||||
|
||||
let numbers_arc = Arc::new(numbers);
|
||||
|
||||
|
@ -349,7 +348,7 @@ fn main() {
|
|||
do spawn {
|
||||
let local_arc : Arc<~[float]> = port.recv();
|
||||
let task_numbers = local_arc.get();
|
||||
println(fmt!("%u-norm = %?", num, pnorm(task_numbers, num)));
|
||||
println!("{}-norm = {}", num, pnorm(task_numbers, num));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -225,7 +225,7 @@ let hi = "hi";
|
|||
let mut count = 0;
|
||||
|
||||
while count < 10 {
|
||||
println(fmt!("count: %?", count));
|
||||
println!("count: {}", count);
|
||||
count += 1;
|
||||
}
|
||||
~~~~
|
||||
|
@ -388,23 +388,26 @@ assert!(y == 4u);
|
|||
but are instead provided by the libraries. To make it clear to the reader when
|
||||
a name refers to a syntax extension, the names of all syntax extensions end
|
||||
with `!`. The standard library defines a few syntax extensions, the most
|
||||
useful of which is `fmt!`, a `sprintf`-style text formatter that you will
|
||||
often see in examples.
|
||||
useful of which is [`format!`][fmt], a `sprintf`-like text formatter that you
|
||||
will often see in examples, and its related family of macros: `print!`,
|
||||
`println!`, and `write!`.
|
||||
|
||||
`fmt!` supports most of the directives that [printf][pf] supports, but unlike
|
||||
printf, will give you a compile-time error when the types of the directives
|
||||
don't match the types of the arguments.
|
||||
`format!` draws syntax from python, but contains many of the same principles
|
||||
that [printf][pf] has. Unlike printf, `format!` will give you a compile-time
|
||||
error when the types of the directives don't match the types of the arguments.
|
||||
|
||||
~~~~
|
||||
# let mystery_object = ();
|
||||
|
||||
println(fmt!("%s is %d", "the answer", 43));
|
||||
// {} will print the "default format" of a type
|
||||
println!("{} is {}", "the answer", 43);
|
||||
|
||||
// %? will conveniently print any type
|
||||
println(fmt!("what is this thing: %?", mystery_object));
|
||||
// {:?} will conveniently print any type
|
||||
println!("what is this thing: {:?}", mystery_object);
|
||||
~~~~
|
||||
|
||||
[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
|
||||
[fmt]: http://static.rust-lang.org/doc/master/std/fmt/index.html
|
||||
|
||||
You can define your own syntax extensions with the macro system. For details, see the [macro tutorial][macros].
|
||||
|
||||
|
@ -737,7 +740,7 @@ fn area(sh: Shape) -> float {
|
|||
match sh {
|
||||
Circle { radius: radius, _ } => float::consts::pi * square(radius),
|
||||
Rectangle { top_left: top_left, bottom_right: bottom_right } => {
|
||||
(bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
|
||||
(bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -753,7 +756,7 @@ unit, `()`, as the empty tuple if you like).
|
|||
~~~~
|
||||
let mytup: (int, int, float) = (10, 20, 30.0);
|
||||
match mytup {
|
||||
(a, b, c) => info!(a + b + (c as int))
|
||||
(a, b, c) => info2!("{}", a + b + (c as int))
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -769,7 +772,7 @@ For example:
|
|||
struct MyTup(int, int, float);
|
||||
let mytup: MyTup = MyTup(10, 20, 30.0);
|
||||
match mytup {
|
||||
MyTup(a, b, c) => info!(a + b + (c as int))
|
||||
MyTup(a, b, c) => info2!("{}", a + b + (c as int))
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -1238,7 +1241,7 @@ something silly like
|
|||
~~~
|
||||
# struct Point { x: float, y: float }
|
||||
let point = &@~Point { x: 10f, y: 20f };
|
||||
println(fmt!("%f", point.x));
|
||||
println!("{:f}", point.x);
|
||||
~~~
|
||||
|
||||
The indexing operator (`[]`) also auto-dereferences.
|
||||
|
@ -1443,7 +1446,7 @@ the enclosing scope.
|
|||
fn call_closure_with_ten(b: &fn(int)) { b(10); }
|
||||
|
||||
let captured_var = 20;
|
||||
let closure = |arg| println(fmt!("captured_var=%d, arg=%d", captured_var, arg));
|
||||
let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
|
||||
|
||||
call_closure_with_ten(closure);
|
||||
~~~~
|
||||
|
@ -1566,7 +1569,7 @@ arguments.
|
|||
use std::task::spawn;
|
||||
|
||||
do spawn() || {
|
||||
debug!("I'm a task, whatever");
|
||||
debug2!("I'm a task, whatever");
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -1578,7 +1581,7 @@ may be omitted from `do` expressions.
|
|||
use std::task::spawn;
|
||||
|
||||
do spawn {
|
||||
debug!("Kablam!");
|
||||
debug2!("Kablam!");
|
||||
}
|
||||
~~~~
|
||||
|
||||
|
@ -1916,7 +1919,7 @@ and `~str`.
|
|||
~~~~
|
||||
# trait Printable { fn print(&self); }
|
||||
impl Printable for int {
|
||||
fn print(&self) { println(fmt!("%d", *self)) }
|
||||
fn print(&self) { println!("{}", *self) }
|
||||
}
|
||||
|
||||
impl Printable for ~str {
|
||||
|
|
Loading…
Reference in New Issue