Hacker News new | past | comments | ask | show | jobs | submit
Yeah, no. C++ is deliberately wrong about lots of things. You don't have to be wrong, choosing not to be wrong isn't somehow hiding the wrong behind a curtain.

If you try to do the same mistake in Rust it does not compile, this function takes an int (obviously shouldn't but that's a different design bug in C not C++) and we have a wchat_t which is not an int, so that does not compile at all. Footgun removed.

When you start trying to figure out how to write the equivalent mistake in Rust properly, you discover that Rust says uppercasing or lowercasing arbitrary Unicode char values gets you an Iterator and you can't convince your compiler that this cannot make the string longer -- because in fact it might make the string longer. The mistake is averted by design.

Sure, but behind the scenes, the string is stored in memory as a list of bytes, and the list of bytes gets longer if you lower-case it. Rust pulls a nice curtain in front of all the nasty pointers and registers, but this problem can't be eliminated.
Way to go in completely ignoring the GP's point that Rust does not do that, because Rust does not export that operation you are describing. The only way to get that behavior in Rust is to write the conversion code by hand, with the wrong behavior.
The only way it works so elegantly in Rust is that someone wrote a much nastier implementation behind the scenes. When the Rust end-user gets that nice Iterator, someone behind the scenes has to decode the string and decide how many bytes the new Iterator has. Complexity can be hidden, but it can't be eliminated.
There's no nastier implementation because the function isn't there.

There's nothing converting the case of a string in place. The iterator does not decide how many bytes the string has, it delivers one character at a time. It's trivial to decide how many bytes a character has (it's on the definition of your encoding), and it's written down at the type definition.

Converting the case of an string in place is just a bad operation. It should not be done. So the Rust stdlib doesn't do it. The idea that complexity has a fixed size is trivially wrong.

> When the Rust end-user gets that nice Iterator, someone behind the scenes has to decode the string and decide how many bytes the new Iterator has.

The Iterator doesn't have bytes in it, we're not idiots, the Iterator is over Rust's char type, a Unicode scalar value (a "character" if you don't know the lingo)

What's delivering value here is division of responsibility. Cutting a problem up into pieces each of which can be tackled by an expert. Uppercasing text, encoding characters as bytes, allocating memory, appending to a buffer, these are all distinct problems which a different expert can solve properly.

In that Ruby code you need to gather maybe a half dozen experts to agree how to write this function. Or, of course, one expert YOLOs the rest of it because every topic we don't know much about is easy, right...

Rust does this division a lot. You as a programmer see String but when your String grows that's not code in String where people who know about Unicode live, that's RawVecInner<A> and that code doesn't even know about characters or strings or any such thing, it just allocates enough space to store data.