@@ -1897,157 +1897,6 @@ This makes it possible to rebind a variable without actually mutating
1897
1897
it, which is mostly useful for destructuring (which can rebind, but
1898
1898
not assign).
1899
1899
1900
- # Macros
1901
-
1902
- Functions are the programmer's primary tool of abstraction, but there are
1903
- cases in which they are insufficient, because the programmer wants to
1904
- abstract over concepts not represented as values. Consider the following
1905
- example:
1906
-
1907
- ~~~~
1908
- # enum t { special_a(uint), special_b(uint) };
1909
- # fn f() -> uint {
1910
- # let input_1 = special_a(0), input_2 = special_a(0);
1911
- match input_1 {
1912
- special_a(x) => { return x; }
1913
- _ => {}
1914
- }
1915
- // ...
1916
- match input_2 {
1917
- special_b(x) => { return x; }
1918
- _ => {}
1919
- }
1920
- # return 0u;
1921
- # }
1922
- ~~~~
1923
-
1924
- This code could become tiresome if repeated many times. However, there is
1925
- no reasonable function that could be written to solve this problem. In such a
1926
- case, it's possible to define a macro to solve the problem. Macros are
1927
- lightweight custom syntax extensions, themselves defined using the
1928
- ` macro_rules! ` syntax extension:
1929
-
1930
- ~~~~
1931
- # enum t { special_a(uint), special_b(uint) };
1932
- # fn f() -> uint {
1933
- # let input_1 = special_a(0), input_2 = special_a(0);
1934
- macro_rules! early_return(
1935
- ($inp:expr $sp:ident) => ( //invoke it like `(input_5 special_e)`
1936
- match $inp {
1937
- $sp(x) => { return x; }
1938
- _ => {}
1939
- }
1940
- );
1941
- );
1942
- // ...
1943
- early_return!(input_1 special_a);
1944
- // ...
1945
- early_return!(input_2 special_b);
1946
- # return 0;
1947
- # }
1948
- ~~~~
1949
-
1950
- Macros are defined in pattern-matching style:
1951
-
1952
- ## Invocation syntax
1953
-
1954
- On the left-hand-side of the ` => ` is the macro invocation syntax. It is
1955
- free-form, excepting the following rules:
1956
-
1957
- 1 . It must be surrounded in parentheses.
1958
- 2 . ` $ ` has special meaning.
1959
- 3 . The ` () ` s, ` [] ` s, and ` {} ` s it contains must balance. For example, ` ([) ` is
1960
- forbidden.
1961
-
1962
- To take as an argument a fragment of Rust code, write ` $ ` followed by a name
1963
- (for use on the right-hand side), followed by a ` : ` , followed by the sort of
1964
- fragment to match (the most common ones are ` ident ` , ` expr ` , ` ty ` , ` pat ` , and
1965
- ` block ` ). Anything not preceeded by a ` $ ` is taken literally. The standard
1966
- rules of tokenization apply,
1967
-
1968
- So ` ($x:ident => (($e:expr))) ` , though excessively fancy, would create a macro
1969
- that could be invoked like ` my_macro!(i=>(( 2+2 ))) ` .
1970
-
1971
- ## Transcription syntax
1972
-
1973
- The right-hand side of the ` => ` follows the same rules as the left-hand side,
1974
- except that ` $ ` need only be followed by the name of the syntactic fragment
1975
- to transcribe.
1976
-
1977
- The right-hand side must be surrounded by delimiters of some kind, and must be
1978
- an expression; currently, user-defined macros can only be invoked in
1979
- expression position (even though ` macro_rules! ` itself can be in item
1980
- position).
1981
-
1982
- ## Multiplicity
1983
-
1984
- ### Invocation
1985
-
1986
- Going back to the motivating example, suppose that we wanted each invocation
1987
- of ` early_return ` to potentially accept multiple "special" identifiers. The
1988
- syntax ` $(...)* ` accepts zero or more occurences of its contents, much like
1989
- the Kleene star operator in regular expressions. It also supports a separator
1990
- token (a comma-separated list could be written ` $(...),* ` ), and ` + ` instead of
1991
- ` * ` to mean "at least one".
1992
-
1993
- ~~~~
1994
- # enum t { special_a(uint),special_b(uint),special_c(uint),special_d(uint)};
1995
- # fn f() -> uint {
1996
- # let input_1 = special_a(0), input_2 = special_a(0);
1997
- macro_rules! early_return(
1998
- ($inp:expr, [ $($sp:ident)|+ ]) => (
1999
- match $inp {
2000
- $(
2001
- $sp(x) => { return x; }
2002
- )+
2003
- _ => {}
2004
- }
2005
- );
2006
- );
2007
- // ...
2008
- early_return!(input_1, [special_a|special_c|special_d]);
2009
- // ...
2010
- early_return!(input_2, [special_b]);
2011
- # return 0;
2012
- # }
2013
- ~~~~
2014
-
2015
- ### Transcription
2016
-
2017
- As the above example demonstrates, ` $(...)* ` is also valid on the right-hand
2018
- side of a macro definition. The behavior of Kleene star in transcription,
2019
- especially in cases where multiple stars are nested, and multiple different
2020
- names are involved, can seem somewhat magical and intuitive at first. The
2021
- system that interprets them is called "Macro By Example". The two rules to
2022
- keep in mind are (1) the behavior of ` $(...)* ` is to walk through one "layer"
2023
- of repetitions for all of the ` $name ` s it contains in lockstep, and (2) each
2024
- ` $name ` must be under at least as many ` $(...)* ` s as it was matched against.
2025
- If it is under more, it'll will be repeated, as appropriate.
2026
-
2027
- ## Parsing limitations
2028
-
2029
- The parser used by the macro system is reasonably powerful, but the parsing of
2030
- Rust syntax is restricted in two ways:
2031
-
2032
- 1 . The parser will always parse as much as possible. For example, if the comma
2033
- were omitted from the syntax of ` early_return! ` above, ` input_1 [ ` would've
2034
- been interpreted as the beginning of an array index. In fact, invoking the
2035
- macro would have been impossible.
2036
- 2 . The parser must have eliminated all ambiguity by the time it reaches a
2037
- ` $name:fragment_specifier ` . This most often affects them when they occur in
2038
- the beginning of, or immediately after, a ` $(...)* ` ; requiring a distinctive
2039
- token in front can solve the problem.
2040
-
2041
- ## A final note
2042
-
2043
- Macros, as currently implemented, are not for the faint of heart. Even
2044
- ordinary syntax errors can be more difficult to debug when they occur inside
2045
- a macro, and errors caused by parse problems in generated code can be very
2046
- tricky. Invoking the ` log_syntax! ` macro can help elucidate intermediate
2047
- states, using ` trace_macros!(true) ` will automatically print those
2048
- intermediate states out, and using ` --pretty expanded ` as an argument to the
2049
- compiler will show the result of expansion.
2050
-
2051
1900
# Traits
2052
1901
2053
1902
Traits are Rust's take on value polymorphism—the thing that
0 commit comments