pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()>;
I am struggling to cast std::fs::remove_file to a function pointer:
Playground
use std::{io, fs, path::Path};
fn main() {
let _: fn(&Path) -> io::Result<()> = &fs::remove_file;
// let _: &dyn FnOnce(&Path) -> io::Result<()> = &fs::remove_file;
}
The error is the following:
error[E0308]: mismatched types
--> src/main.rs:4:43
|
4 | let _: fn(&Path) -> io::Result<()> = &fs::remove_file;
| --------------------------- ^^^^^^^^^^^^^^^^ expected fn pointer, found reference
| |
| expected due to this
|
= note: expected fn pointer `for<'r> fn(&'r std::path::Path) -> std::result::Result<(), std::io::Error>`
found reference `&fn(_) -> std::result::Result<(), std::io::Error> {std::fs::remove_file::<_>}`
The problem might be with the for<'r> higher-order lifetimes requirement, but I don't know how to solve it.
How would I do it with a trait object? The following doesn't compile either:
let _: &dyn FnOnce(&Path) -> io::Result<()> = &fs::remove_file;
I know I can make a wrapper function around fs::remove_file, but I'd like to avoid that and get fs::remove_file to become a function pointer or trait object itself.
This is not a duplicate of this Function pointers in Rust using constrained generics as I'd like to get a function pointer to the concrete instantiation of std::fs::remove_file with &Path as a type parameter, but not to have a generic function pointer type.
I tried the following and it doesn't work either:
use std::{io, fs, path::Path};
fn main() {
let _: fn(&Path) -> io::Result<()> = &fs::remove_file::<&Path>;
}