From 0b6565c12aed9084b741cfda033d6bccf27c1efe Mon Sep 17 00:00:00 2001 From: parrrate Date: Fri, 12 Sep 2025 18:33:45 +0000 Subject: [PATCH] `multiple_blanket.md` --- .gitignore | 13 +++++++ Cargo.toml | 8 ++++ src/SUMMARY.md | 1 + src/exercises/multiple_blanket.md | 64 +++++++++++++++++++++++++++++++ src/lib.rs | 14 +++++++ 5 files changed, 100 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/exercises/multiple_blanket.md create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore index 7585238..ef05725 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,14 @@ book + + +# Added by cargo + +/target + + +# Added by cargo +# +# already existing elements were commented out + +#/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d51cd24 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "exercises" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7390c82..d6e08d3 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,3 +1,4 @@ # Summary - [Chapter 1](./chapter_1.md) + - [`A`/`A1`/`A2`](./exercises/multiple_blanket.md) diff --git a/src/exercises/multiple_blanket.md b/src/exercises/multiple_blanket.md new file mode 100644 index 0000000..bc601f0 --- /dev/null +++ b/src/exercises/multiple_blanket.md @@ -0,0 +1,64 @@ +```rust +trait A { + fn a_ref(&self) -> u64; + fn a_mut(&mut self) -> bool; + fn a_move(self) -> Self; +} + +fn test(x: impl A) { + x.a_ref(); + let mut x = x.a_move(); + x.a_mut(); +} + +fn test1(x: impl A1) { + test(x) +} + +fn test2(x: impl A2) { + test(x) +} +# use std::marker::PhantomData; +# trait Wrapper: Sized {} +# #[repr(transparent)] +# struct Wrapped(PhantomData, T); +# trait AProxy { +# type T; +# fn ap_ref(t: &Self::T) -> u64; +# fn ap_mut(t: &mut Self::T) -> bool; +# fn ap_move(t: Self::T) -> Self::T; +# } +# trait AWrapper: Wrapper { type AWrapped; } +# impl A for T where T::AWrapped: AProxy, +# { +# fn a_ref(&self) -> u64 { ::ap_ref(self) } +# fn a_mut(&mut self) -> bool { ::ap_mut(self) } +# fn a_move(self) -> Self { ::ap_move(self) } +# } +# trait A1: AWrapper> { +# fn a1_ref(&self) -> u64; +# fn a1_mut(&mut self) -> bool; +# fn a1_move(self) -> Self; +# } +# trait A2: AWrapper> { +# fn a2_ref(&self) -> u64; +# fn a2_mut(&mut self) -> bool; +# fn a2_move(self) -> Self; +# } +# struct Av1; +# struct Av2; +# type Aw1 = Wrapped; +# type Aw2 = Wrapped; +# impl AProxy for Aw1 { +# type T = T; +# fn ap_ref(t: &Self::T) -> u64 { t.a1_ref() } +# fn ap_mut(t: &mut Self::T) -> bool { t.a1_mut() } +# fn ap_move(t: Self::T) -> Self::T { t.a1_move() } +# } +# impl AProxy for Aw2 { +# type T = T; +# fn ap_ref(t: &Self::T) -> u64 { t.a2_ref() } +# fn ap_mut(t: &mut Self::T) -> bool { t.a2_mut() } +# fn ap_move(t: Self::T) -> Self::T { t.a2_move() } +# } +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +}