this post was submitted on 15 Mar 2026
64 points (97.1% liked)
Rust
7855 readers
88 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
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.