这有效:
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");
}