Clipping rust/upload ...
warning: passing a unit value to a function
--> rust/upload.rs:8:5
|
8 | Ok(Upload{}.show())
| ^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
= note: `#[warn(clippy::unit_arg)]` on by default
help: move the expression in front of the call and replace it with the unit literal `()`
|
8 ~ Upload{}.show();
9 + Ok(())
|
I read the clippy explanation and the risk of my code missed ';'. Indeed, I have no ';', but I do not need them. Any other reason why my code is bad?
2. I program in Rust using C style, so Clippy corrected me. Indeed, I tried the Clippy recommendation and it is 15% faster. But why? There is the code:
use std::error::Error;
use std::time::Instant;
trait CharLen {
fn char_len(&self) -> usize ;
}
impl CharLen for String {
fn char_len(&self) -> usize {
self.chars().count()
}
}
fn main() -> Result<(), Box<dyn Error>> {
let s = "test ΑΒΓΔΕΖΗΘ".to_string();
let size = 1001;
let mut children = Vec::with_capacity(size);
for _ in 0..size {
children.push(s.clone())
}
let mut tot_len = 0;
let start = Instant::now();
for _ in 0..1000000 {
for idx in 1..size {
tot_len += children[idx].char_len()
}
}
let duration = start.elapsed();
println!("DR Time elapsed: {:?} - {tot_len}", duration);
tot_len = 0;
let start = Instant::now();
for _ in 0..1000000 {
for child in children.iter().take(size).skip(1) {
tot_len += child.char_len()
}
}
let duration = start.elapsed();
println!("Clippy Time elapsed: {:?} - {tot_len}", duration);
Ok(())
}
Is the C style code problem that it needs to check an index boundary from both sides, and for an iterator only upper boundary?
This looks to a rust programmer like show returns an important value. In general, people assume that nesting in code has a purpose. But since that's not the case, it looks a little strange. You might've called the wrong function.
Your code has to check the length of children on every iteration. In an ideal world this would be optimized to be the same as the other version, but here we are. Also, the corrected version isn't entirely equivalent. If size is past the end of children, it will add less elements instead of panicking. If that's what you want, then fine, but this would be a closer translation: