첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ macro_rules! 매크로를 사용해 재귀 호출 매크로를 만드는 방법을 보여준다.

 

▶ 예제 코드 (RS)

macro_rules! print_html
{
    () => {()};

    ($e : tt) => {print!("{}", $e)};

    ($tag : ident [$($inner : tt)*] $($rest : tt)*) =>
    {
        {
            print!("<{}>", stringify!($tag));

            print_html!($($inner)*);

            println!("</{}>", stringify!($tag));

            print_html!($($rest)*);
        }
    };
}

fn main()
{
    print_html!
    (
        html
        [
            head[title["test"]]
            body
            [
                h1["test"]
                p ["This is test."]
            ]
        ]
    );
}

/*
<html><head><title>test</title>
</head>
<body><h1>test</h1>
<p>This is test.</p>
</body>
</html>
*/
728x90
그리드형(광고전용)
Posted by icodebroker
,