public/subjects/rgb_match/README.md

84 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2020-12-29 10:40:42 +00:00
## rgb_match
### Instructions
2022-05-30 14:32:55 +00:00
Implement the struct `Color` with the associated function `swap`. This function returns a `Color` with the matching values swapped.
2020-12-29 10:40:42 +00:00
### Expected functions
```rust
#[derive(Debug, Clone, Copy, PartialEq)]
2021-03-16 01:56:54 +00:00
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
2020-12-29 10:40:42 +00:00
impl Color {
2021-03-16 01:56:54 +00:00
pub fn swap(mut self, first: u8, second: u8) -> Color {
2020-12-29 10:40:42 +00:00
}
2021-03-16 01:56:54 +00:00
}
2020-12-29 10:40:42 +00:00
```
### Usage
Here is a program to test your function.
```rust
2021-03-16 01:56:54 +00:00
use rgb_match::*;
2020-12-29 10:40:42 +00:00
fn main() {
let c = Color {
r: 255,
g: 200,
b: 10,
a: 30,
};
println!("{:?}", c.swap(c.r, c.b));
println!("{:?}", c.swap(c.r, c.g));
println!("{:?}", c.swap(c.r, c.a));
println!();
println!("{:?}", c.swap(c.g, c.r));
println!("{:?}", c.swap(c.g, c.b));
println!("{:?}", c.swap(c.g, c.a));
println!();
println!("{:?}", c.swap(c.b, c.r));
println!("{:?}", c.swap(c.b, c.g));
println!("{:?}", c.swap(c.b, c.a));
println!();
println!("{:?}", c.swap(c.a, c.r));
println!("{:?}", c.swap(c.a, c.b));
println!("{:?}", c.swap(c.a, c.g));
}
```
2021-03-16 01:56:54 +00:00
And its output:
2020-12-29 10:40:42 +00:00
```console
$ cargo run
2020-12-29 10:40:42 +00:00
Color { r: 10, g: 200, b: 255, a: 30 }
Color { r: 200, g: 255, b: 10, a: 30 }
Color { r: 30, g: 200, b: 10, a: 255 }
Color { r: 200, g: 255, b: 10, a: 30 }
Color { r: 255, g: 10, b: 200, a: 30 }
Color { r: 255, g: 30, b: 10, a: 200 }
Color { r: 10, g: 200, b: 255, a: 30 }
Color { r: 255, g: 10, b: 200, a: 30 }
Color { r: 255, g: 200, b: 30, a: 10 }
Color { r: 30, g: 200, b: 10, a: 255 }
Color { r: 255, g: 200, b: 30, a: 10 }
Color { r: 255, g: 30, b: 10, a: 200 }
$
2020-12-29 10:40:42 +00:00
```
2022-05-30 14:32:55 +00:00
### Notions
- [patterns](https://doc.rust-lang.org/book/ch18-00-patterns.html)