Warning
This post was published 70 days ago. The information described in this article may have changed.
Hi folks,
I'd like to be able to:
let x = vec![Foo{ ... } ... ];
let y: Vec<String> = x.into()
or
let x = &[Foo{ ... } ... ];
let y: &[&str] = x.as_ref()
but I can't:
pub struct Foo{
bar: String
}
impl From<Foo> for String {
fn from(value: Foo) -> String {
value.bar
}
}
impl AsRef<str> for Foo {
fn as_ref(&self) -> &str {
self.bar.as_ref()
}
}
// error: this is not defined in the current crate
impl From<Vec<Foo>> for Vec<String> {
fn from(value: Vec<Foo>) -> Self {
value.into_iter().map(|x| x.bar).collect()
}
}
Could you please advise what is the best practice for this?
2 posts - 2 participants
🏷️ rust_feed