Skip to content

Commit a84736d

Browse files
authored
use byte literals instead of casting char to u8 in exercise examples (#1017)
1 parent 9a0ffec commit a84736d

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

exercises/minesweeper/example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Board {
3030
if count == 0 {
3131
' '
3232
} else {
33-
(('0' as u8) + count) as char
33+
(b'0' + count) as char
3434
}
3535
}
3636
}

exercises/simple-cipher/example.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub fn encode_random(s: &str) -> (String, String) {
55
let mut r = rand::thread_rng();
66
let mut key = String::new();
77
for _ in 0..100 {
8-
key.push(char::from('a' as u8 + r.gen_range(0, 26)));
8+
key.push(char::from(b'a' + r.gen_range(0, 26)));
99
}
1010
let encoded = encode(&key, s);
1111
(key, encoded.unwrap())
@@ -34,8 +34,8 @@ fn shift(key: &str, s: &str, dir: i8) -> Option<String> {
3434
}
3535
for c in s.chars() {
3636
let shift = key_arr[i % key_arr.len()] as i8 - 'a' as i8;
37-
let n = ((c as i8 - 'a' as i8 + dir * shift) % 26 + 26) % 26;
38-
o.push(char::from('a' as u8 + n as u8));
37+
let n = ((c as i8 - b'a' as i8 + dir * shift) % 26 + 26) % 26;
38+
o.push(char::from(b'a' + n as u8));
3939
i += 1;
4040
}
4141
Some(o)

0 commit comments

Comments
 (0)