mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-01 00:00:02 +03:00
60 lines
1.4 KiB
Rust
60 lines
1.4 KiB
Rust
// structs1.rs
|
|
//
|
|
// Address all the TODOs to make the tests pass!
|
|
//
|
|
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
|
// hint.
|
|
|
|
struct ColorClassicStruct {
|
|
// TODO: Something goes here
|
|
red: i32,
|
|
green: i32,
|
|
blue: i32,
|
|
}
|
|
|
|
struct ColorTupleStruct(i32, i32, i32);
|
|
|
|
#[derive(Debug)]
|
|
struct UnitLikeStruct;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn classic_c_structs() {
|
|
// TODO: Instantiate a classic c struct!
|
|
let green = ColorClassicStruct {
|
|
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() {
|
|
// TODO: 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() {
|
|
// TODO: Instantiate a unit-like struct!
|
|
// does not hold data but can be used to improve type safety
|
|
// best used when you don't need data but you want to define methods
|
|
// Great example of this is errors.
|
|
let unit_like_struct = UnitLikeStruct;
|
|
let message = format!("{:?}s are fun!", unit_like_struct);
|
|
|
|
assert_eq!(message, "UnitLikeStructs are fun!");
|
|
}
|
|
}
|