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