0xe3aad

0xe3aad

Implementing Helang using Rust 😅

Introduction#

Recently, many users on Bilibili have created some interesting derivatives of He Tongxue's erroneous code, which has even made it to the Github Trending page:

vsZ7DO.png


Among them, someone used macros in C++ to implement He Tongxue's "or operation", which inspired me to implement it using macros in Rust.

Source Code#

fn parse_to_vec(nums: &str) -> Vec<usize> {
    let nums = nums.replace(' ', "");
    nums.split('|').map(|x| x.parse().unwrap()).collect()
}

fn power_con(powers: &mut [u8], nums: &[usize], power: u8) {
    for &num in nums {
        powers[num] = power;
    }
}

#[macro_export]
macro_rules! powerCon {
    ($powers: expr, $nums: expr, $force: expr) => {
        power_con($powers, &parse_to_vec(stringify!($nums)), $force)
    };
}

fn main() {
    let mut powers = [0_u8; 68];
    powerCon!(&mut powers, 0, 100);
    powerCon!(&mut powers, 1 | 2 | 6 | 7 | 11 | 52 | 57 | 58 | 65, 10);
    println!("{:?}", powers)
}

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.