Warning: Some posts on this platform may contain adult material intended for mature audiences only. Viewer discretion is advised. By clicking ‘Continue’, you confirm that you are 18 years or older and consent to viewing explicit content.
The problem is when you think you know stuff, but you don’t. I knew that counting bytes doesn’t work, but thought the number of codepoints was what I want. And then knowing that Rust uses UTF-8 internally, it’s logical that .chars().count() gives the number of codepoints. No need to read documentation, if you’re so smart. 🙃
It does give you the correct length in quite a lot of cases, too. Even the byte length looks correct for ASCII characters.
So, yeah, this would require a lot more consideration whether it’s worth it, but I’m mostly thinking there’d be no .len() on the String type itself, and instead to get the byte count, you’d have to do .as_bytes().len().
The problem is when you think you know stuff, but you don’t. I knew that counting bytes doesn’t work, but thought the number of codepoints was what I want. And then knowing that Rust uses UTF-8 internally, it’s logical that
.chars().count()
gives the number of codepoints. No need to read documentation, if you’re so smart. 🙃It does give you the correct length in quite a lot of cases, too. Even the byte length looks correct for ASCII characters.
So, yeah, this would require a lot more consideration whether it’s worth it, but I’m mostly thinking there’d be no
.len()
on the String type itself, and instead to get the byte count, you’d have to do.as_bytes().len()
.