From 64d95837e9813541cf5b357de13865ce687ae98d Mon Sep 17 00:00:00 2001 From: Adam Brewer Date: Mon, 16 Oct 2023 07:37:12 -0400 Subject: Update Exercises Directory Names to Reflect Order --- exercises/14_generics/README.md | 11 +++++++++++ exercises/14_generics/generics1.rs | 14 ++++++++++++++ exercises/14_generics/generics2.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 exercises/14_generics/README.md create mode 100644 exercises/14_generics/generics1.rs create mode 100644 exercises/14_generics/generics2.rs (limited to 'exercises/14_generics') diff --git a/exercises/14_generics/README.md b/exercises/14_generics/README.md new file mode 100644 index 0000000..de46d50 --- /dev/null +++ b/exercises/14_generics/README.md @@ -0,0 +1,11 @@ +# Generics + +Generics is the topic of generalizing types and functionalities to broader cases. +This is extremely useful for reducing code duplication in many ways, but can call for rather involving syntax. +Namely, being generic requires taking great care to specify over which types a generic type is actually considered valid. +The simplest and most common use of generics is for type parameters. + +## Further information + +- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html) +- [Bounds](https://doc.rust-lang.org/rust-by-example/generics/bounds.html) diff --git a/exercises/14_generics/generics1.rs b/exercises/14_generics/generics1.rs new file mode 100644 index 0000000..35c1d2f --- /dev/null +++ b/exercises/14_generics/generics1.rs @@ -0,0 +1,14 @@ +// generics1.rs +// +// This shopping list program isn't compiling! Use your knowledge of generics to +// fix it. +// +// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a +// hint. + +// I AM NOT DONE + +fn main() { + let mut shopping_list: Vec = Vec::new(); + shopping_list.push("milk"); +} diff --git a/exercises/14_generics/generics2.rs b/exercises/14_generics/generics2.rs new file mode 100644 index 0000000..074cd93 --- /dev/null +++ b/exercises/14_generics/generics2.rs @@ -0,0 +1,34 @@ +// generics2.rs +// +// This powerful wrapper provides the ability to store a positive integer value. +// Rewrite it using generics so that it supports wrapping ANY type. +// +// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a +// hint. + +// I AM NOT DONE + +struct Wrapper { + value: u32, +} + +impl Wrapper { + pub fn new(value: u32) -> Self { + Wrapper { value } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn store_u32_in_wrapper() { + assert_eq!(Wrapper::new(42).value, 42); + } + + #[test] + fn store_str_in_wrapper() { + assert_eq!(Wrapper::new("Foo").value, "Foo"); + } +} -- cgit v1.2.3