mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 23:10:30 +00:00
Do not use .as_bytes().len()
on strings
This commit is contained in:
parent
4e4b65711a
commit
2653c3c4d4
2 changed files with 6 additions and 4 deletions
|
@ -2,10 +2,11 @@
|
||||||
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
|
// 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.
|
// 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.
|
// TODO: Add the `AsRef` trait appropriately as a trait bound.
|
||||||
fn byte_counter<T>(arg: T) -> usize {
|
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.
|
// Obtain the number of characters (not bytes) in the given argument.
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
|
// 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.
|
// 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 {
|
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.
|
// Obtain the number of characters (not bytes) in the given argument.
|
||||||
|
|
Loading…
Reference in a new issue