0

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ratorx
  • 257
  • 1
  • 8
  • 1
    Your immediate problem is that you are using invalid syntax. You want `impl> From

    for Example`. However, yes, what you are trying to do is fundamentally disallowed, as explained by the duplicate.

    – Shepmaster Nov 16 '18 at 04:01
  • 1
    Thanks for linking the duplicate. I should've searched harder before posting. – ratorx Nov 16 '18 at 04:06

0 Answers0