I'm having trouble trying to implement From on a struct. A simple example is:
#[derive(Debug)]
struct Example {
field: String,
}
impl From<P: AsRef<Path>> for Example {
fn from(path: P) -> Self {
Example {
field: path.as_ref().to_str().unwrap().to_owned(),
}
}
}
fn main() {
println!("{:?}", Example::from("test"));
}
This fails to compile with:
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `:`
--> src/main.rs:6:12
|
6 | impl From<P: AsRef<Path>> for Example {
| ^ expected one of 7 possible tokens here
I have also tried From<AsRef<Path>>
, but couldn't get that to work either. I expected the first example to work because that is the syntax I would use for a function.
Is what I'm trying to do fundamentally not possible? Or am I doing it wrong?
for Example`. However, yes, what you are trying to do is fundamentally disallowed, as explained by the duplicate.