structs1 solution

This commit is contained in:
mo8it 2024-06-21 22:22:37 +02:00
parent d768353806
commit ef842d3a94
3 changed files with 65 additions and 19 deletions

View file

@ -1,13 +1,12 @@
// Address all the TODOs to make the tests pass! struct ColorRegularStruct {
// TODO: Add the fields that the test `regular_structs` expects.
struct ColorClassicStruct { // What types should the fields have? What are the minimum and maximum values for RGB colors?
// TODO: Something goes here
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
#[derive(Debug)] #[derive(Debug)]
struct UnitLikeStruct; struct UnitStruct;
fn main() { fn main() {
// You can optionally experiment here. // You can optionally experiment here.
@ -18,8 +17,8 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn classic_c_structs() { fn regular_structs() {
// TODO: Instantiate a classic c struct! // TODO: Instantiate a regular struct.
// let green = // let green =
assert_eq!(green.red, 0); assert_eq!(green.red, 0);
@ -29,7 +28,7 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! // TODO: Instantiate a tuple struct.
// let green = // let green =
assert_eq!(green.0, 0); assert_eq!(green.0, 0);
@ -39,10 +38,10 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit-like struct! // TODO: Instantiate a unit struct.
// let unit_like_struct = // let unit_struct =
let message = format!("{:?}s are fun!", unit_like_struct); let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitLikeStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");
} }
} }

View file

@ -402,15 +402,14 @@ hint = """
Rust has more than one type of struct. Three actually, all variants are used to Rust has more than one type of struct. Three actually, all variants are used to
package related data together. package related data together.
There are normal (or classic) structs. These are named collections of related There are regular structs. These are named collections of related data stored in
data stored in fields. fields.
Tuple structs are basically just named tuples. Tuple structs are basically just named tuples.
Finally, Unit-like structs. These don't have any fields and are useful for Finally, unit structs. These don't have any fields and are useful for generics.
generics.
In this exercise you need to complete and implement one of each kind. In this exercise, you need to complete and implement one of each kind.
Read more about structs in The Book: Read more about structs in The Book:
https://doc.rust-lang.org/book/ch05-01-defining-structs.html""" https://doc.rust-lang.org/book/ch05-01-defining-structs.html"""

View file

@ -1 +1,49 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 struct ColorRegularStruct {
red: u8,
green: u8,
blue: u8,
}
struct ColorTupleStruct(u8, u8, u8);
#[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() {
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() {
let unit_struct = UnitStruct;
let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitStructs are fun!");
}
}