News for hackers. Stuff that matters.
Re:Helix: Rust and Ruby, Without the Glue by aidenn0 on Saturday May 14, 2016 @09:12PM (#11698853)
The Rust version works on strings not on bytes. Strings don't have encodings.
Re:Helix: Rust and Ruby, Without the Glue by hetman on Saturday May 14, 2016 @09:47PM (#11698960)
What do you mean? All Rust strings are UTF-8 encoded, and all Ruby strings have an associated encoding.
Re:Helix: Rust and Ruby, Without the Glue by steveklabnik on Saturday May 14, 2016 @11:45PM (#11699306)
All rust String and &strs are UTF-8 encoded, there are also other string types.
Re:Helix: Rust and Ruby, Without the Glue by x5n1 on Saturday May 14, 2016 @09:46PM (#11698959)
huh? strings have encodings. rust strings are bytes encoded in utf-8.

https://doc.rust-lang.org/book/strings.html

Re:Helix: Rust and Ruby, Without the Glue by aidenn0 on Saturday May 14, 2016 @10:37PM (#11699116)
In Rust a string is a sequence of unicode scalar values. I personally find it unfortunate that they dictate the storage of it at the API level, but that is a necessary evil for presenting a consistent ABI with foreign code.

I did not know that strings in Ruby have encodings. Is there a reason for that? I personally don't like mixing characters and opaque byte sequences as they are very different.

Re:Helix: Rust and Ruby, Without the Glue by burntsushi on Saturday May 14, 2016 @11:19PM (#11699235)
> In Rust a string is a sequence of unicode scalar values.

The representation of a Rust String in memory is guaranteed valid UTF-8. To me, a "sequence of Unicode scalar values" is an abstract description, because it could be implemented via UTF-8, UTF-16 or UTF-32.

> I personally find it unfortunate that they dictate the storage of it at the API level

It is extraordinarily convenient and provides a very transparent way to analyze the performance of string operations.

For transcoding, there is the in-progress `encoding` crate: https://github.com/lifthrasiir/rust-encoding

I note that Go does things very similarly (`string` is conventionally UTF-8) and it works famously for them. They have a much more mature set of encoding libraries, but they work the same as the equivalent libraries would work in Rust: transcode to and from UTF-8 at the boundaries. See: https://godoc.org/golang.org/x/text

Re:Helix: Rust and Ruby, Without the Glue by MichaelGG on Saturday May 14, 2016 @10:50PM (#11699151)
Ruby's Japanese heritage is probably why it handles encodings like that - I think there were multiple encs it had to deal with at once or something. Also Unicode doesn't completely handle all kanji in that there's some that have an old style not available in Unicode. But maybe that's not relevant.
Re:Helix: Rust and Ruby, Without the Glue by aidenn0 on Saturday May 14, 2016 @11:02PM (#11699195)
Unicode now handles all the Kanji in JIS. I wouldn't be surprised if Ruby predated that. It almost certainly predates good library support for all the Kanji in JIS.
Re:Helix: Rust and Ruby, Without the Glue by GolDDranks on Sunday May 15, 2016 @06:57AM (#11700399)
I think the problem isn't whether it handles all the Kanji in JIS – it does. But the problem is that JIS at the time was so common that it didn't necessarily make sense to settle exclusively for then-less-used UTF-8. That would make re-encodings necessary at interfaces and on IO.
Re:Helix: Rust and Ruby, Without the Glue by steveklabnik on Saturday May 14, 2016 @11:47PM (#11699313)
Ruby encoding stuff changed a lot over its history; it was one of the big changes from 1.8 to 1.9.
Re:Helix: Rust and Ruby, Without the Glue by twelvechairs on Saturday May 14, 2016 @11:21PM (#11699244)
Its a better way of doing things - you can handle things in their native format rather than have to arbitrarily convert to UTF8 (which is an 'encoding' itself).

[edit] I remember a talk where Matz was asked this specific question and tried to explain it clearly but seemed confused as to how the questioner could have such a poor grasp of unicode (the difference between monolingual americans and japanese i guess)

Re:Helix: Rust and Ruby, Without the Glue by kibwen on Sunday May 15, 2016 @01:02AM (#11699517)
String is just a typedef for Vec<u8> with some extra convenience functions for working with UTF-8. There's nothing stopping anyone from just using Vec<u8> to handle non-UTF-8 data in their native format, nor stopping anyone from writing convenience types like String for other encodings.
Re:Helix: Rust and Ruby, Without the Glue by twelvechairs on Sunday May 15, 2016 @06:24AM (#11700295)
Yeah right so Ruby effectively has just made a bunch of these (and done the hard work for you of defining how to convert between them and work with them all in similar ways), and the higher-level class which includes UTF8 and a whole bunch of others is called 'String'. Its really what you want from a high-level language - to just work with different encodings out of the box, but not have to convert to a standard interal type (like UTF8) to do so.
Re:Helix: Rust and Ruby, Without the Glue by wtetzner on Sunday May 15, 2016 @10:54AM (#11701120)
Well, it's hard to say, really. It depends on what you're doing. The benefit of converting to UTF-8 when making a string from bytes is that string operations have predictable performance, and strings have predictable memory-usage. But of course, you then have to pay the cost of converting to UTF-8.

On the other hand, if you just track the encoding in your string type, then you don't have to pay a conversion cost at the boundary, but each encoding will have different memory-usage and performance characteristics.

Re:Helix: Rust and Ruby, Without the Glue by lobster_johnson on Saturday May 14, 2016 @11:46PM (#11699312)
The reason is that Ruby supports non-Unicode encodings that are not subsets of Unicode. Not possible if your string is Unicode.
Re:Helix: Rust and Ruby, Without the Glue by MichaelGG on Saturday May 14, 2016 @10:07PM (#11699036)
Right, so how do you get from Ruby strings (various encodings) to a Rust string? The sample code just calls std::str::from_utf8_unchecked(s) which is obviously not dealing with Ruby encodings.
Re:Helix: Rust and Ruby, Without the Glue by aidenn0 on Saturday May 14, 2016 @10:39PM (#11699121)
Yeah, that's a clear bug. I was not aware that Ruby strings had encodings.
Re:Helix: Rust and Ruby, Without the Glue by x5n1 on Sunday May 15, 2016 @05:19PM (#11702820)
All strings have encodings. It is not possible to represent a string which is a series of bytes except with encodings. I guess you probably mean default encoding or no encoding support... which implies ASCII, better known as US-ASCII.
Re:Helix: Rust and Ruby, Without the Glue by aidenn0 on Monday May 16, 2016 @04:52PM (#11709517)
In the high-level language I am most familiar with (Common Lisp), strings do not have encodings because they are vectors of characters, not vectors of bytes. How the string is actually stored in memory is an implementation detail.

Encoding is purely an artifact of I/O if your language has a character type that can represent all possible characters you might want read or write.

Rust's strings are almost this; if there were no way to get a string's raw representation, nor perform bytewise slices, then how the string was stored in RAM would be an implementation detail rather than part of the public API. Rust, being a systems language, probably does need to specify this so that it doesn't incur encode/decode overhead when dealing with foreign code that can understand utf-8.