Files
rustlings/exercises/07_structs/structs1.rs
T

55 lines
1.2 KiB
Rust
Raw Normal View History

2026-03-12 20:54:14 -04:00
struct ColorRegularStruct {
// TODO: Add the fields that the test `regular_structs` expects.
// What types should the fields have? What are the minimum and maximum values for RGB colors?
2026-03-23 03:36:33 -04:00
red: u8,
green: u8,
blue: u8,
2026-03-12 20:54:14 -04:00
}
2026-03-23 03:36:33 -04:00
struct ColorTupleStruct(u8, u8, u8);
2026-03-12 20:54:14 -04:00
#[derive(Debug)]
2026-03-23 03:36:33 -04:00
struct UnitStruct();
2026-03-12 20:54:14 -04:00
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn regular_structs() {
// TODO: Instantiate a regular struct.
2026-03-23 03:36:33 -04:00
let green = ColorRegularStruct {
red: 0,
green: 255,
blue: 0,
};
2026-03-12 20:54:14 -04:00
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
assert_eq!(green.blue, 0);
}
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct.
2026-03-23 03:36:33 -04:00
let green = ColorTupleStruct(0, 255, 0);
2026-03-12 20:54:14 -04:00
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
assert_eq!(green.2, 0);
}
#[test]
fn unit_structs() {
// TODO: Instantiate a unit struct.
2026-03-23 03:36:33 -04:00
let unit_struct = UnitStruct();
2026-03-12 20:54:14 -04:00
let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitStructs are fun!");
}
}