StopClock Part Two

Here is the next part of my StopClock post that I’ve made yesterday[0].

Warning two: unnecessary parentheses

warning: unnecessary parentheses around `if` condition
  --> src/main.rs:14:8                                                                                                                             
   |                                
14 |     if (args.len() == 2)    
   |        ^^^^^^^^^^^^^^^^^ help: remove these parentheses             
   |                                                                     
   = note: `#[warn(unused_parens)]` on by default                        

This seems like bash or something? Why did I add these parentheses? The default if else case does not require them. Here is an example: if n < 10 && n > -10 [1]. No parentheses. It does not stop only on this line.

warning: unnecessary parentheses around `while` condition
  --> src/main.rs:37:15
   |
37 |         while (remaining_time >= MINUTE as u64){
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
   |
   = note: `#[warn(unused_parens)]` on by default

warning: unnecessary parentheses around `while` condition
  --> src/main.rs:46:14
   |
46 |         while(minutes_remaining != time_as_minutes)
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses

This seems like a C related syntax mistake[3].

Warning three: unused variable

  --> src/main.rs:16:11
   |
16 |       let name = &args[0];
   |           ^^^^ help: if this is intentional, prefix it with an underscore: `_name`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `time_to_sleep_arg2`
  --> src/main.rs:24:11
   |
24 |       let time_to_sleep_arg2 = &args[1];
   |           ^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_time_to_sleep_arg2`
   |
   = note: `#[warn(unused_variables)]` on by default

This is simple, remove these, and we’re good to go.

Warning four: unused std::result::Result that must be used

warning: unused `std::result::Result` that must be used
  --> src/main.rs:53:9
   |
53 | /         Command::new("/usr/bin/notify-send")
54 | |                 .args(&["-u", "normal", "-t", "15000", "StopClock", "Times up!"]) 
55 | |                 .output();
   | |__________________________^
   |
   = note: `#[warn(unused_must_use)]` on by default
   = note: this `Result` may be an `Err` variant, which should be handled

warning: unused `std::result::Result` that must be used
  --> src/main.rs:58:9
   |
58 | /         Command::new("/usr/bin/mplayer")
59 | |                  .args(&["-volume","50", "/home/akendo/Downloads/end.mp3"])
60 | |                  .output();
   | |___________________________^
   |
   = note: this `Result` may be an `Err` variant, which should be handled

warning: 3 warnings emitted

Oh boy, that’s a more difficult one, but a good warning indeed.

What we’re going to do for the moment is to add a .unwrap() to ignore that cases. However, we’re adding To-Do for later to handles some testing before executing. The main problem here is that we’re relying on external files. When these files or program are absence we will run into error. Hence, create some error cases for such a situation seems reasonable.

When i frist wrote rust I did not understood the concepct of Result[4]. Therefor this error message did not made any sense.

In my understanding now, Result is similar to Option and therefore a handler of failure. The problem is that not every function can succeed all the time. Option is useful to have something that will be returned while Result is the high-level option that only case for success or not.

Err of Result should indicate to the user what the error was and how to potentially resolve it. I think the Python pardon would be a try block. The Result would be the raise function.

Any way, that’s it for today.

So far, akendo

[0] https://blog.akendo.eu/post/2021-02-26-rust-stopclock/
[1] https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
[2] https://doc.rust-lang.org/std/keyword.while.html
[3] https://www.tutorialspoint.com/cprogramming/c_while_loop.htm
[4] https://doc.rust-lang.org/std/result/enum.Result.html