Hello, I am kind of new to rust (i dove in a while back but not deep, and am trying again)
Here is a problem I encountered:
use std::collections::HashMap;
struct Translation
{
search: String,
replace: String
}
impl Translation {
fn make_map(&mut self) -> HashMap<char, char> {
let count = self.search.chars().count();
let results = self.replace.chars().count();
println!("keys: {}", count);
println!("vals: {}", results);
let mut keys = self.search.chars();
for key in keys {
println!("key: {:?}", key);
}
let mut values = self.replace.chars();
for val in values {
println!("val: {:?}", val);
}
keys = self.search.chars();
values = self.replace.chars();
println!("search: {}", self.search);
println!("replace: {}", self.replace);
let mut mapping = HashMap::new();
for index in 0..count {
let key = keys.nth(index);
let val = values.nth(index);
println!("index: {} key: {:?} val: {:?}", index, key, val);
if key.is_some() && val.is_some() {
mapping.insert(key.unwrap(), val.unwrap());
}
}
return mapping;
}
}
fn main() {
let mut tr = Translation {search: "abcde".to_string() , replace: "vwxyz".to_string()};
let map = tr.make_map();
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}
}
this gives the following output:
$ rustc map.rs ; ./map
keys: 5
vals: 5
key: 'a'
key: 'b'
key: 'c'
key: 'd'
key: 'e'
val: 'v'
val: 'w'
val: 'x'
val: 'y'
val: 'z'
search: abcde
replace: vwxyz
index: 0 key: Some('a') val: Some('v')
index: 1 key: Some('c') val: Some('x')
index: 2 key: None val: None
index: 3 key: None val: None
index: 4 key: None val: None
key: a val: v
key: c val: x
in the mapping of the strings I seem to lose most of my characters. What is wrong with my tactic?