I build a Vec and then want to process those elements. Processing means changing the result field of IndividualTestResults type. I've danced around all kinds of errors between mutability and borrowing and can't seem to make progress. I'm guessing this is one of those learning curve things where vec in rust aren't like lists in other languages.
So my question is how can I change result field of an IndividualTestResults instance in vector?
eg:
let test_name: String = str2string!("test_one");
let mut failed : TestSet = TestSet::new();
let test = IndividualTestResults { name : test_name.to_string(), result : str2string!("")};
failed.push(test);
failed.update_results(test_name, str2string!("new results text));
Here's my record type:
pub struct IndividualTestResults {
pub name : String,
pub result : String
}
impl PartialEq for IndividualTestResults {
fn eq(&self, other: &IndividualTestResults) -> bool {
return self.name == other.name;
}
fn ne(&self, other: &IndividualTestResults) -> bool {
return self.name != other.name;
}
}
and here's my collection, based on Vec:
pub type TestSet = Vec<IndividualTestResults>;
pub trait TestSetFunctions {
fn new() -> TestSet;
fn find_by_name(&self, test_name : &String) -> &IndividualTestResults;
fn index_of(&self, test_name: &String) -> usize;
fn update_results(&mut self, test_name: &String, test_results: &String);
}
impl TestSetFunctions for TestSet {
fn new() -> TestSet {
return Vec::new();
}
fn find_by_name(&self, test_name : &String) -> &IndividualTestResults {
for test in self {
if test_name.to_string() == test.name {
return test;
}
}
panic!(format!("{} not found.", test_name))
}
fn index_of(&self, test_name: &String) -> usize {
for index in 0..self.len() {
if test_name.to_string() == self[index].name {
return index;
}
}
panic!(format!("{} not found.", test_name))
}
fn update_results(&mut self, test_name: &String, test_results: &String) {
let test = self.find_by_name(test_name); **// immutable borrow occurs here....?**
let new_test: IndividualTestResults = IndividualTestResults {
name: test.name.to_string(),
result : test_results.to_string(),
};
let index = self.index_of(test_name);
self.remove(index);
self.push(new_test);
}
}
The errors I get are:
let test = self.find_by_name(test_name); // immutable borrow occurs here....?
self.push(new_test); // mutable borrow occurs here
I apologize this is such a mess. I dug myself into a mess on this one.
Thnx
Matt