@@ -212,7 +212,7 @@ do drop
212
212
else enum extern
213
213
false fn for
214
214
if impl
215
- let loop
215
+ let log loop
216
216
match mod mut
217
217
priv pub pure
218
218
ref return
@@ -805,20 +805,21 @@ Use declarations support a number of "convenience" notations:
805
805
An example of ` use ` declarations:
806
806
807
807
~~~~
808
+ use foo = core::info;
808
809
use core::float::sin;
809
810
use core::str::{slice, to_upper};
810
811
use core::option::Some;
811
812
812
813
fn main() {
813
- // Equivalent to 'info!( core::float::sin(1.0));'
814
- info!( sin(1.0));
814
+ // Equivalent to 'log(core::info, core::float::sin(1.0));'
815
+ log(foo, sin(1.0));
815
816
816
- // Equivalent to 'info!( core::option::Some(1.0));'
817
- info!( Some(1.0));
817
+ // Equivalent to 'log(core::info, core::option::Some(1.0));'
818
+ log(info, Some(1.0));
818
819
819
- // Equivalent to
820
- // 'info!( core::str::to_upper(core::str::slice("foo", 0, 1)));'
821
- info!( to_upper(slice("foo", 0, 1)));
820
+ // Equivalent to 'log(core::info,
821
+ // core::str::to_upper(core::str::slice("foo", 0, 1)));'
822
+ log(info, to_upper(slice("foo", 0, 1)));
822
823
}
823
824
~~~~
824
825
@@ -888,10 +889,10 @@ declared, in an angle-bracket-enclosed, comma-separated list following
888
889
the function name.
889
890
890
891
~~~~ {.xfail-test}
891
- fn iter<T>(seq: &[T], f: & fn(T)) {
892
+ fn iter<T>(seq: &[T], f: fn(T)) {
892
893
for seq.each |elt| { f(elt); }
893
894
}
894
- fn map<T, U>(seq: &[T], f: & fn(T) -> U) -> ~[U] {
895
+ fn map<T, U>(seq: &[T], f: fn(T) -> U) -> ~[U] {
895
896
let mut acc = ~[];
896
897
for seq.each |elt| { acc.push(f(elt)); }
897
898
acc
@@ -989,7 +990,7 @@ output slot type would normally be. For example:
989
990
990
991
~~~~
991
992
fn my_err(s: &str) -> ! {
992
- info!( s);
993
+ log(info, s);
993
994
fail!();
994
995
}
995
996
~~~~
@@ -1181,8 +1182,8 @@ Traits are implemented for specific types through separate [implementations](#im
1181
1182
# type BoundingBox = int;
1182
1183
1183
1184
trait Shape {
1184
- fn draw(&self, Surface);
1185
- fn bounding_box(&self ) -> BoundingBox;
1185
+ fn draw(Surface);
1186
+ fn bounding_box() -> BoundingBox;
1186
1187
}
1187
1188
~~~~
1188
1189
@@ -1195,9 +1196,9 @@ These appear after the trait name, using the same syntax used in [generic functi
1195
1196
1196
1197
~~~~
1197
1198
trait Seq<T> {
1198
- fn len(&self ) -> uint;
1199
- fn elt_at(&self, n: uint) -> T;
1200
- fn iter(&self, & fn(T));
1199
+ fn len() -> uint;
1200
+ fn elt_at(n: uint) -> T;
1201
+ fn iter(fn(T));
1201
1202
}
1202
1203
~~~~
1203
1204
@@ -1209,7 +1210,7 @@ For example:
1209
1210
1210
1211
~~~~
1211
1212
# type Surface = int;
1212
- # trait Shape { fn draw(&self, Surface); }
1213
+ # trait Shape { fn draw(Surface); }
1213
1214
1214
1215
fn draw_twice<T: Shape>(surface: Surface, sh: T) {
1215
1216
sh.draw(surface);
@@ -1227,7 +1228,7 @@ to pointers to the trait name, used as a type.
1227
1228
# impl Shape for int { }
1228
1229
# let mycircle = 0;
1229
1230
1230
- let myshape: @ Shape = @mycircle as @Shape;
1231
+ let myshape: Shape = @mycircle as @Shape;
1231
1232
~~~~
1232
1233
1233
1234
The resulting value is a managed box containing the value that was cast,
@@ -1271,8 +1272,8 @@ methods of the supertrait may be called on values of subtrait-bound type paramet
1271
1272
Refering to the previous example of ` trait Circle : Shape ` :
1272
1273
1273
1274
~~~
1274
- # trait Shape { fn area(&self ) -> float; }
1275
- # trait Circle : Shape { fn radius(&self ) -> float; }
1275
+ # trait Shape { fn area() -> float; }
1276
+ # trait Circle : Shape { fn radius() -> float; }
1276
1277
fn radius_times_area<T: Circle>(c: T) -> float {
1277
1278
// `c` is both a Circle and a Shape
1278
1279
c.radius() * c.area()
@@ -1282,10 +1283,10 @@ fn radius_times_area<T: Circle>(c: T) -> float {
1282
1283
Likewise, supertrait methods may also be called on trait objects.
1283
1284
1284
1285
~~~ {.xfail-test}
1285
- # trait Shape { fn area(&self ) -> float; }
1286
- # trait Circle : Shape { fn radius(&self ) -> float; }
1287
- # impl Shape for int { fn area(&self ) -> float { 0.0 } }
1288
- # impl Circle for int { fn radius(&self ) -> float { 0.0 } }
1286
+ # trait Shape { fn area() -> float; }
1287
+ # trait Circle : Shape { fn radius() -> float; }
1288
+ # impl Shape for int { fn area() -> float { 0.0 } }
1289
+ # impl Circle for int { fn radius() -> float { 0.0 } }
1289
1290
# let mycircle = 0;
1290
1291
1291
1292
let mycircle: Circle = @mycircle as @Circle;
@@ -1302,7 +1303,7 @@ Implementations are defined with the keyword `impl`.
1302
1303
# struct Point {x: float, y: float};
1303
1304
# type Surface = int;
1304
1305
# struct BoundingBox {x: float, y: float, width: float, height: float};
1305
- # trait Shape { fn draw(&self, Surface); fn bounding_box(&self ) -> BoundingBox; }
1306
+ # trait Shape { fn draw(Surface); fn bounding_box() -> BoundingBox; }
1306
1307
# fn do_draw_circle(s: Surface, c: Circle) { }
1307
1308
1308
1309
struct Circle {
@@ -1311,8 +1312,8 @@ struct Circle {
1311
1312
}
1312
1313
1313
1314
impl Shape for Circle {
1314
- fn draw(&self, s: Surface) { do_draw_circle(s, * self); }
1315
- fn bounding_box(&self ) -> BoundingBox {
1315
+ fn draw(s: Surface) { do_draw_circle(s, self); }
1316
+ fn bounding_box() -> BoundingBox {
1316
1317
let r = self.radius;
1317
1318
BoundingBox{x: self.center.x - r, y: self.center.y - r,
1318
1319
width: 2.0 * r, height: 2.0 * r}
@@ -2073,7 +2074,7 @@ and moving values from the environment into the lambda expression's captured env
2073
2074
An example of a lambda expression:
2074
2075
2075
2076
~~~~
2076
- fn ten_times(f: & fn(int)) {
2077
+ fn ten_times(f: fn(int)) {
2077
2078
let mut i = 0;
2078
2079
while i < 10 {
2079
2080
f(i);
@@ -2176,7 +2177,7 @@ If the `expr` is a [field expression](#field-expressions), it is parsed as thoug
2176
2177
In this example, both calls to ` f ` are equivalent:
2177
2178
2178
2179
~~~~
2179
- # fn f(f: & fn(int)) { }
2180
+ # fn f(f: fn(int)) { }
2180
2181
# fn g(i: int) { }
2181
2182
2182
2183
f(|j| g(j));
@@ -2396,6 +2397,58 @@ fn max(a: int, b: int) -> int {
2396
2397
}
2397
2398
~~~~
2398
2399
2400
+ ### Log expressions
2401
+
2402
+ ~~~~~~~~ {.ebnf .gram}
2403
+ log_expr : "log" '(' level ',' expr ')' ;
2404
+ ~~~~~~~~
2405
+
2406
+ Evaluating a ` log ` expression may, depending on runtime configuration, cause a
2407
+ value to be appended to an internal diagnostic logging buffer provided by the
2408
+ runtime or emitted to a system console. Log expressions are enabled or
2409
+ disabled dynamically at run-time on a per-task and per-item basis. See
2410
+ [ logging system] ( #logging-system ) .
2411
+
2412
+ Each ` log ` expression must be provided with a * level* argument in
2413
+ addition to the value to log. The logging level is a ` u32 ` value, where
2414
+ lower levels indicate more-urgent levels of logging. By default, the lowest
2415
+ four logging levels (` 1_u32 ... 4_u32 ` ) are predefined as the constants
2416
+ ` error ` , ` warn ` , ` info ` and ` debug ` in the ` core ` library.
2417
+
2418
+ Additionally, the macros ` error! ` , ` warn! ` , ` info! ` and ` debug! ` are defined
2419
+ in the default syntax-extension namespace. These expand into calls to the
2420
+ logging facility composed with calls to the ` fmt! ` string formatting
2421
+ syntax-extension.
2422
+
2423
+ The following examples all produce the same output, logged at the ` error `
2424
+ logging level:
2425
+
2426
+ ~~~~
2427
+ # let filename = "bulbasaur";
2428
+
2429
+ // Full version, logging a value.
2430
+ log(core::error, ~"file not found: " + filename);
2431
+
2432
+ // Log-level abbreviated, since core::* is used by default.
2433
+ log(error, ~"file not found: " + filename);
2434
+
2435
+ // Formatting the message using a format-string and fmt!
2436
+ log(error, fmt!("file not found: %s", filename));
2437
+
2438
+ // Using the error! macro, that expands to the previous call.
2439
+ error!("file not found: %s", filename);
2440
+ ~~~~
2441
+
2442
+ A ` log ` expression is * not evaluated* when logging at the specified logging-level, module or task is disabled at runtime.
2443
+ This makes inactive ` log ` expressions very cheap;
2444
+ they should be used extensively in Rust code, as diagnostic aids,
2445
+ as they add little overhead beyond a single integer-compare and branch at runtime.
2446
+
2447
+ Logging is presently implemented as a language built-in feature,
2448
+ as it makes use of compiler-provided, per-module data tables and flags.
2449
+ In the future, logging will move into a library, and will no longer be a core expression type.
2450
+ It is therefore recommended to use the macro forms of logging (` error! ` , ` debug! ` , etc.) to minimize disruption in code that uses logging.
2451
+
2399
2452
2400
2453
# Type system
2401
2454
@@ -2678,11 +2731,11 @@ An example of an object type:
2678
2731
2679
2732
~~~~~~~~
2680
2733
trait Printable {
2681
- fn to_str(&self ) -> ~str;
2734
+ fn to_str() -> ~str;
2682
2735
}
2683
2736
2684
2737
impl Printable for int {
2685
- fn to_str(&self ) -> ~str { int::to_str(* self) }
2738
+ fn to_str() -> ~str { int::to_str(self) }
2686
2739
}
2687
2740
2688
2741
fn print(a: @Printable) {
@@ -2702,7 +2755,7 @@ and the cast expression in `main`.
2702
2755
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
2703
2756
2704
2757
~~~~~~~
2705
- fn map<A: Copy, B: Copy>(f: & fn(A) -> B, xs: &[A]) -> ~[B] {
2758
+ fn map<A: Copy, B: Copy>(f: fn(A) -> B, xs: &[A]) -> ~[B] {
2706
2759
if xs.len() == 0 { return ~[]; }
2707
2760
let first: B = f(xs[0]);
2708
2761
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
@@ -2721,11 +2774,11 @@ example, in:
2721
2774
2722
2775
~~~~~~~~
2723
2776
trait Printable {
2724
- fn make_string(&self ) -> ~str;
2777
+ fn make_string() -> ~str;
2725
2778
}
2726
2779
2727
2780
impl Printable for ~str {
2728
- fn make_string(&self ) -> ~str { copy * self }
2781
+ fn make_string() -> ~str { copy self }
2729
2782
}
2730
2783
~~~~~~~~
2731
2784
@@ -3096,7 +3149,7 @@ communication facilities.
3096
3149
3097
3150
The runtime contains a system for directing [ logging
3098
3151
expressions] ( #log-expressions ) to a logging console and/or internal logging
3099
- buffers. Logging can be enabled per module.
3152
+ buffers. Logging expressions can be enabled per module.
3100
3153
3101
3154
Logging output is enabled by setting the ` RUST_LOG ` environment
3102
3155
variable. ` RUST_LOG ` accepts a logging specification made up of a
0 commit comments