[SOLVED] Word scrambler, mismatched types even though they are both chars


Hello Rustacians! I’m trying to make a word scrambler, not the full game yet. I am just working on the initial scrambling of the word. I have seen many examples of this and I think I might have an idea, although the logic is still a little cloudy. I can’t get this code to compile due to mismatched types which have been really messing with me. I have posted my code below, please tell me I’m at least on the right track. :slight_smile:

use rand::prelude::*;

fn main() {
    let scramble_string = String::from("Hello");

    let random_thing = rand::thread_rng().gen_range(0, scramble_string.len() + 1);


    let mut sv1: Vec<char> = Vec::new();
    let vlen = 5;

    let sv2 = vec![" ".chars(); vlen];
    for i in scramble_string.chars() {

    for i in scramble_string.chars() {
        sv2.push(sv1[random_thing];  //Mismatched types eventhough sv1 and sv2 are Vec<char>?
    println!("{:?}", sv2);  //Word should be scramble now



Hi, i’m not entirely sure what you are trying to accomplish, but i made a version of your code that compiles:

sv2 in your example is not actually a Vec<char> but a Vec<Chars>, because " ".chars() returns a Chars, which is a iterator over multiple char. To get just the single space character as a char, you can use single quotes.

If your goal is to just shuffle all characters in a new random order, have a look at

1 Like


Thanks mmmmib for your anwser, as the title says I’m trying to accomplish writing a program that takes a string and scrambles the specific chars randomly and then prints it out. A word scrambler, if you don’t know what I’m trying to accomplish by looking at my code then that tell me I’m not even going in the right direction. I’m WAY off :slight_smile:



I must not have the logic down right.

Take this code:

fn main() {
    let my_str = String::from("Hello");
    let _str_len = my_str.len();
    let str_vec = vec![]

    for i in my_str.chars() {
    println!("{:?}", str_vec);

The above code takes the “Hello” and puts the characters into a Vec. So now I have access to each character. But from here I have not idea what to do.

I know I need the index in str_vec[index] to be randomized, but I don’t know how to accomplish this.



And looking at the link you showed me, it looks like the work it done for me. I’m really good at finding the hardest way possible to accomplish something. Thanks again!




note that chosing the correct indices is acutally nontrivial if you implement it yourself, because you need each index to occur exactly once. however, the .shuffle method takes care of that internally, so you can just use that.



Beautiful, thats what I have been pulling my hair out about for a few days, and all the time the work has already been done by rand. :laughing:

Thanks a lot @mmmmib you helped a great deal.