This link gives a good, if technical breakdown of why that is: doc.rust-lang.org/book/ch08-02-strings.html#storing-utf-8-encoded-text-with-strings The brief (and mangled) version is that international character sets may store a single display character as multiple bytes... they may even store the diacritic marks separately from the character! So instead of giving you a default way of indexing a string that is likely to lead you into trouble down the road (say you make an international app), they make you deal with the trouble up-front (which seems to be an operating principle in the language, come to think of it.)
Hi Domenic. Great videos. I'm trying to understand why you would do the last code snippet there (get char at index). That seems like a lot of code to print out the 4 element of a string. I know you explained that my_string[4] would work. I'm just trying to imagine scenarios where you would use this.
Hey Steve, thanks mate :) - with that code snippet, I was simply demonstrating the "safe" way to do it. I've noticed the Rust community tends to really encourage safe code like this and so I thought it would be a good idea to demonstrate that. After doing a bit of research it looks like you can't actually use array notation in Rust to get characters at a particular index.
Check the video I did earlier on Strings here: th-cam.com/video/ABYdoxzNJJ8/w-d-xo.html
simple and to the point, thank you :)
Finally someone actually talking about using strings and not just talking about what slices are.
Hi, are you planning on continuing this series?
Why, in the replacement example, my_string doesn't have to be mutable?
how can i remove duplicates from a string??
At 5:20 i was saying "that wont work, that wont work", then i was confused when it ran then realized he didn't print the vector
Can you explain the filter function?
+Jack Smith I haven't heard of the filter function on strings, do you have a link for documentation on it?
how come I couldn't find how to get a character at an index on google, does no one need it ?
This link gives a good, if technical breakdown of why that is: doc.rust-lang.org/book/ch08-02-strings.html#storing-utf-8-encoded-text-with-strings
The brief (and mangled) version is that international character sets may store a single display character as multiple bytes... they may even store the diacritic marks separately from the character! So instead of giving you a default way of indexing a string that is likely to lead you into trouble down the road (say you make an international app), they make you deal with the trouble up-front (which seems to be an operating principle in the language, come to think of it.)
Line 5: my_string is not mut and on line 5 is replace of substring. I though that it is not possible on const var.
The reason why it worked is because `.replace` returns a value instead of changing the variable
How come you dont have to implement Err(e) on the match my_string.chars().nth(4) ?
that is because the nth method returns an Option enum not a Result
how to join vector chars to one string?
yourvector.to_iter().collect()
Hi Domenic. Great videos. I'm trying to understand why you would do the last code snippet there (get char at index). That seems like a lot of code to print out the 4 element of a string. I know you explained that my_string[4] would work. I'm just trying to imagine scenarios where you would use this.
Hey Steve, thanks mate :) - with that code snippet, I was simply demonstrating the "safe" way to do it. I've noticed the Rust community tends to really encourage safe code like this and so I thought it would be a good idea to demonstrate that. After doing a bit of research it looks like you can't actually use array notation in Rust to get characters at a particular index.