[HN Gopher] Why is capitalizing the first letter of a string so ...
       ___________________________________________________________________
        
       Why is capitalizing the first letter of a string so convoluted in
       Rust?
        
       Author : cjg
       Score  : 25 points
       Date   : 2021-06-09 19:25 UTC (3 hours ago)
        
 (HTM) web link (stackoverflow.com)
 (TXT) w3m dump (stackoverflow.com)
        
       | Someone wrote:
       | There's also Dutch, where 'ij' sort-of is a single letter, so
       | capitalizing "ijs" yields "IJs"
       | (https://en.wikipedia.org/wiki/IJ_(digraph)#Capitalisation)
       | 
       | Unicode has separate code points for those, but their use is
       | discouraged.
        
       | inshadows wrote:
       | I know nothing about Rust, and couldn't grok most of what was
       | said in the post, but I'm curious. Is this all just to please the
       | type system, and will it result in efficient code as if I did the
       | following in C (for some implementation of to_uppercase())?
       | c[0] = to_uppercase(c[0]);
        
         | ibraheemdev wrote:
         | Rust strings are guaranteed to be valid utf-8. Mutating a
         | string byte directly might result in an invalid String. You
         | perform a free conversion to bytes, mutate the slice, and then
         | perform an free but unsafe conversion back to String, or a
         | potentially expensive but safe (utf-8 validation) conversion.
         | If you want to handle non-ascii, then you must search the
         | string for unicode character indices, which is not free.
        
         | steveklabnik wrote:
         | Doing that in C is not Unicode aware, but Rust's language
         | built-in string type (&str) and main standard library string
         | type (String) are UTF-8.
         | 
         | You could write the same thing as the C if you wanted to, if
         | you had a string type that was encoded differently. Say, in
         | ASCII.
        
           | ibraheemdev wrote:
           | Yeah, when working with ascii, I just use `Vec<u8>`/`&[u8]`.
           | There's also the `ascii` crate which encodes ascii into the
           | type system (enums), but I find it easier to work with raw
           | bytes. The equivalent code in Rust would be:
           | let mut bytes = c.as_bytes();
           | bytes[0].make_ascii_uppercase();
        
       ___________________________________________________________________
       (page generated 2021-06-09 23:03 UTC)