3 ms·
Is there any other example of "length" meaning "byte length", or is it just Rust just being confusing? I've never seen this elsewhere. Offset is ordinarily jus
by dataflow 7mo ago
Is there any other example of "length" meaning "byte length", or is it just Rust just being confusing? I've never seen this elsewhere.
Offset is ordinarily just a difference of two indices. In a container I don't recall seeing it implicitly refer to byte offset.
- SabrinaJewson 7mo agoIn general in Rust, “length” refers to “count”. If you view strings as being sequences of Unicode scalar values, then it might seem odd that `str::len` counts bytes, but if you view strings as being a subset of byte slices it makes perfect sense that it gives the number of UTF-8 code units (and it is analoguous to, say, how Javascript uses `.length` to return the number of UTF-16 code units). So I think it depends on perspective.
- dataflow 7mo agoThat makes sense, I agree -- seems Rust is on board here too.
- AlotOfReading 7mo agoIt's the usual convention for systems programming languages and has been for decades, e.g. strlen() and std::string.length(). Byte length is also just more useful in many cases.
- dataflow 7mo agoNo, those are counts by definition, and byte lengths only by coincidence. Look at wcslen() and std::wstring::length().
- wyldfire 7mo agoA length could refer to lots of different units - elements, pages, sectors, blocks, N-aligned bytes, kbytes, characters, etc. Always good to qualify your identifiers with units IMO (or types that reflect units).
- zahlman 7mo ago> or is it just Rust just being confusing? It doesn't mean "byte length", so much as "byte" happens to be the element type. Unicode is conventionally represented as UTF-8, so the container can't be directly indexed to yield a character.