我是redstone 发表于 2025-8-1 14:57:05

Rust Macro 的小实验... Rust 真的好坏啊



无奖竞猜

lib.rs

use proc_macro::TokenStream;

#
pub fn throw_into_toilet(item: TokenStream) -> TokenStream {

    // Parse the name
    let name = item.to_string();

    // Return the token stream that defines the function
    format!("let flush = |_| (); flush({})", name).parse().unwrap()
}

#
pub fn make_toliet(_: TokenStream) -> TokenStream {
    // This macro doesn't do anything in this context, but you can define it if needed
    "let flush = |_| (); ".parse().unwrap()
}

#
pub fn make_flush(item: TokenStream) -> TokenStream {
    // Parse the name
    let name = item.to_string();

    // Return the token stream that defines the function
    format!("flush({})", name).parse().unwrap()
}


可以运行的 main 代码
main.rs

use proc2_toliet::*;

fn main() {
    let a = 1;
    throw_into_toilet!(a);

    let b = String::from("Poopoo");
    throw_into_toilet!(b);

    // make_toliet!();

    // let a = 1;
    // make_flush!(a);

    // let b = String::from("A Lot Of Poo Poo");
    // make_flush!(b);

}


不可以运行的 main.rs

use proc2_toliet::*;

fn main() {
    // let a = 1;
    // throw_into_toilet!(a);

    // let b = String::from("Poopoo");
    // throw_into_toilet!(b);

    make_toliet!();

    let a = 1;
    make_flush!(a);

    let b = String::from("A Lot Of Poo Poo");
    make_flush!(b);

}


为什么后面的代码跑不起来呢?

【1】第一次代码杂鱼主人用了明明两个厕所,后面的那次杂鱼主人居然两次工作都交给同一个厕所简直太坏了!
【2】啊啊啊,主人的粑粑太多了... 厕所再冲... 要坏掉的❤️ 厕所溢出来了呜呜呜... 所以就跑不起来了呜呜呜
【3】杂鱼主人的屁屁是不是干了什么不干净的事情,居然要上这么多次厕所!厕所当然不会让主人冲的
【4】其他

JerrySu5379 发表于 2025-8-1 15:38:58

孩子是坏喵
页: [1]
查看完整版本: Rust Macro 的小实验... Rust 真的好坏啊