News for hackers. Stuff that matters.
Re:Thoughts on Rust, a few thousand lines in by yurymik on Wednesday January 09, 2019 @05:15PM (#18869538)
But neither AsciiString. It has a as_str method, but it's still a kludge.

This example was basically a suggestion to throw0u1t: if they want to cut in the middle of the utf-sequence for whatever reason, they can [edit:] do it without extra crates.

What I don't understand is why slices are indexed in bytes and not in objects. If String has an ability to check that we're cutting in the middle of the character sequence, why doesn't it provide an ability to take 3 fully formed characters.

Re:Thoughts on Rust, a few thousand lines in by Someone on Wednesday January 09, 2019 @05:38PM (#18869738)
I think the rust designers want to keep the implicit contract that indexing into a string is fast and O(1).

If you want to find the one millionth codepoint of a UTF8-encoded string, you have to more or less (1) visit every byte of the string.

If, on the other hand, you want to find the codepoint that covers the millionth byte, on the other hand, you have to read at most four bytes (read the millionth byte, and there are three cases:

- it’s a full codepoint. If so, you‘re done.

- it is the first byte of a multi-byte codepoint. If so, read forwards in the string for up to 3 continuation characters.

- it is a continuation character. If so, search backwards in the string for the first byte, then, if necessary, read forwards to find more continuation characters.

So, that is O(1)

(1) you can skip continuation characters, but these typically are rare.

Re:Thoughts on Rust, a few thousand lines in by steveklabnik on Wednesday January 09, 2019 @05:28PM (#18869654)
> What I don't understand is why slices are indexed in bytes and not in objects.

Slicing is an O(1) operation, and that would be an O(n) operation.

Re:Thoughts on Rust, a few thousand lines in by TheCoelacanth on Thursday January 10, 2019 @10:57AM (#18874750)
It does: `s.chars().take(3)`. It just does it with iterators rather than with indexes because that better communicates the performance characteristics.