3 ms·
How would the compiler determine, in this case, that the programmer didn't need the fancier to_lower case function? At best the compiler could ASK the programm
by gameswithgo 6y ago
How would the compiler determine, in this case, that the programmer didn't need the fancier to_lower case function?
At best the compiler could ASK the programmer about it.
- flohofwoe 6y agoIt's possible to design a standard library which makes it obvious to the user when a function allocates memory under the hood, for instance by taking an allocator argument. That way it becames very clear what "simple" functions are potentially expensive, and writing potentially slow code should be much less convenient than writing potentially fast code so that users are pushed to writing fast code, same as Rust pushes them to write memory-safe code.
- steveklabnik 6y agoThis is sorta Zig’s wheelhouse.
- DougBTX 6y agoIn Rust to_lowercase returns a String, which is an owned struct with heap allocated data, so it is reasonably clear that it allocates. In contrast, trim_start for example returns a reference to a string slice with the same lifetime as the source string, so again it is reasonably obvious from the type signature what's going on. The mistake here really is trying to do case insensitive string comparison by converting both arguments to the same case, then comparing them with case-sensitivity. It is much better to compare them directly, but case-insensitively.