The Rust Compiler is the most helpful one I have experienced so far

https://feddit.org/post/27154110

The Rust Compiler is the most helpful one I have experienced so far - feddit.org

I recently started learning rust, and I was ready for one hell of a fight. I heard all those horror Storys about the compiler complaining about every single detail and that developing rust means having a constant fight with the compiler about seemingly irrelevant things. However, so far I have to tell, that while its somewhat true, that the compiler is somewhat picky, it is incredibly helpful. Never before have I seen such good and helpful compiler messages. It not only says what you did wrong, but also gives direct help on what to do to fix your code. I also really like, that it gives you direct references to the rust book in the compiler messages. Prior to starting my journey with rust I did quite a lot of python, some C and some bash and their interpreter/compiler messages are nothing when comparing them with rust. Especially the bash error messages are awful if you do not know what they mean and how to fix them.

Definitely! The only other language where I felt the compiler actually helped me was Haskell. C and C++ just go like “something is fucked, you figure it out”.

I think the learning curve exists but it has been vastly overestimated by the rumours. I have many years of experience with Rust now, just ask if you are unsure of anything. Feel free to tag me in any post or PM me, then I’ll definitely see it.

Currently I am trying to figure out how to access and change single characters in a string (I am coding a basic version of hangman to get used to working with rust). The main problem I have is, that I can not figure out how to change the value of a single character in a string based on its index. I have a string (that has the same length as my “word to guess”) containing only underscores and whenever the user correctly guesses a character I want to replace the underscores by the guessed character to show the user how much he has guessed so far. I was able to turn the string into a char iterator, but I could not figure out how to change elements of said iterator (this can be seen at line 55).

The code is here: pastebin.com/kfSYWT42___

hangman_rs - Pastebin.com

Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

Pastebin

I was able to turn the string into a char iterator, but I could not figure out how to change elements of said iterator (this can be seen at line 55).

You have a few options here, but the easiest is to collect into a Vec<char>, replace the character there, then do a String::from_iter(chars) to get it back as a string.

You can also manipulate the original chars iterator directly through takes, skips, and so on and collect it into a string, but that’s more complicated.

Also, “character” is such a complicated concept because unicode is not simple. If you can work directly with bytes though, you can convert the string to a Vec<u8> (which is the underlying type for String), manipulate that directly, then do String::from_utf8 (or the same method for str) to convert it back to a string.

Since OP will want to do a lot of character based operations for the hangman game, she could also consider using Vec<char> as an internal representation everywhere and only converting into String for display purposes.
I agree, this makes the most sense. I was under the assumption they wanted to keep it as a String, but your suggestion is a better way to approach the problem if they’re able to do that.