Skip to content

Commit

Permalink
Merge pull request rust-lang#2114 from samueltardieu/push-ptorzrrnmxyp
Browse files Browse the repository at this point in the history
Do not use `.as_bytes().len()` on strings
  • Loading branch information
mo8it authored Sep 22, 2024
2 parents 4e4b657 + 2653c3c commit a55e848
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions exercises/23_conversions/as_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions solutions/23_conversions/as_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a55e848

Please sign in to comment.