我希望能够重用泛型类型别名作为Rust中一些函数的泛型类型参数。
我尝试按照type alias rust docs中指定的语法创建以下类型别名
type DebuggableFromStr<T: FromStr>
where
<T as std::str::FromStr>::Err: std::fmt::Debug,
= T;
并希望使用它来替换以下函数中的泛型类型定义:
fn split_string_to_vec<T: FromStr>(s: String) -> Vec<T>
where
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
s.split_whitespace()
.map(|s| s.parse().unwrap())
.collect::<Vec<T>>()
}
发布于 2020-01-04 07:56:11
不,因为Rust没有强制类型别名的类型界限。您的示例等同于:
type DebuggableFromStr<T> = T;
我不相信它在任何地方都有专门的文档,但是如果你尝试的话,编译器会发出一个警告。
https://stackoverflow.com/questions/59586238
复制相似问题