@@ -5212,8 +5212,8 @@ We can check this out using a special flag to `rustc`. This code, in a file
5212
5212
5213
5213
``` {rust}
5214
5214
fn main() {
5215
- let x = "Hello" ;
5216
- println!("x is: {:s }", x);
5215
+ let x = 5i ;
5216
+ println!("x is: {}", x);
5217
5217
}
5218
5218
```
5219
5219
@@ -5225,32 +5225,19 @@ give us this huge result:
5225
5225
#![no_std]
5226
5226
#![feature(globs)]
5227
5227
#[phase(plugin, link)]
5228
- extern crate std = "std";
5229
- extern crate rt = "native";
5228
+ extern crate "std" as std;
5229
+ extern crate "native" as rt;
5230
+ #[prelude_import]
5230
5231
use std::prelude::*;
5231
5232
fn main() {
5232
- let x = "Hello" ;
5233
+ let x = 5i ;
5233
5234
match (&x,) {
5234
5235
(__arg0,) => {
5235
5236
#[inline]
5236
5237
#[allow(dead_code)]
5237
- static __STATIC_FMTSTR: [::std::fmt::rt::Piece<'static>, ..2u] =
5238
- [::std::fmt::rt::String("x is: "),
5239
- ::std::fmt::rt::Argument(::std::fmt::rt::Argument{position:
5240
- ::std::fmt::rt::ArgumentNext,
5241
- format:
5242
- ::std::fmt::rt::FormatSpec{fill:
5243
- ' ',
5244
- align:
5245
- ::std::fmt::rt::AlignUnknown,
5246
- flags:
5247
- 0u,
5248
- precision:
5249
- ::std::fmt::rt::CountImplied,
5250
- width:
5251
- ::std::fmt::rt::CountImplied,},})];
5238
+ static __STATIC_FMTSTR: [&'static str, ..1u] = ["x is: "];
5252
5239
let __args_vec =
5253
- &[::std::fmt::argument(::std::fmt::secret_string , __arg0)];
5240
+ &[::std::fmt::argument(::std::fmt::secret_show , __arg0)];
5254
5241
let __args =
5255
5242
unsafe {
5256
5243
::std::fmt::Arguments::new(__STATIC_FMTSTR, __args_vec)
@@ -5261,45 +5248,16 @@ fn main() {
5261
5248
}
5262
5249
```
5263
5250
5264
- Intense. Here's a trimmed down version that's a bit easier to read:
5265
-
5266
- ``` {rust,ignore}
5267
- fn main() {
5268
- let x = 5i;
5269
- match (&x,) {
5270
- (__arg0,) => {
5271
- static __STATIC_FMTSTR: =
5272
- [String("x is: "),
5273
- Argument(Argument {
5274
- position: ArgumentNext,
5275
- format: FormatSpec {
5276
- fill: ' ',
5277
- align: AlignUnknown,
5278
- flags: 0u,
5279
- precision: CountImplied,
5280
- width: CountImplied,
5281
- },
5282
- },
5283
- ];
5284
- let __args_vec = &[argument(secret_string, __arg0)];
5285
- let __args = unsafe { Arguments::new(__STATIC_FMTSTR, __args_vec) };
5286
-
5287
- println_args(&__args)
5288
- }
5289
- };
5290
- }
5291
- ```
5292
-
5293
5251
Whew! This isn't too terrible. You can see that we still ` let x = 5i ` ,
5294
5252
but then things get a little bit hairy. Three more bindings get set: a
5295
5253
static format string, an argument vector, and the arguments. We then
5296
5254
invoke the ` println_args ` function with the generated arguments.
5297
5255
5298
- This is the code (well, the full version) that Rust actually compiles. You can
5299
- see all of the extra information that's here. We get all of the type safety and
5300
- options that it provides, but at compile time, and without needing to type all
5301
- of this out. This is how macros are powerful. Without them, you would need to
5302
- type all of this by hand to get a type checked ` println ` .
5256
+ This is the code that Rust actually compiles. You can see all of the extra
5257
+ information that's here. We get all of the type safety and options that it
5258
+ provides, but at compile time, and without needing to type all of this out.
5259
+ This is how macros are powerful. Without them, you would need to type all of
5260
+ this by hand to get a type checked ` println ` .
5303
5261
5304
5262
For more on macros, please consult [ the Macros Guide] ( guide-macros.html ) .
5305
5263
Macros are a very advanced and still slightly experimental feature, but don't
0 commit comments