Hello everybody,
this is my first time post, so first of all "Hello, world!".
These days, I am learning rust, and currently, I am working on a unix keylogger, hooked between the keyboard and the unix system (in my case Linux).
The keylogger will run as a background service, which forces me to manage so called PID files. Since I am learning rust, I try write clean code, which includes effort to get rid of all warnings.
So, that's the problem. I have this snippet:
///
/// Reads the content of the PID_FILE.
/// None, if the file does not exist, or if reading fails.
/// otherwise Some(pid)
///
fn read_pid_file(f: &str) -> Option<u32> {
let mut pid = None;
if std::path::Path::new(f).exists() {
// read file and fill pid, if file contains value
let pidf = std::fs::OpenOptions::new()
.read(true)
.open(f);
if pidf.is_ok() {
let mut buf = String::new();
pidf.unwrap().read_to_string(&mut buf);
pid = Some(buf.parse::<u32>().unwrap());
}
}
pid
}
The warning is the following:
warning: unused `std::result::Result` that must be used
--> src/os/process.rs:56:13
|
56 | pidf.unwrap().read_to_string(&mut buf);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
As you can see, the method will return "None" if something goes wrong, otherwise it will return Some().
Reading the file will return a result, which needs to be unwrapped. I am checking this with the preceding if pidf.is_ok(). But still, the compiler generates warnings to handle the (im)possible case of an error.
How can I get rid of the warning? Is there a better way to write my code? I would like to have a short & clean solution.
Thanks for any input, remarks or different. If I violate any rust paradigms please let me know. I would love to enhance my rust knowledge, which requires feedback.
Thanks for any response.
Best regards
Max