I'm trying to make my struct's members accessible through an index like an array, so if I have a struct like this in rust:
pub struct SubjectsProgress {
pub sword: i32,
pub lance: i32,
pub axe: i32,
pub bow: i32,
pub brawl: i32,
pub reason: i32,
pub faith: i32,
pub authority: i32,
pub heavy_armor: i32,
pub riding: i32,
pub flying: i32,
}
I want to make it accessible to an index so I can do this:
impl Index<usize> for SubjectsProgress {
type Output = i32;
fn index(&self, i: usize) -> &i32 {
match i % 11 {
0 => &self.sword,
1 => &self.lance,
2 => &self.axe,
3 => &self.bow,
4 => &self.brawl,
5 => &self.reason,
6 => &self.faith,
7 => &self.authority,
8 => &self.heavy_armor,
9 => &self.riding,
10 => &self.flying,
_ => unreachable!(),
}
}
}
I do this by copying the struct members pub sword: i32,...
into the match statement, select them and replace pub \(.*\):.*
with n => \&self.\1,
So the full thing looks like this:
:'<,'>s/pub \(.*\):.*/n => \&self.\1,
.
However this doesn't match the match number, (0=>sword, 1=>lance,...
), so I have to manually replace n
. How do I keep the match number as well when replacing?