官术网_书友最值得收藏!

Macros

Macro rules, also called macros by example, are a way to avoid code duplication by generating code at compile time. We will implement a simple macro to implement our BitSet trait for integer types:

macro_rules! int_bitset {
    ($ty:ty) => {
        impl BitSet for $ty {
            fn clear(&mut self, index: usize) {
                *self &= !(1 << index);
            }

            fn is_set(&self, index: usize) -> bool {
                (*self >> index) & 1 == 1
            }

            fn set(&mut self, index: usize) {
                *self |= 1 << index;
            }
        }
    };
}

The name of the int_bitset macro is written after macro_rules!. A macro can have multiple rules, similar to match arms, but it matches on Rust syntactic elements instead, with types, expressions, blocks of code, and so on. Here we only have one rule and it matches against a single type since we use :ty. The part before :ty ($ty) is the name for the element that was matched. Inside the curly brackets, after the => symbol, we see the actual code that will be generated. It is the same as our previous implementation of BitSet for u64, except that it uses the meta-variable $ty instead of u64.

To avoid a lot of boilerplate code, we can then use this macro as follows:

int_bitset!(i32);
int_bitset!(u8);
int_bitset!(u64);
主站蜘蛛池模板: 古蔺县| 河东区| 深州市| 阳高县| 城固县| 准格尔旗| 定结县| 平阴县| 红河县| 乐清市| 蓬安县| 额尔古纳市| 新和县| 屏东县| 杨浦区| 咸宁市| 西畴县| 东兰县| 嘉峪关市| 桦川县| 安远县| 阳西县| 获嘉县| 吉林市| 拜泉县| 图们市| 吕梁市| 类乌齐县| 新龙县| 江永县| 铁岭县| 沙河市| 清涧县| 瑞金市| 札达县| 灌云县| 祁连县| 黄浦区| 天镇县| 柳江县| 长岭县|