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

#1

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() {
        sv.push(i);
    }
*/

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

}
0 Likes

#2

Hi, i’m not entirely sure what you are trying to accomplish, but i made a version of your code that compiles:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=856cb75d32ddb5b3c96fd0c6e921b657

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
https://docs.rs/rand/0.6.5/rand/seq/trait.SliceRandom.html#tymethod.shuffle

1 Like

#3

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:

0 Likes

#4

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() {
        str_vec.push(i);
    }
    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.

0 Likes

#5

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!

0 Likes

#6

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ba9088c17bdfdae067e64cdc0fa40b14

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.

2 Likes

#7

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.

2 Likes