RE: [PATCH 00/13] [RFC] Rust support
From: Leandro Coutinho <hidden>
Date: 2021-05-09 21:29:04
quoted
The more you make it look like (Kernel) C, the easier it is for us C people to actually read. My eyes have been reading C for almost 30 years by now, they have a lexer built in the optical nerve; reading something that looks vaguely like C but is definitely not C is an utterly painful experience.I'll see your 30 years and raise to over 35. (And writing code that accesses hardware for 6 or 7 years before that.) Both Java and go can look more like the K&R style C than any of the examples from microsoft - which seem to utilise as much vertical space as humanly? possible. Those rust examples seemed to be of the horrid microsoft sytle. Nothing about that style makes reading code easy. David
One thing I miss is the good old for loop.
Rust `for` works fine, until the step is not a simple unsigned int, eg:
for (int i = n / 2; i > 0; i /= 2)
In Rust you do: (please let me know if there is a better way):
let mut i = n / 2;
while i > 0 {
// some logic ...
i /= 2;
}
The great thing about the C 'for' loop is initialization, condition and
step at the same line makes it very easy to understand the code, and
less error prone, like forgetting the step at the end of the loop.
Some code if people would like to test too:
// https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.step_by
fn main() {
let n: i32 = 10;
// for (int i = n / 2; i > 0; i /= 2)
let mut i = n / 2;
while i > 0 {
print!("{} ", i);
i /= 2;
}
println!();
// or ...
i = n / 2;
loop {
if i == 0 { break; }
print!("{} ", i);
i /= 2;
}
println!();
// i = i == 0 ? n >> 2 : n / 2; // nope. Rust does not have it ... :/
i = if i == 0 { n >> 2 } else { n / 2};
// for (int i = n >> 2; i > 0; i = i >> 2)
while i > 0 {
// some logic ...
print!("{} ", i);
i = i >> 2;
}
println!();
for i in 0..4 { print!("{} ", i); }
println!();
for i in (0..4).step_by(2) { print!("{} ", i); }
println!();
for i in (0..4).rev() { print!("{} ", i); }
println!();
for i in (0..4).rev().step_by(2) { print!("{} ", i); }
println!();
}
But Rust has many nice features. I hope it works. :)