From 7d5dcc7e2b8b5896ac490b1d013021bf3157a725 Mon Sep 17 00:00:00 2001 From: parrrate Date: Thu, 9 Apr 2026 12:56:17 +0000 Subject: [PATCH] further simplify anystr solution --- src/exercises/anystr.md | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/exercises/anystr.md b/src/exercises/anystr.md index ad2a04c..6cb8a3e 100644 --- a/src/exercises/anystr.md +++ b/src/exercises/anystr.md @@ -7,43 +7,27 @@ Create something similar to [Python's `typing.AnyStr`]. Example of what it may allow (solution isn't required to pass these tests, but should): ```rust -# pub trait Equivalent { -# type T; -# fn st_ref(&self) -> &Self::T; -# fn st_mut(&mut self) -> &mut Self::T; -# fn st_move(self) -> Self::T; -# fn ts_ref(t: &Self::T) -> &Self; -# fn ts_mut(t: &mut Self::T) -> &mut Self; -# fn ts_move(t: Self::T) -> Self; -# } +# pub trait Equivalent { type T; } # pub trait Accepts { # type Output; # type E; # fn accept(self) -> Self::Output where Self::E: Equivalent; # } -# pub trait AnyStr:Equivalent{fn provide+Accepts,E=Self,Output=Output>,Output>(accepts:A)->Output;} -# impl Equivalent for T { -# type T = T; -# fn st_ref(&self) -> &Self::T { self } -# fn st_mut(&mut self) -> &mut Self::T { self } -# fn st_move(self) -> Self::T { self } -# fn ts_ref(t: &Self::T) -> &Self { t } -# fn ts_mut(t: &mut Self::T) -> &mut Self { t } -# fn ts_move(t: Self::T) -> Self { t } +# pub trait AnyStrRaw:Equivalent{fn provide+Accepts,E=Self,Output=Output>,Output>(accepts:A)->Output;} +# impl Equivalent for T { type T = T; } +# impl AnyStrRaw for String { +# fn provide,Output>(accepts:A)->Output{>::accept(accepts)} # } -# impl AnyStr for String { -# fn provide+Accepts,E=Self,Output=Output>,Output>(accepts:A)->Output{>::accept(accepts)} -# } -# impl AnyStr for Vec { -# fn provide+Accepts,E=Self,Output=Output>,Output>(accepts:A)->Output{>>::accept(accepts)} +# impl AnyStrRaw for Vec { +# fn provide,E=Self,Output=Output>,Output>(accepts:A)->Output{>>::accept(accepts)} # } # pub struct Concat(E::T, E::T); -# impl Accepts for Concat { +# impl Accepts for Concat { # type Output = E::T; # type E = E; # fn accept(self) -> Self::Output where E: Equivalent { self.0 + &self.1 } # } -# impl Accepts> for Concat { +# impl Accepts> for Concat { # type Output = E::T; # type E = E; # fn accept(self) -> Self::Output where E: Equivalent> { @@ -53,7 +37,9 @@ Example of what it may allow (solution isn't required to pass these tests, but s # a # } # } -# pub fn concat(a: T, b: T) -> T { T::ts_move(T::provide(Concat(a.st_move(), b.st_move()))) } +# pub trait AnyStr: AnyStrRaw {} +# impl> AnyStr for T {} +# pub fn concat(a: T, b: T) -> T { T::provide(Concat(a, b)) } assert_eq!(concat("ab".to_string(), "cd".to_string()), "abcd".to_string()); assert_eq!(concat(vec![2u8, 3u8], vec![5u8, 7u8]), vec![2u8, 3u8, 5u8, 7u8]); ```