1

So I've been scratching my head for a couple of hours with this problem. After reading the chapter on slices in the rust book. I tried to concat strings inside a loop.

fn append_words(times: u16){
    let mut final_word = String::new();
    let word: &str = "aahing ";
    for _ in 0..times {
        let tmp = format!("{}{}", &mut final_word[..], word);
        final_word.push_str(&tmp[..]);
        println!("{}", final_word);
    }
}

Output

aahing 
aahing aahing aahing 
aahing aahing aahing aahing aahing aahing aahing 

This works just fine.

The problem arises when instead of concatenating a &str that is declared inside the function, I try to use an element from a vector

fn select_words(words: Vec<&str>, times: u16){
    let mut final_word = String::new();
    println!("{}", words[5]);
    for _ in 0..times {
        let tmp = format!("{}{}", &mut final_word[..], words[5]);
        final_word.push_str(&tmp[..]);
    }
    println!("{}", final_word);
}

Output

aahing
aahing

Instead of concatenating and showing something like the first output, the string it is overwriting it. I think this is the case because I started by trying to concat different words in the Vec<&str>, but it wrote over them in each iteration.

I found these similar questions (the first one is really helpful), but again, when not using Vec<&str> it works just fine. How do I concatenate strings? Appending to a string inside a for loop

Am I not taking into account ownership or something like that?

Thank you in advance, I'm making my way through the rust book so I'm still a rust noob

Edit:

The way I'm building the vector is from a file, also this is the way I'm calling the function.

use structopt::StructOpt;
use structopt::clap;

#[derive(StructOpt)]
#[structopt(setting = clap::AppSettings::InferSubcommands)]
struct Cli {
    #[structopt(short = "f", long = "file",
                help="get words from specific file",
                default_value="./words.txt",
                parse(from_os_str))]
    file: std::path::PathBuf,
}

fn main() {
    let args = Cli::from_args();
    let words = std::fs::read_to_string(&args.file)
        .expect("could not read file");
    let word_list = words.split('\n');
    let words_vec: Vec<&str> = word_list.collect();
    select_words(words_vec, 3);
}

The file has this format

a
aa
aaa
aah
aahed
aahing
aahs

I think that the “building the vector” part works just fine.

bsantanad
  • 21
  • 4
  • Your code looks okay from what I can see. How are you calling `select_words`, what value of `times` are you passing? It would help if you could make a complete reproducible example. – loganfsmyth Jul 29 '21 at 00:11
  • sure thing :) let me upload that part as well – bsantanad Jul 29 '21 at 00:11
  • [This example on the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=285665addebfbcb43786d0cb075eeed9) shows that `select_words` seems to work as expected. – kmdreko Jul 29 '21 at 00:50
  • @kmdreko you are absolutely right, the problem was on the building the vector part, the vector ended up something like this `["a\r", "aa\r"]`, with the `\r` character, that was messing my output, after removing it, all works now :). Sorry for wasting your time – bsantanad Jul 29 '21 at 01:01

1 Answers1

1

Both functions were working just fine as pointed by @kmdreko and @loganfsmyth, the problem was the way I was building the vector.

There was a hidden character at the end of each element \r, after removing it. It all works now

Thanks to everyone :)

PS: I should have made a dos2unix beforehand.

bsantanad
  • 21
  • 4