Greetings,
I have a trait bounded struct. I'd like to declare a variable, but would like to avoid specifying the exact type.
use std::path::Path;
pub struct Foo<P>
where
P: AsRef<Path>,
{
file_path: P,
}
impl<P> Foo<P>
where
P: AsRef<Path>,
{
fn new(
file_path: P,
) -> Self {
Self {
file_path,
}
}
}
fn main(
) {
let foo: Foo<&str>;
let foo: Foo<&Path>;
let some_condition = false;
if some_condition {
foo = Foo::new(
"/etc/passwd",
);
}
else {
foo = Foo::new(
Path::new("/etc/passwd"),
);
}
}
but I don't know how to generalize the:
let foo: Foo<&str>;
let foo: Foo<&Path>;
When I pick one of the two options, I get an error like:
Compiling pathbuf_struct v0.1.0 (/home/mzagrabe/git/internal/rust/projects/pathbuf_struct)
error[E0308]: mismatched types
--> src/main.rs:32:13
|
32 | "/etc/passwd",
| ^^^^^^^^^^^^^ expected struct `Path`, found `str`
|
= note: expected reference `&Path`
found reference `&'static str`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `pathbuf_struct` due to previous error
Is it possible to declare a trait bounded type without specifying a specific type?
Thanks for any help!
-m