@@ -469,14 +469,16 @@ include:
469
469
* ` include! ` : include the Rust expression in the given file
470
470
* ` include_str! ` : include the contents of the given file as a string
471
471
* ` include_bin! ` : include the contents of the given file as a binary blob
472
+ * ` error! ` , ` warn! ` , ` info! ` , ` debug! ` : provide diagnostic information.
472
473
473
474
All of the above extensions, with the exception of ` proto! ` , are expressions
474
475
with values. ` proto! ` is an item, defining a new name.
475
476
476
477
## Macros
477
478
478
479
User-defined syntax extensions are called "macros", and they can be defined
479
- with the ` macro_rules! ` syntax extension.
480
+ with the ` macro_rules! ` syntax extension. User-defined macros can currently
481
+ only be invoked in expression position.
480
482
481
483
~~~~ {.ebnf .gram}
482
484
expr_macro_rules : "macro_rules" '!' ident '(' macro_rule * ')'
@@ -2325,9 +2327,9 @@ lower levels indicate more-urgent levels of logging. By default, the lowest
2325
2327
four logging levels (` 0_u32 ... 3_u32 ` ) are predefined as the constants
2326
2328
` error ` , ` warn ` , ` info ` and ` debug ` in the ` core ` library.
2327
2329
2328
- Additionally, the macros ` # error` , ` # warn` , ` # info` and ` # debug` are defined
2330
+ Additionally, the macros ` error! ` , ` warn! ` , ` info! ` and ` debug! ` are defined
2329
2331
in the default syntax-extension namespace. These expand into calls to the
2330
- logging facility composed with calls to the ` # fmt` string formatting
2332
+ logging facility composed with calls to the ` fmt! ` string formatting
2331
2333
syntax-extension.
2332
2334
2333
2335
The following examples all produce the same output, logged at the ` error `
@@ -2377,84 +2379,6 @@ use of the knowledge that the condition holds if the program continues to
2377
2379
execute after the ` assert ` .
2378
2380
2379
2381
2380
- ### Syntax extension expressions
2381
-
2382
- ~~~~~~~~ {.abnf .gram}
2383
- syntax_ext_expr : '#' ident paren_expr_list ? brace_match ? ;
2384
- ~~~~~~~~
2385
-
2386
- Rust provides a notation for _ syntax extension_ . The notation for invoking
2387
- a syntax extension is a marked syntactic form that can appear as an expression
2388
- in the body of a Rust program.
2389
-
2390
- After parsing, a syntax-extension invocation is expanded into a Rust
2391
- expression. The name of the extension determines the translation performed. In
2392
- future versions of Rust, user-provided syntax extensions aside from macros
2393
- will be provided via external crates.
2394
-
2395
- At present, only a set of built-in syntax extensions, as well as macros
2396
- introduced inline in source code using the ` macro ` extension, may be used. The
2397
- current built-in syntax extensions are:
2398
-
2399
-
2400
- * ` fmt ` expands into code to produce a formatted string, similar to
2401
- ` printf ` from C.
2402
- * ` env ` expands into a string literal containing the value of that
2403
- environment variable at compile-time.
2404
- * ` concat_idents ` expands into an identifier which is the
2405
- concatenation of its arguments.
2406
- * ` ident_to_str ` expands into a string literal containing the name of
2407
- its argument (which must be a literal).
2408
- * ` log_syntax ` causes the compiler to pretty-print its arguments.
2409
-
2410
-
2411
- Finally, ` macro ` is used to define a new macro. A macro can abstract over
2412
- second-class Rust concepts that are present in syntax. The arguments to
2413
- ` macro ` are pairs (two-element vectors). The pairs consist of an invocation
2414
- and the syntax to expand into. An example:
2415
-
2416
- ~~~~~~~~ {.xfail-test}
2417
- #macro([#apply[fn, [args, ...]], fn(args, ...)]);
2418
- ~~~~~~~~
2419
-
2420
- In this case, the invocation ` apply!(sum, 5, 8, 6) ` expands to
2421
- ` sum(5,8,6) ` . If ` ... ` follows an expression (which need not be as
2422
- simple as a single identifier) in the input syntax, the matcher will expect an
2423
- arbitrary number of occurrences of the thing preceding it, and bind syntax to
2424
- the identifiers it contains. If it follows an expression in the output syntax,
2425
- it will transcribe that expression repeatedly, according to the identifiers
2426
- (bound to syntax) that it contains.
2427
-
2428
- The behaviour of ` ... ` is known as Macro By Example. It allows you to
2429
- write a macro with arbitrary repetition by specifying only one case of that
2430
- repetition, and following it by ` ... ` , both where the repeated input is
2431
- matched, and where the repeated output must be transcribed. A more
2432
- sophisticated example:
2433
-
2434
-
2435
- ~~~~~~~~ {.xfail-test}
2436
- #macro([#zip_literals[[x, ...], [y, ...]), [[x, y], ...]]);
2437
- #macro([#unzip_literals[[x, y], ...], [[x, ...], [y, ...]]]);
2438
- ~~~~~~~~
2439
-
2440
- In this case, ` zip_literals!([1,2,3], [1,2,3]) ` expands to
2441
- ` [[1,1],[2,2],[3,3]] ` , and ` unzip_literals!([1,1], [2,2], [3,3]) `
2442
- expands to ` [[1,2,3],[1,2,3]] ` .
2443
-
2444
- Macro expansion takes place outside-in: that is,
2445
- ` unzip_literals!(zip_literals!([1,2,3],[1,2,3])) ` will fail because
2446
- ` unzip_literals ` expects a list, not a macro invocation, as an argument.
2447
-
2448
- The macro system currently has some limitations. It's not possible to
2449
- destructure anything other than vector literals (therefore, the arguments to
2450
- complicated macros will tend to be an ocean of square brackets). Macro
2451
- invocations and ` ... ` can only appear in expression positions. Finally,
2452
- macro expansion is currently unhygienic. That is, name collisions between
2453
- macro-generated and user-written code can cause unintentional capture.
2454
-
2455
- Future versions of Rust will address these issues.
2456
-
2457
-
2458
2382
# Type system
2459
2383
2460
2384
## Types
0 commit comments