I created my first project in rust. It's a sudoku solver. I separated my code into several files. I'm not quite sure if the code is good though and clippy is complaining about line 109 in solver.rs. Is that bad code or is clippy sometimes not to taken too seriously?
I'd really appreciate some feedback. Thanks in advance
I only glanced at the project, I'm guessing clippy is suggesting that you use iterator methods here instead of an explicit loop. Something like this I'm guessing :
board.fields.iter().any(|f| f.is_none())
In this case you should probably do it and familiarize yourself with iterators in general. There should be other places where you can improve with it.
Classic! A Sudoku solver is always my first project in any new language
Clippy is (almost) always worth listening to, but this might be a rare exception. So this code:
for i in 0..9 {
for j in 0..9 {
if board.fields[9 * i + j].is_some() {
row_amount[i] += 1
}
if board.fields[i + j * 9].is_some() {
column_amount[j] += 1
}
}
}
Clippy says (Clippy Lints):
"Checks for looping over the range of 0..len
of some collection just to get the values by index."
But that's not the only thing your code is doing with the i
and j
, so maybe clippy is off-base here.
If you really want to keep Clippy happy, try this (WARNING: NOT TESTED):
for (i, r_amt) in row_amount.iter_mut().enumerate() {
for (j, c_amt) in column_amount.iter_mut().enumerate() {
if board.fields[9 * i + j].is_some() {
*r_amt += 1
}
if board.fields[i + j * 9].is_some() {
*c_amt += 1
}
}
}
Or if you want to get really functional: (WARNING: NOT TESTED)
let (row_amount, col_amount) = board
.fields
// Iterates `field`, along with an incrementing index `i`
.iter()
.enumerate()
// Filters out the fields that are None
.filter_map(|(i, field)| field.as_ref().map(|_| i))
// Adjusts `row_amount` and `col_amount` arrays for each non-None field
.fold(([0; 9], [0; 9]), |(mut r_amt, mut c_amt), i| {
let r = i % 9;
let c = i / 9;
r_amt[r] += 1;
c_amt[c] += 1;
(r_amt, c_amt)
});
Yeah, usually clippy lints are great and you should follow them.
But sometimes you may feel your code is more readable the way you wrote it in the first place, and you can annotate the case with #[allow(name_of_clippy_lint)]
and it will stop bugging you about it for that case.
(post deleted by author)
Thanks, but this wouldn't update the counters
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.