这有效:
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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句