From d9bec9da0822ad35e91a6ec5507210611305ece6 Mon Sep 17 00:00:00 2001 From: timofey <tim@ongoteam.yaconnect.com> Date: Sat, 26 Aug 2023 15:26:33 +0000 Subject: [PATCH] fix formatting --- src/exercises/anystr.md | 1 + src/exercises/bind.md | 1 + src/exercises/bool_stream.md | 3 +++ src/exercises/composition.md | 2 ++ src/exercises/duration.md | 2 ++ src/exercises/fromref.md | 2 ++ src/exercises/mode.md | 1 + src/exercises/multiple_blanket.md | 3 +++ src/exercises/rcchars.md | 11 ++++------- src/exercises/refbind.md | 3 ++- src/overview.md | 5 ++++- src/topics.md | 1 - 12 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/exercises/anystr.md b/src/exercises/anystr.md index ae187cb..ea76c7d 100644 --- a/src/exercises/anystr.md +++ b/src/exercises/anystr.md @@ -5,6 +5,7 @@ Create something similar to [Python's `typing.AnyStr`]. [Python's `typing.AnyStr`]: https://docs.python.org/3/library/typing.html#typing.AnyStr Example of what it may allow (solution isn't required to pass these tests, but should): + ```rust # pub trait Equivalent { # type T; diff --git a/src/exercises/bind.md b/src/exercises/bind.md index b33779b..c17421c 100644 --- a/src/exercises/bind.md +++ b/src/exercises/bind.md @@ -1,4 +1,5 @@ Make this compile and pass tests: + ```rust # mod __ { fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> { diff --git a/src/exercises/bool_stream.md b/src/exercises/bool_stream.md index b138d99..a9db415 100644 --- a/src/exercises/bool_stream.md +++ b/src/exercises/bool_stream.md @@ -1,10 +1,12 @@ # Introducing variance to objects Make `test_covariance` compile by making `BoolStream<'a>` covariant over `'a`. Restrictions: + - Can only change implementation details of `BoolStream` and its methods and add extra items outside of what's given, i.e. no signature/test change. - Changed version must behave the same way as the original. Consider the following code: + ```rust,compile_fail pub struct BoolStream<'a>( Box<dyn 'a + FnOnce() -> (bool, BoolStream<'a>)>, @@ -24,6 +26,7 @@ fn test_covariance<'a: 'b, 'b>(e: BoolStream<'a>) -> BoolStream<'b> { e } ``` + Why does it fail to compile? ```rust diff --git a/src/exercises/composition.md b/src/exercises/composition.md index af14bf1..ace2a5d 100644 --- a/src/exercises/composition.md +++ b/src/exercises/composition.md @@ -1,3 +1,5 @@ +# Monad composition? + ```rust # use std::ops::ControlFlow; trait Monad<'a>: 'a { diff --git a/src/exercises/duration.md b/src/exercises/duration.md index 2687269..1c62826 100644 --- a/src/exercises/duration.md +++ b/src/exercises/duration.md @@ -1,6 +1,7 @@ # `const` `Add`? Define `FIVE_SECONDS` as `const`: + ```rust # use std::time::Duration; # const fn unwrap_time(d: Option<Duration>) -> Duration { match d { Some(d) => d, None => panic!() } } @@ -13,6 +14,7 @@ assert_eq!(FIVE_SECONDS, Duration::from_secs(5)); ``` Try solving it in the playground: + ```rust,editable,compile_fail use std::time::Duration; diff --git a/src/exercises/fromref.md b/src/exercises/fromref.md index 88600ba..8c93afd 100644 --- a/src/exercises/fromref.md +++ b/src/exercises/fromref.md @@ -1,4 +1,5 @@ Make this compile and pass tests: + ```rust fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T { let s = "te".to_string() + "st"; @@ -16,6 +17,7 @@ assert_eq!(string, "test".to_string()); ``` Try solving it in the playground: + ```rust,editable,compile_fail fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T { f("test") diff --git a/src/exercises/mode.md b/src/exercises/mode.md index e105522..268138e 100644 --- a/src/exercises/mode.md +++ b/src/exercises/mode.md @@ -1,4 +1,5 @@ # Deserialisation modes + Merge implementations of `ConsumesStream` and `Deterministic` for tuple. It's recommended to first solve [this exercise](./multiple_blanket.md). diff --git a/src/exercises/multiple_blanket.md b/src/exercises/multiple_blanket.md index 3ad3b49..ab7ec15 100644 --- a/src/exercises/multiple_blanket.md +++ b/src/exercises/multiple_blanket.md @@ -1,5 +1,7 @@ # Blanket, blanket, blanket + Make this compile with the following restrictions: + - **Can't edit definitions of `A`, `test`, `tes1`, `test2`; can only add code outside of those items.** - **Some types are `A1` but not `A2`.** - **Some types are `A2` but not `A1`.** @@ -75,6 +77,7 @@ fn test2(x: impl A2) { ``` You may assume that `A`, `A1`, `A2` can be in separate crates with a dependency graph equivalent to this: + ```rust # mod __ { mod a1 { diff --git a/src/exercises/rcchars.md b/src/exercises/rcchars.md index 19ee5de..de6a1b4 100644 --- a/src/exercises/rcchars.md +++ b/src/exercises/rcchars.md @@ -15,16 +15,12 @@ # impl RcChars { # pub fn from_rc(rc: std::rc::Rc<String>) -> Self { # let mut new = Self { _rc: rc, chars: std::mem::MaybeUninit::uninit() }; -# new.chars .write(unsafe { &*std::rc::Rc::as_ptr(&new._rc) }.chars()); +# new.chars.write(unsafe { &*std::rc::Rc::as_ptr(&new._rc) }.chars()); # new # } # } -# impl From<std::rc::Rc<String>> for RcChars { -# fn from(value: std::rc::Rc<String>) -> Self { Self::from_rc(value) } -# } -# impl Drop for RcChars { -# fn drop(&mut self) { unsafe { self.chars.assume_init_drop() } } -# } +# impl From<std::rc::Rc<String>> for RcChars { fn from(value: std::rc::Rc<String>) -> Self { Self::from_rc(value) } } +# impl Drop for RcChars { fn drop(&mut self) { unsafe { self.chars.assume_init_drop() } } } # } # use std::rc::Rc; # use rcchars::RcChars; @@ -43,6 +39,7 @@ ``` ## Solutions + - [Implementation] used in rattlescript. - [Same solution] but with some extra comments. - [Another solution]. I prefer this one. diff --git a/src/exercises/refbind.md b/src/exercises/refbind.md index 5a38dc1..feb5076 100644 --- a/src/exercises/refbind.md +++ b/src/exercises/refbind.md @@ -1,4 +1,5 @@ Make this compile and pass tests: + ```rust # mod __ { fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> { @@ -27,6 +28,7 @@ assert_eq!( ``` Try solving it in the playground: + ```rust,editable,compile_fail fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> { match fa { @@ -50,4 +52,3 @@ fn main() { ); } ``` - diff --git a/src/overview.md b/src/overview.md index 37a19d9..d231c76 100644 --- a/src/overview.md +++ b/src/overview.md @@ -4,10 +4,11 @@ Random Rust exercises. Mostly typesystem-oriented. **Warning if you're viewing this not on github.io: this is a live-edited website, errors and accidental spoilers are to be expected.** -# Solutions +## Solutions Presently, solutions are often provided in a non-human-readable (compacted+misformatted) form as hidden code. Those are intended for testing that the exercise is even possible to solve. + ```rust # /* fn fix_me() { // Greyed out because the solution changes this line. @@ -19,7 +20,9 @@ fn do_not_change_this() { fixed_name() } ``` + Some exercises may not have the replaced parts greyed out: + ```rust # mod __ { fn fix_me() { diff --git a/src/topics.md b/src/topics.md index 232e27f..de9cbc6 100644 --- a/src/topics.md +++ b/src/topics.md @@ -11,7 +11,6 @@ Rows are ordered based on estimated difficulty. p.s. most of the exercises are actually about `trait`s, this categorisation isn't strict. - [explicit lifetimes]: ./exercises/refbind.md [`Fn`?]: ./exercises/bind.md [`RcChars`]: ./exercises/rcchars.md