【发布时间】:2020-05-07 04:47:45
【问题描述】:
如果可能的话,我如何使用带有 @ 字符的装饰器在我的结构上实现特征时具有 python 风格的味道?
trait Animal {
fn get_name(&self) -> &str {
&self.name
}
}
trait Felidae {
fn to_claw(&self) -> &str;{
println!("i can scratch off !");
}
}
struct Dog {
name: String,
}
struct Cat {
name: String,
}
@Animal
Dog
@Animal,Felidae
Cat
【问题讨论】:
-
我不知道 rust,但这看起来也不像是 Python 装饰器。
-
你不能。与装饰器“最接近”的事情可能是添加自定义派生:doc.rust-lang.org/edition-guide/rust-2018/macros/…
标签: python struct rust decorator traits