Why can't I forward `From` implementations?
⚓ Rust 📅 2025-05-16 👤 surdeus 👁️ 10I have a container that contains a generic T.
struct Container<T> {
content: T,
}
I would like for it to "absorb" into() calls so people can directly create Container<...> instead of having to first call u.into() to get a T and then wrap it in Container<T>.
For example, I would like to do
let s = "Hello World!";
let container: Container<String> = s.into():
instead of having to do
let s = "Hello World!";
let container: Container<String> = Container {
content: s.into()
}
I wanted to achieve it like this:
impl<T, U> From<U> for Container<T>
where
T: From<U>,
{
fn from(value: U) -> Self {
Self {
content: value.into(),
}
}
}
The compiler gives me an error message that I don't really understand:
conflicting implementations of trait `std::convert::From<Container<_>>` for type `Container<_>`
conflicting implementation in crate `core`:
- impl<T> std::convert::From<T> for T;rustcE0119
Is what I want to do even possible?
Thanks in advance for any pointers ![]()
4 posts - 3 participants
🏷️ rust_feed