summaryrefslogtreecommitdiff
path: root/exercises/15_traits
diff options
context:
space:
mode:
authorAdam Brewer <adamhb321@gmail.com>2023-10-16 07:37:12 -0400
committerAdam Brewer <adamhb321@gmail.com>2023-10-16 07:37:12 -0400
commit64d95837e9813541cf5b357de13865ce687ae98d (patch)
treef022c5d5ba01128811c0b77618a7adb843ee876b /exercises/15_traits
parentc3941323e2c0b9ee286494327de92e00f23b9e3a (diff)
Update Exercises Directory Names to Reflect Order
Diffstat (limited to 'exercises/15_traits')
-rw-r--r--exercises/15_traits/README.md19
-rw-r--r--exercises/15_traits/traits1.rs42
-rw-r--r--exercises/15_traits/traits2.rs29
-rw-r--r--exercises/15_traits/traits3.rs42
-rw-r--r--exercises/15_traits/traits4.rs49
-rw-r--r--exercises/15_traits/traits5.rs40
6 files changed, 221 insertions, 0 deletions
diff --git a/exercises/15_traits/README.md b/exercises/15_traits/README.md
new file mode 100644
index 0000000..ac87c64
--- /dev/null
+++ b/exercises/15_traits/README.md
@@ -0,0 +1,19 @@
+# Traits
+
+A trait is a collection of methods.
+
+Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the `String` data type implements the `From<&str>` trait. This allows a user to write `String::from("hello")`.
+
+In this way, traits are somewhat similar to Java interfaces and C++ abstract classes.
+
+Some additional common Rust traits include:
+
+- `Clone` (the `clone` method)
+- `Display` (which allows formatted display via `{}`)
+- `Debug` (which allows formatted display via `{:?}`)
+
+Because traits indicate shared behavior between data types, they are useful when writing generics.
+
+## Further information
+
+- [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html)
diff --git a/exercises/15_traits/traits1.rs b/exercises/15_traits/traits1.rs
new file mode 100644
index 0000000..37dfcbf
--- /dev/null
+++ b/exercises/15_traits/traits1.rs
@@ -0,0 +1,42 @@
+// traits1.rs
+//
+// Time to implement some traits! Your task is to implement the trait
+// `AppendBar` for the type `String`. The trait AppendBar has only one function,
+// which appends "Bar" to any object implementing this trait.
+//
+// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
+// hint.
+
+// I AM NOT DONE
+
+trait AppendBar {
+ fn append_bar(self) -> Self;
+}
+
+impl AppendBar for String {
+ // TODO: Implement `AppendBar` for type `String`.
+}
+
+fn main() {
+ let s = String::from("Foo");
+ let s = s.append_bar();
+ println!("s: {}", s);
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn is_foo_bar() {
+ assert_eq!(String::from("Foo").append_bar(), String::from("FooBar"));
+ }
+
+ #[test]
+ fn is_bar_bar() {
+ assert_eq!(
+ String::from("").append_bar().append_bar(),
+ String::from("BarBar")
+ );
+ }
+}
diff --git a/exercises/15_traits/traits2.rs b/exercises/15_traits/traits2.rs
new file mode 100644
index 0000000..3e35f8e
--- /dev/null
+++ b/exercises/15_traits/traits2.rs
@@ -0,0 +1,29 @@
+// traits2.rs
+//
+// Your task is to implement the trait `AppendBar` for a vector of strings. To
+// implement this trait, consider for a moment what it means to 'append "Bar"'
+// to a vector of strings.
+//
+// No boiler plate code this time, you can do this!
+//
+// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
+
+// I AM NOT DONE
+
+trait AppendBar {
+ fn append_bar(self) -> Self;
+}
+
+// TODO: Implement trait `AppendBar` for a vector of strings.
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn is_vec_pop_eq_bar() {
+ let mut foo = vec![String::from("Foo")].append_bar();
+ assert_eq!(foo.pop().unwrap(), String::from("Bar"));
+ assert_eq!(foo.pop().unwrap(), String::from("Foo"));
+ }
+}
diff --git a/exercises/15_traits/traits3.rs b/exercises/15_traits/traits3.rs
new file mode 100644
index 0000000..4e2b06b
--- /dev/null
+++ b/exercises/15_traits/traits3.rs
@@ -0,0 +1,42 @@
+// traits3.rs
+//
+// Your task is to implement the Licensed trait for both structures and have
+// them return the same information without writing the same function twice.
+//
+// Consider what you can add to the Licensed trait.
+//
+// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
+// hint.
+
+// I AM NOT DONE
+
+pub trait Licensed {
+ fn licensing_info(&self) -> String;
+}
+
+struct SomeSoftware {
+ version_number: i32,
+}
+
+struct OtherSoftware {
+ version_number: String,
+}
+
+impl Licensed for SomeSoftware {} // Don't edit this line
+impl Licensed for OtherSoftware {} // Don't edit this line
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn is_licensing_info_the_same() {
+ let licensing_info = String::from("Some information");
+ let some_software = SomeSoftware { version_number: 1 };
+ let other_software = OtherSoftware {
+ version_number: "v2.0.0".to_string(),
+ };
+ assert_eq!(some_software.licensing_info(), licensing_info);
+ assert_eq!(other_software.licensing_info(), licensing_info);
+ }
+}
diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs
new file mode 100644
index 0000000..4bda3e5
--- /dev/null
+++ b/exercises/15_traits/traits4.rs
@@ -0,0 +1,49 @@
+// traits4.rs
+//
+// Your task is to replace the '??' sections so the code compiles.
+//
+// Don't change any line other than the marked one.
+//
+// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
+// hint.
+
+// I AM NOT DONE
+
+pub trait Licensed {
+ fn licensing_info(&self) -> String {
+ "some information".to_string()
+ }
+}
+
+struct SomeSoftware {}
+
+struct OtherSoftware {}
+
+impl Licensed for SomeSoftware {}
+impl Licensed for OtherSoftware {}
+
+// YOU MAY ONLY CHANGE THE NEXT LINE
+fn compare_license_types(software: ??, software_two: ??) -> bool {
+ software.licensing_info() == software_two.licensing_info()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn compare_license_information() {
+ let some_software = SomeSoftware {};
+ let other_software = OtherSoftware {};
+
+ assert!(compare_license_types(some_software, other_software));
+ }
+
+ #[test]
+ fn compare_license_information_backwards() {
+ let some_software = SomeSoftware {};
+ let other_software = OtherSoftware {};
+
+ assert!(compare_license_types(other_software, some_software));
+ }
+}
diff --git a/exercises/15_traits/traits5.rs b/exercises/15_traits/traits5.rs
new file mode 100644
index 0000000..df18380
--- /dev/null
+++ b/exercises/15_traits/traits5.rs
@@ -0,0 +1,40 @@
+// traits5.rs
+//
+// Your task is to replace the '??' sections so the code compiles.
+//
+// Don't change any line other than the marked one.
+//
+// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
+// hint.
+
+// I AM NOT DONE
+
+pub trait SomeTrait {
+ fn some_function(&self) -> bool {
+ true
+ }
+}
+
+pub trait OtherTrait {
+ fn other_function(&self) -> bool {
+ true
+ }
+}
+
+struct SomeStruct {}
+struct OtherStruct {}
+
+impl SomeTrait for SomeStruct {}
+impl OtherTrait for SomeStruct {}
+impl SomeTrait for OtherStruct {}
+impl OtherTrait for OtherStruct {}
+
+// YOU MAY ONLY CHANGE THE NEXT LINE
+fn some_func(item: ??) -> bool {
+ item.some_function() && item.other_function()
+}
+
+fn main() {
+ some_func(SomeStruct {});
+ some_func(OtherStruct {});
+}