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.