Couple Clippy questions
⚓ Rust 📅 2025-11-26 👤 surdeus 👁️ 8- Clippy gives me the warning:
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?
3 posts - 3 participants
🏷️ Rust_feed