Closed
Description
What it does
Suggests replacing slicing a str
followed by as_bytes
with the opposite order
s[a..b].as_bytes()
with
&s.as_bytes()[a..b]
Advantage
- Removes unnecessary utf8 indexing check
- won't panic if a and b are not on utf8 boundaries
Drawbacks
Technically possible someone relied on it panicking, but find that unlikely.
Example
let s = "Hello World";
let bytes = s[1..3].as_bytes();
Could be written as:
let s = "Hello World";
let bytes = &s.as_bytes()[1..3];