Skip to content

Commit

Permalink
auto merge of #9599 : alexcrichton/rust/less-fmt, r=huonw
Browse files Browse the repository at this point in the history
This also prevents future fmt! usage from leaking into the compiler, but it's still turned on by default for everyone else.
  • Loading branch information
bors committed Oct 1, 2013
2 parents f6df7ab + dec3705 commit 8bb48cc
Show file tree
Hide file tree
Showing 817 changed files with 4,793 additions and 4,781 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ifneq ($(wildcard $(NON_BUILD_TARGET_TRIPLES)),)
CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET_TRIPLES))
endif

CFG_RUSTC_FLAGS := $(RUSTFLAGS)
CFG_RUSTC_FLAGS := $(RUSTFLAGS) --cfg nofmt
CFG_GCCISH_CFLAGS :=
CFG_GCCISH_LINK_FLAGS :=

Expand Down
46 changes: 24 additions & 22 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
}
}
~~~~~~~~
Expand Down Expand Up @@ -817,12 +817,14 @@ An example of `use` declarations:
use std::num::sin;
use std::option::{Some, None};
# fn foo<T>(_: T){}
fn main() {
// Equivalent to 'info!(std::num::sin(1.0));'
info!(sin(1.0));
// Equivalent to 'std::num::sin(1.0);'
sin(1.0);
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'
info!(~[Some(1.0), None]);
// Equivalent to 'foo(~[std::option::Some(1.0), std::option::None]);'
foo(~[Some(1.0), None]);
}
~~~~

Expand Down Expand Up @@ -1040,8 +1042,8 @@ output slot type would normally be. For example:

~~~~
fn my_err(s: &str) -> ! {
info!(s);
fail!();
info2!("{}", s);
fail2!();
}
~~~~

Expand All @@ -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 {
Expand Down Expand Up @@ -2382,7 +2384,7 @@ fn ten_times(f: &fn(int)) {
}
}
ten_times(|j| println(fmt!("hello, %d", j)));
ten_times(|j| println!("hello, {}", j));
~~~~

Expand Down Expand Up @@ -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")
}
~~~~

Expand Down Expand Up @@ -2633,7 +2635,7 @@ match x {
return;
}
_ => {
fail!();
fail2!();
}
}
~~~~
Expand Down Expand Up @@ -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!()
};
~~~~

Expand Down Expand Up @@ -3472,20 +3474,20 @@ 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")
}
```

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
Expand Down
14 changes: 7 additions & 7 deletions doc/tutorial-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorial-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ 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
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);
}
~~~

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
~~~

Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ let hi = "hi";
let mut count = 0;
while count < 10 {
println(fmt!("count: %?", count));
println!("count: {}", count);
count += 1;
}
~~~~
Expand Down Expand Up @@ -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].

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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))
}
~~~~

Expand All @@ -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))
}
~~~~

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
~~~~
Expand Down Expand Up @@ -1566,7 +1569,7 @@ arguments.
use std::task::spawn;
do spawn() || {
debug!("I'm a task, whatever");
debug2!("I'm a task, whatever");
}
~~~~

Expand All @@ -1578,7 +1581,7 @@ may be omitted from `do` expressions.
use std::task::spawn;
do spawn {
debug!("Kablam!");
debug2!("Kablam!");
}
~~~~

Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 8bb48cc

Please sign in to comment.