在 Programming Rust 的第 298 页可以找到如下语句
The standard library automatically implements the trivial conversion from each type to itself: every type
T
implementsFrom<T>
andInto<T>
但是它实际上并没有说明为什么这会有用,或者为什么标准库会自动这样做?这是什么原因?
最佳答案
在编写带有泛型参数的函数/类型时会很方便。例如,如果我写一个函数:
fn do_something_with_string<S: Into<String>>(s: S) {}
该函数同样适用于 String
和任何可以变成一个的东西。
关于rust - 为什么标准库要实现从每种类型到自身的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089386/