Skip to content

Commit

Permalink
Update minesweeper.rs
Browse files Browse the repository at this point in the history
Using as_bytes instead of chars() since All the inputs and outputs are in ASCII. Rust Strings and &str are utf8, so while one might expect "Hello".chars() to be simple, it has to check each char to see if it's 1, 2, 3, or 4 u8s long. If we know a &str is ASCII then we can call .as_bytes() and refer to the underlying data as a &[u8] (byte slice). Iterating over a slice of ASCII bytes is much quicker as there are no codepoints involved - every ASCII byte is one u8 long.
  • Loading branch information
BillQK authored Mar 17, 2024
1 parent 2973309 commit 0c1e9b0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion exercises/practice/minesweeper/tests/minesweeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ fn remove_annotations(board: &[&str]) -> Vec<String> {
}

fn remove_annotations_in_row(row: &str) -> String {
row.chars()
row.as_bytes()
.iter()
.map(|ch| match ch {
'*' => '*',
_ => ' ',
Expand Down

0 comments on commit 0c1e9b0

Please sign in to comment.