this post was submitted on 15 Mar 2026
61 points (96.9% liked)
Rust
7855 readers
85 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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: https://pastebin.com/kfSYWT42`___`
You have a few options here, but the easiest is to collect into a
Vec<char>, replace the character there, then do aString::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
collectit 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 forString), manipulate that directly, then doString::from_utf8(or the same method forstr) 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 intoStringfor 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.Others have already pointed out the issue - in Rust, String is UTF-8 encoded and therefore characters are variable length. So you can't just change a character in a string, as it may not fit (e.g. replacing 'a' with '🙂' would lead to trouble).
You can do what the others suggest, but honestly for a game like hangman, I'd suggest you just work directly with chars and don't use any string. As in, just use a Vec instead of a string. Then you can freely change characters based on index, but this representation uses more memory than a typical String. But this won't matter for your use case.
Working with a vector directly would probably be easier, however for a learning experience this was actually great. I read through quite a lot of documentation trying to figure out how I achieve what I want. Might reimplement it with vectors later just for the learning experience.
@cows_are_underrated The simplest way I’ve found to do that is to convert the string into a Vec<char>. UTF-8 strings aren’t optimized for random access because of multi-byte chars.
To array of chars:
let c = s.chars().collect::<Vec<char>>();
And back to string:
let s = c.into_iter().collect::<String>();
That worked. Thanks :3
what is character?
This is simply not possible in rust, because strings are utf-8, so the size of a single character can vary. I think there might be a class for ASCII strings, but otherwise you can just use Vev and then convert to string