为什么使用涡轮鱼会给"wrong number of type arguments"?

  • 本文关键字:number wrong of type arguments rust
  • 更新时间 :
  • 英文 :


这有效:

use std::error::Error;
fn main() {
let _: Box<dyn Error> = "test".into();
}

但这给出了一个错误:

use std::error::Error;
fn main() {
let _ = "test".into::<Box<dyn Error>>();
}
error[E0107]: wrong number of type arguments: expected 0, found 1
--> src/main.rs:4:27
|
4 |     let _ = "test".into::<Box<dyn Error>>();
|                           ^^^^^^^^^^^^^^ unexpected type argument

为什么?

这来自Into特征定义:

pub trait Into<T> {
fn into(self) -> T;
}

如您所见,into没有通用参数,但它来自特征定义本身。那么正确的完全限定语法将是:

use std::error::Error;
fn main() {
let _ = Into::<Box<dyn Error>>::into("asd");
}

相关内容

最新更新