开启辅助访问     
收藏本站

站内搜索

搜索

Minecraft(我的世界)苦力怕论坛

[闲聊] Rust Macro 的小实验... Rust 真的好坏啊

 发表于 2025-8-1 14:57:05|显示全部楼层|阅读模式 IP:上海


无奖竞猜

lib.rs

  1. use proc_macro::TokenStream;

  2. #[proc_macro]
  3. pub fn throw_into_toilet(item: TokenStream) -> TokenStream {

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

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

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

  14. #[proc_macro]
  15. pub fn make_flush(item: TokenStream) -> TokenStream {
  16.     // Parse the name
  17.     let name = item.to_string();

  18.     // Return the token stream that defines the function
  19.     format!("flush({})", name).parse().unwrap()
  20. }
复制代码


可以运行的 main 代码
main.rs

  1. use proc2_toliet::*;

  2. fn main() {
  3.     let a = 1;
  4.     throw_into_toilet!(a);

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

  7.     // make_toliet!();

  8.     // let a = 1;
  9.     // make_flush!(a);

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

  12. }
复制代码


不可以运行的 main.rs

  1. use proc2_toliet::*;

  2. fn main() {
  3.     // let a = 1;
  4.     // throw_into_toilet!(a);

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

  7.     make_toliet!();

  8.     let a = 1;
  9.     make_flush!(a);

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

  12. }
复制代码


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

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

苦力怕论坛,感谢有您~
 发表于 2025-8-1 15:38:58|显示全部楼层 IP:湖北省
孩子是坏喵
2#2025-8-1 15:38:58回复收起回复
苦力怕论坛,感谢有您~
回复支持

使用道具举报

本版积分规则

本站
关于我们
联系我们
坛史纲要
官方
哔哩哔哩
技术博客
下载
网易版
安卓版
JAVA
反馈
意见建议
教程中心
更多
捐助本站
QQ群
QQ群

QQ群

访问手机版

访问手机版

手机版|小黑屋|系统状态|klpbbs.com

| 由 木韩网络 提供支持 | GMT+8, 2026-6-18 00:44

声明:本站与Mojang以及微软公司没有从属关系

Powered by Discuz! X3.4