Skip to content

Commit afb5fcd

Browse files
committed
auto merge of #17843 : coffeejunk/rust/guide-macros, r=steveklabnik
The old version switched in between examples from the value `5i` to `"Hello"` and back. Additionally, the code generated by `rustc print.rs --pretty=expanded` is not as verbose anymore.
2 parents c588e40 + e656aff commit afb5fcd

File tree

1 file changed

+13
-55
lines changed

1 file changed

+13
-55
lines changed

src/doc/guide.md

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5212,8 +5212,8 @@ We can check this out using a special flag to `rustc`. This code, in a file
52125212

52135213
```{rust}
52145214
fn main() {
5215-
let x = "Hello";
5216-
println!("x is: {:s}", x);
5215+
let x = 5i;
5216+
println!("x is: {}", x);
52175217
}
52185218
```
52195219

@@ -5225,32 +5225,19 @@ give us this huge result:
52255225
#![no_std]
52265226
#![feature(globs)]
52275227
#[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]
52305231
use std::prelude::*;
52315232
fn main() {
5232-
let x = "Hello";
5233+
let x = 5i;
52335234
match (&x,) {
52345235
(__arg0,) => {
52355236
#[inline]
52365237
#[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: "];
52525239
let __args_vec =
5253-
&[::std::fmt::argument(::std::fmt::secret_string, __arg0)];
5240+
&[::std::fmt::argument(::std::fmt::secret_show, __arg0)];
52545241
let __args =
52555242
unsafe {
52565243
::std::fmt::Arguments::new(__STATIC_FMTSTR, __args_vec)
@@ -5261,45 +5248,16 @@ fn main() {
52615248
}
52625249
```
52635250

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-
52935251
Whew! This isn't too terrible. You can see that we still `let x = 5i`,
52945252
but then things get a little bit hairy. Three more bindings get set: a
52955253
static format string, an argument vector, and the arguments. We then
52965254
invoke the `println_args` function with the generated arguments.
52975255

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`.
53035261

53045262
For more on macros, please consult [the Macros Guide](guide-macros.html).
53055263
Macros are a very advanced and still slightly experimental feature, but don't

0 commit comments

Comments
 (0)