I'm trying to swap contents of a struct based on values passed in associated function. However, while using match
, arms do not seem to recognize the value parameter, and thus arms after first one become unreachable.
#[derive(Debug, Copy, Clone)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub fn swap(mut self, first: u8, second: u8) -> Color {
let mut swapped = self;
match self {
Color { r: first, g, b, a } => swapped.r = second,
Color { r, g: first, b, a } => swapped.g = second,
Color { r, g, b: first, a } => swapped.b = second,
Color { r, b, g, a: first } => swapped.a = second,
}
match self {
Color { r: second, g, b, a } => swapped.r = first,
Color { r, g: second, b, a } => swapped.g = first,
Color { r, g, b: second, a } => swapped.b = first,
Color { r, g, b, a: second } => swapped.a = first,
}
self = swapped;
self
}
}
However, if I put an actual u8 like Color { r: 10, g,b,a }
, then it works.
What am I doing wrong here?