小编Анд*_*ких的帖子

如何使结构可调用?

#![feature(unboxed_closures)]
#![feature(fn_traits)]

struct foo;

impl std::ops::Add for foo {
    type Output = foo;
    fn add(self, x: foo) -> foo {
        println!("Add for foo");
        x
    }
}

impl Fn for foo {
    extern "rust-call" fn call(&self) -> Self {
        println!("Call for Foo ");
        self
    }
}

fn main() {
    let x = foo;
    let y = foo;
    x + y;

    x();
}
Run Code Online (Sandbox Code Playgroud)

我实现了Add特性,但我不明白如何将结构作为函数调用.我收到错误:

error[E0243]: wrong number of type arguments: expected 1, found 0
  --> src/main.rs:14:10
   |
14 |     impl …
Run Code Online (Sandbox Code Playgroud)

implementation traits rust

11
推荐指数
2
解决办法
1223
查看次数

有没有办法捕捉未知的结构域?

struct Test;

// here must be code to overload the standard field resolution methods

fn main() {
    let t = Test;
    println!("I wanna catch request for unknown struct field {}", t.unexpected_field)
}
Run Code Online (Sandbox Code Playgroud)

struct field dynamic rust

2
推荐指数
1
解决办法
69
查看次数

标签 统计

rust ×2

dynamic ×1

field ×1

implementation ×1

struct ×1

traits ×1