rustlings/exercises/07_structs/structs1.rs

54 lines
1.1 KiB
Rust
Raw Permalink Normal View History

2024-10-28 20:46:17 +00:00
struct ColorRegularStruct {
// What types should the fields have? What are the minimum and maximum values for RGB colors?
red: i32,
green: i32,
blue: i32
}
struct ColorTupleStruct(i32, i32, i32);
#[derive(Debug)]
struct UnitStruct;
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn regular_structs() {
//
let green = ColorRegularStruct {
red: 0,
green: 255,
blue: 0,
};
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
assert_eq!(green.blue, 0);
}
#[test]
fn tuple_structs() {
// Instantiate a tuple struct.
let green = ColorTupleStruct(0, 255, 0);
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
assert_eq!(green.2, 0);
}
#[test]
fn unit_structs() {
// Instantiate a unit struct.
let unit_struct = UnitStruct;
let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitStructs are fun!");
}
}