From 091e1e7f7a1afad539479674e06cae7c8d8dab7f Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 27 Jun 2024 11:58:44 +0200 Subject: traits2 solution --- solutions/15_traits/traits2.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'solutions/15_traits') diff --git a/solutions/15_traits/traits2.rs b/solutions/15_traits/traits2.rs index 4e18198..0db93e0 100644 --- a/solutions/15_traits/traits2.rs +++ b/solutions/15_traits/traits2.rs @@ -1 +1,27 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +trait AppendBar { + fn append_bar(self) -> Self; +} + +impl AppendBar for Vec { + fn append_bar(mut self) -> Self { + // ^^^ this is important + self.push(String::from("Bar")); + self + } +} + +fn main() { + // You can optionally experiment here. +} + +#[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(), "Bar"); + assert_eq!(foo.pop().unwrap(), "Foo"); + } +} -- cgit v1.2.3