首页 文章
  • 1 votes
     answers
     views

    包含任意数量类型的宏

    是否可以编写一个宏来定义包含任意数量(不同)输入类型的枚举?我想做一种类型级别的匹配 . type_switch!(i32 => println!("integer"), f32 => println!("float"), Foo => println!("foo")) 这将扩展到: { enum Wrappe...
  • 7 votes
     answers
     views

    无法在宏中使用self,因为宏扩展忽略了令牌“self”

    我想写一个打印“OK”然后在方法中返回self的宏 . 这是我的第一个宏,所以我尝试了这个,认为它会像文本替换一样,但它失败了: macro_rules! print_ok_and_return_self { () => { println!("OK"); self } } fn main() { let a = ...
  • 2 votes
     answers
     views

    是否可以使用传递给宏的项arg作为方法?

    我正在尝试创建一个生成 struct 的宏,它提供了一组传递给宏的方法 . 例如,调用: create_impl!(StructName, fn foo() -> u32 { return 432 }) 应该生成一个提供方法 foo() 的空结构 StructName . 我最初尝试使用 item 宏arg类型 . 但是,当我尝试在规则中使用 item 时,我收到以下编译器错误: err...
  • 0 votes
     answers
     views

    尝试在宏扩展中实现特征时出现宏错误

    fn a() {} 似乎满足了预期 fn 的解析规则,然后是其他一些东西 . item 应该可以是函数定义,对吗?所以他们应该工作,对吗? macro_rules! multi_impl { (for $base:ty : $($t:ty { $($i:item);* }),+) => { $( ...
  • 3 votes
     answers
     views

    宏生成代码中未使用的变量

    我已经编写了一个宏来实现类似Scala,用于Rust中的理解 . 它会变成这样: map_for!{ x <- 0..4; y = 2*x; z <- 0..1; => y+z } 进入这个: ((0..4).map (move |x| { let y = 2 * x; (x, y) })) .flat_map (move |params...
  • 0 votes
     answers
     views

    Rust中宏调用的意外结束

    我在Rust中有以下代码 use std::fmt; pub struct MyRange<Idx> { pub start: Idx, pub end: Idx, } impl fmt::Debug for MyRange<f32> { fn fmt( &self, f: &mut fmt::Formatter ) ->...
  • -3 votes
     answers
     views

    Rust Macros中相同变量的不同分隔符

    我想匹配一个模式,如: foo!(1,2,3;4,5,6;7,8,9); 将为所有数字生成相同的代码,但是当需要使用分号时,我希望运行其他代码 . 这种模式可能吗? 我试过了: macro_rule! { foo ($($x:expr),*);*) => ... 但我似乎无法在右侧进行这项工作 .
  • 2 votes
     answers
     views

    打印!宏无序执行[重复]

    这个问题在这里已有答案: Why does this read input before printing? 2个答案 我的部分代码如下所示: print_usage_instructions(); print!("Command: "); let stdin = io::stdin(); let mut line = String::new(); stdin.lock()...
  • 0 votes
     answers
     views

    如何编写自定义派生宏?

    我正在尝试在Rust中编写自己的派生模式宏,而documentation在示例中有点缺乏 . 我有一个像这样的结构: #[derive(MyMacroHere)] struct Example { id: i64, value: Option<String>, } 我希望我的宏生成一个方法àla fn set_fields(&mut self, id: i64...

热门问题