提交 5f100699 编写于 作者: 梦境迷离's avatar 梦境迷离

add Marco ,时间:2020-01-27 11:47:45

上级 032a345e
/// 标准库的定义
///#[cfg(not(test))]
///#[macro_export]
///#[stable(feature = "rust1", since = "1.0.0")]
///#[allow_internal_unstable(box_syntax)]
///macro_rules! vec {
/// ($elem:expr; $n:expr) => (
/// $crate::vec::from_elem($elem, $n)
/// );
/// ($($x:expr),*) => (
/// <[_]>::into_vec(box [$($x),*])
/// );
/// ($($x:expr,)*) => ($crate::vec![$($x),*])
///}
///
///
#[macro_export]//#[macro_export]注解(注释)表示,只要将定义了该宏的板条箱放入范围内,就应使该宏可用。没有此注释,宏将无法进入范围。
macro_rules! Vec {//然后,我们从macro_rules! 开始宏定义!以及我们定义的不带感叹号的宏的名称。该名称(在本例中为Vec)后跟大括号,表示宏定义的正文。
( $( $x:expr ),* ) => {//模式与代码块,宏模式是针对Rust代码结构而非值进行匹配的
//首先一组括号()包括整个模式,这些括号捕获与括号内的模式匹配的值,以供替换代码使用
//$()中是$x:expr,它与任何Rust表达式匹配,并为表达式指定名称$x
//*指定该模式与*之前的零个或多个匹配
{
let mut temp_vec = Vec::new();
//$()* 表示匹配0次或多次(等价为每个匹配到的表达式执行下面操作),$x表示匹配上的表达式(用户传进来的)
$(
temp_vec.push($x);
)*
temp_vec//最终返回集合
}
};
}
pub fn marco_function() {
///定义自己的宏。(标准库的是vec!)
let v: Vec<u32> = Vec![1, 2, 3];// $x 匹配 1,2,3 三次
println!("{}", v.len())//宏定义必须在前面
}
\ No newline at end of file
......@@ -19,6 +19,7 @@ use enum_data_type::*;
use example_guessing_game::example_guessing_game;
use generic_traits_lifetimes::*;
use iterator_demonstration::*;
use macro_syntax::*;
use match_syntax::*;
use method_syntax::*;
use other_function::*;
......@@ -47,6 +48,7 @@ pub mod control_function;
pub mod other_function;
pub mod closures_syntax;
pub mod smart_point;
pub mod macro_syntax;
/// 引用和借用:https://dreamylost.cn/rust/Rust-Rust%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%BC%95%E7%94%A8%E4%B8%8E%E5%80%9F%E7%94%A8.html
/// 所有权:https://dreamylost.cn/rust/Rust-%E6%89%80%E6%9C%89%E6%9D%83.html
......@@ -127,5 +129,7 @@ fn main() {
println!("====================");
mutex_multi_thread();
println!("====================");
marco_function();
println!("====================");
//example_guessing_game();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册