1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/// # 宏系统: 声明宏
///
/// Basic usage:
///
/// ```rust
/// macro_rules! unless {
///     ($arg:expr, $branch:expr) => ( if !$arg { $branch };); 
/// } 
/// fn cmp(a: i32, b: i32) {
///     unless!( a > b, {
///         println!("{} < {}", a, b);
///     });
/// }
/// fn main() {
///    let (a, b) = (1, 2);
///    cmp(a, b);
/// }
/// ```
/// 
///
/// Basic usage: 使用宏2.0语法,定义unless!宏
///
/// ```
/// #![feature(decl_macro)]
/// 
/// macro unless($arg:expr, $branch:expr) {
///     ( if !$arg { $branch }); 
/// } 
/// fn cmp(a: i32, b: i32) {
///     unless!( a > b, {
///         println!("{} < {}", a, b);
///     });
/// }
/// fn main() {
///    let (a, b) = (1, 2);
///    cmp(a, b);
/// }
/// ```
///
/// Basic usage: 如何实现hashmap!宏 V1.0
///
/// 使用 
///   
///   `cargo rustc -- -Z unstable-options --pretty=expanded`
/// 
/// 或
/// 
///   `rustc -Z unstable-options --pretty=expanded main.rs `
/// 
/// 命令查看宏展开代码
/// 
/// ```rust
/// 
/// macro_rules! hashmap {
///     ($($key:expr => $value:expr),* ) => {
///         {
///             let mut _map = ::std::collections::HashMap::new();
///             $(  
///                 _map.insert($key, $value); 
///             )*
///             _map
///         }
///     };
/// }
/// fn main(){
///     let map = hashmap!{
///         "a" => 1,
///         "b" => 2
///       //  "c" => 3, // V1.0不支持结尾有逗号
///     };
///     assert_eq!(map["a"], 1);
/// }
/// ```
/// 
/// 
/// Basic usage:  V2.0 - hashmap!递归调用消去最后键值对的结尾逗号
/// 
/// ```rust
/// macro_rules! hashmap {
///     ($($key:expr => $value:expr,)*) =>
///         {  hashmap!($($key => $value),*) };
///     ($($key:expr => $value:expr),* ) => {
///         {
///             let mut _map = ::std::collections::HashMap::new();
///             $(
///                 _map.insert($key, $value);
///             )*
///            _map
///        }
///    };
/// }
/// fn main(){
///     let map = hashmap!{
///         "a" => 1,
///         "b" => 2,
///         "c" => 3, 
///     };
///     assert_eq!(map["a"], 1);
/// }
/// ```
/// 
/// Basic usage:  V3.0 - 利用重复匹配规则
/// 
/// ```rust
/// macro_rules! hashmap {
///     ($($key:expr => $value:expr),* $(,)*) => {
///         {
///             let mut _map = ::std::collections::HashMap::new();
///             $(
///                 _map.insert($key, $value);
///             )*
///             _map
///         }
///    };
/// }
/// fn main(){
///     let map = hashmap!{
///         "a" => 1,
///         "b" => 2,
///         "c" => 3, 
///     };
///     assert_eq!(map["a"], 1);
/// }
/// ```
/// 
/// Basic usage:  V4.0 - 如何为hashmap!宏添加根据给定的键值对数量预分配空间的功能?
/// 
/// ```rust
/// macro_rules! unit {
///     ($($x:tt)*) => (());
/// }
/// macro_rules! count {
///     ($($key:expr),*) => (<[()]>::len(&[$(unit!($key)),*]));
/// }
/// macro_rules! hashmap {
///     ($($key:expr => $value:expr),* $(,)*) => {
///         {
///            let _cap = count!($($key),*);
///            let mut _map 
///                = ::std::collections::HashMap::with_capacity(_cap);
///            $(
///                _map.insert($key, $value);
///            )*
///            _map
///        }
///    };
/// }
/// fn main(){
///    let map = hashmap!{
///        "a" => 1,
///        "b" => 2,
///        "c" => 3, 
///    };
///    assert_eq!(map["a"], 1);
/// }
/// ```
/// 
/// Basic usage:  V5.0 - 重构V4.0版hashmap!宏,消除外部宏unit!和count!
/// 
/// ```rust
/// macro_rules! hashmap {
///     (@unit $($x:tt)*) => (());
///     (@count $($rest:expr),*) => 
///         (<[()]>::len(&[$(hashmap!(@unit $rest)),*]));
///     ($($key:expr => $value:expr),* $(,)*) => {
///         {
///             let _cap = hashmap!(@count $($key),*);
///             let mut _map = 
///                 ::std::collections::HashMap::with_capacity(_cap);
///            $(
///                _map.insert($key, $value);
///            )*
///            _map
///        }
///    };
/// }
/// fn main(){
///    let map = hashmap!{
///        "a" => 1,
///        "b" => 2,
///        "c" => 3, 
///    };
///    assert_eq!(map["a"], 1);
/// }
/// ```
/// 
/// 
/// Basic usage: 使用`#![feature(trace_macros)]`特性调试hashmap!宏
/// 
/// ```rust
/// #![feature(trace_macros)]
/// macro_rules! hashmap {
///     (@unit $($x:tt)*) => (());
///     (@count $($rest:expr),*) => 
///         (<[()]>::len(&[$(hashmap!(@unit $rest)),*]));
///     ($($key:expr => $value:expr),* $(,)*) => {
///         {
///             let _cap = hashmap!(@count $($key),*);
///             let mut _map = 
///                 ::std::collections::HashMap::with_capacity(_cap);
///            $(
///                _map.insert($key, $value);
///            )*
///            _map
///        }
///    };
/// }
/// fn main(){
///    trace_macros!(true);
///    let map = hashmap!{
///        "a" => 1,
///        "b" => 2,
///        "c" => 3, 
///    };
///    assert_eq!(map["a"], 1);
/// }
/// ```
/// # 宏导入导出
///
/// 请查阅源码中的相关crate
///   - hashmap_lite
pub fn declarative_macros(){
    unimplemented!();
}


/// # 宏系统: 声明宏的卫生性
///
/// Basic usage: 展示宏的卫生性
///
/// 该代码报错
/// 
/// ```
/// macro_rules! sum {
///     ($e:expr) => ({
///         let a = 2;
///         $e + a
///     })
/// }
/// fn main(){
///     // error[E0425]: cannot find value `a` in this scope
///     let four = sum!(a);
/// }
/// ```
/// 
/// 假想中sum!宏会生成的代码
/// 
/// ```rust
/// // fn main(){
/// //     let four = {
/// //         let a = 2;
/// // 	       a + a
/// // 	   };
/// // }
/// ```
/// 
/// 说明:
/// 
/// > 宏定义中的变量a和外部传入的同名a,Rust会识别它们为两个不同的变量.
/// > 宏定义中的作用域是独立的。但是目前Rust的卫生性并不完善,生命周期、类型等都无法保证卫生性。
/// 
pub fn hygienic_test(){
    unimplemented!();
}


/// # 宏系统: 过程宏
///
/// 请查阅源码中的相关crate
///   - simple_proc_macro
///   - derive-new (支持syn 0.15 和 quote 0.6)
/// 
/// 
pub fn proc_macros(){
    unimplemented!();
}

/// # 宏系统: 编译器插件
///
/// 请查阅源码中的相关crate
///   - plugin_demo
/// 
/// 
pub fn plugin_demo(){
    unimplemented!();
}