相关疑难解决方法(0)

是否可以在特征中使用构造函数?

我试图在特征中找到构造函数的例子,但运气不好.这是Rust的惯用之处吗?

trait A {
    fn new() -> A;
}

struct B;
impl A for B {
    fn new() -> B {
        B
    }
}

fn main() {
    println!("message")
}
Run Code Online (Sandbox Code Playgroud)
<anon>:7:8: 9:9 error: method `new` has an incompatible type for trait: expected trait A, found struct `B` [E0053]
<anon>:7        fn new() -> B {
<anon>:8          B
<anon>:9        }
<anon>:7:8: 9:9 help: see the detailed explanation for E0053
error: aborting due to previous error
playpen: application terminated with error code 101
Run Code Online (Sandbox Code Playgroud)

转换它会返回core …

rust

8
推荐指数
1
解决办法
2391
查看次数

标签 统计

rust ×1