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

■ macro_rules! 매크로를 사용해 HashMap 객체를 초기화하는 매크로를 만드는 방법을 보여준다.

 

▶ 예제 코드 (RS)

use std::collections;

macro_rules! map
{
    ($($key : expr => $val : expr), *) =>
    {
        {
            let mut hash_map = collections::HashMap::new();

            $(
                hash_map.insert($key, $val);
            )*

            hash_map
        }
    }
}

fn main()
{
    let hash_map : collections::HashMap<&str, &str> = map!
    [
        "mon" => "월요일",
        "tue" => "화요일",
        "wed" => "수요일",
        "thu" => "목요일",
        "fri" => "금요일",
        "sat" => "토요일",
        "sun" => "일요일"
    ];

    println!("mon = {}", hash_map["mon"]);
    println!("wed = {}", hash_map["wed"]);
}

/*
mon = 월요일
wed = 수요일
*/
728x90
그리드형(광고전용)
Posted by icodebroker
,