| ▲ | dataflow 3 hours 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 3 hours ago | parent | next [-] | |
In 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. | ||
| ▲ | wyldfire an hour ago | parent | prev | next [-] | |
A 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). | ||
| ▲ | AlotOfReading 3 hours ago | parent | prev [-] | |
It'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. | ||