Rust

Rust

By DeepSource

Found occurrence of .bytes().nth(..) RS-P1002

Performance

.bytes().nth(..) is better written as .as_bytes().get(..) which is more performant.

.as_bytes().get(..) is a constant-time operation, whereas .bytes().nth(..) iterates over the bytes in the string and is a linear-time operation.

Examples

Bad practice

"random str".bytes().nth(5)

Recommended

"random str".as_bytes().get(5)

References