fix formatting

This commit is contained in:
AF 2023-08-26 15:26:33 +00:00
parent 41a93479d3
commit d9bec9da08
12 changed files with 25 additions and 10 deletions

View File

@ -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;

View File

@ -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> {

View File

@ -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

View File

@ -1,3 +1,5 @@
# Monad composition?
```rust
# use std::ops::ControlFlow;
trait Monad<'a>: 'a {

View File

@ -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;

View File

@ -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")

View File

@ -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).

View File

@ -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 {

View File

@ -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.

View File

@ -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() {
);
}
```

View File

@ -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() {

View File

@ -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