Do not use .as_bytes().len() on strings

This commit is contained in:
Samuel Tardieu 2024-09-22 10:48:14 +02:00
parent 4e4b65711a
commit 2653c3c4d4
2 changed files with 6 additions and 4 deletions

View file

@ -2,10 +2,11 @@
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Obtain the number of bytes (not characters) in the given argument.
// Obtain the number of bytes (not characters) in the given argument
// (`.len()` returns the number of bytes in a string).
// TODO: Add the `AsRef` trait appropriately as a trait bound.
fn byte_counter<T>(arg: T) -> usize {
arg.as_ref().as_bytes().len()
arg.as_ref().len()
}
// Obtain the number of characters (not bytes) in the given argument.

View file

@ -2,9 +2,10 @@
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Obtain the number of bytes (not characters) in the given argument.
// Obtain the number of bytes (not characters) in the given argument
// (`.len()` returns the number of bytes in a string).
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().as_bytes().len()
arg.as_ref().len()
}
// Obtain the number of characters (not bytes) in the given argument.