While processing the lines of the input file entries are pushed into the vec!
ath_data.reg = reg;
ath_data_vec.push(ath_data);
My problem is when trying to sort ath_data_vec using "sort_by_key" on the "reg" field.
I have two problems:
1/ All examples I have seen are for basic integer or strings.
I have finally got a clean compile with the following but does it sort? What about a reverse sort?
(&mut *ath_data_vec).sort_by_key(|k| k.sort());
2/ Trying to access data extracted from the vec with a get. Example
ath_get = ath_data_vec.get(1);
Returns "ath_get_vec(1)" as an "Option<&<unknown>> type.
Surely it should be AthData type???
Leonardo,
Thank you for your response. It certainly tidied up many things and the sort worked!
However I have a problem accessing a field of ath_get.
I can access ath_data.reg with "ath_data.reg = 10", but cannot access "ath_get.reg" after the ath_data_vec.get(1).
"ath_get" has a type "Option<&>, if I apply a type as follows:
"ath_get : AthData" (which I think is reasonable) the compiler complains with:
ath_data_vec.get(3);
| expected struct `AthData`, found enum `std::option::Option
If you use get() you get an Option<&AthData>, that could be None if the index is past the vec bounds. The problems you note come from AthData not being Copy and Rust enforcing borrowing rules. This is one way to modify your field:
fn main() {
let mut ath_data_vec = vec![AthData::default()];
let mut ath_data = AthData::new();
ath_data.reg = 10;
ath_data_vec.push(ath_data);
ath_data_vec.sort_by_key(|k| k.sort_field());
if let Some(ath_get) = ath_data_vec.get_mut(1) {
ath_get.reg = 11;
}
println!("{:?}", ath_data_vec);
}
Leonardo,
Once again thank you, all fixed and working. Options & Results have given me a lot of problems.
I have always thought that C derived languages are left handed while a lot of my experience is from the right handed "Wirthian" world.