Wild idea: deprecating APIs that conflate str and [u8]

The documentation for len() is very explicit about this:

Returns the length of this String , in bytes, not char s or graphemes. In other words, it may not be what a human considers the length of the string.

It even includes an example of how to compute the number of chars instead:

let fancy_f = String::from("ƒoo");
assert_eq!(fancy_f.len(), 4);
assert_eq!(fancy_f.chars().count(), 3);

It is logical that .len() operates on bytes, because anything else would have to be computed expensively.

However, it's weird that drain() returns an Iterator<Item = char> and not just a struct that implements Deref<Target = str>, because that would be both more powerful and more explicit: .drain() would have to be replaced with .drain().chars() (or .drain().bytes() or .drain().graphemes(true), if you want to iterate over bytes or grapheme clusters instead).

4 Likes