fix formatting
This commit is contained in:
parent
41a93479d3
commit
d9bec9da08
@ -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
|
[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):
|
Example of what it may allow (solution isn't required to pass these tests, but should):
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# pub trait Equivalent {
|
# pub trait Equivalent {
|
||||||
# type T;
|
# type T;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
Make this compile and pass tests:
|
Make this compile and pass tests:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# mod __ {
|
# mod __ {
|
||||||
fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> {
|
fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> {
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
# Introducing variance to objects
|
# Introducing variance to objects
|
||||||
|
|
||||||
Make `test_covariance` compile by making `BoolStream<'a>` covariant over `'a`. Restrictions:
|
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.
|
- 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.
|
- Changed version must behave the same way as the original.
|
||||||
|
|
||||||
Consider the following code:
|
Consider the following code:
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
pub struct BoolStream<'a>(
|
pub struct BoolStream<'a>(
|
||||||
Box<dyn 'a + FnOnce() -> (bool, 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
|
e
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Why does it fail to compile?
|
Why does it fail to compile?
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# Monad composition?
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# use std::ops::ControlFlow;
|
# use std::ops::ControlFlow;
|
||||||
trait Monad<'a>: 'a {
|
trait Monad<'a>: 'a {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# `const` `Add`?
|
# `const` `Add`?
|
||||||
|
|
||||||
Define `FIVE_SECONDS` as `const`:
|
Define `FIVE_SECONDS` as `const`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# use std::time::Duration;
|
# use std::time::Duration;
|
||||||
# const fn unwrap_time(d: Option<Duration>) -> Duration { match d { Some(d) => d, None => panic!() } }
|
# 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:
|
Try solving it in the playground:
|
||||||
|
|
||||||
```rust,editable,compile_fail
|
```rust,editable,compile_fail
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
Make this compile and pass tests:
|
Make this compile and pass tests:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T {
|
fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T {
|
||||||
let s = "te".to_string() + "st";
|
let s = "te".to_string() + "st";
|
||||||
@ -16,6 +17,7 @@ assert_eq!(string, "test".to_string());
|
|||||||
```
|
```
|
||||||
|
|
||||||
Try solving it in the playground:
|
Try solving it in the playground:
|
||||||
|
|
||||||
```rust,editable,compile_fail
|
```rust,editable,compile_fail
|
||||||
fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T {
|
fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T {
|
||||||
f("test")
|
f("test")
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
# Deserialisation modes
|
# Deserialisation modes
|
||||||
|
|
||||||
Merge implementations of `ConsumesStream` and `Deterministic` for tuple.
|
Merge implementations of `ConsumesStream` and `Deterministic` for tuple.
|
||||||
|
|
||||||
It's recommended to first solve [this exercise](./multiple_blanket.md).
|
It's recommended to first solve [this exercise](./multiple_blanket.md).
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
# Blanket, blanket, blanket
|
# Blanket, blanket, blanket
|
||||||
|
|
||||||
Make this compile with the following restrictions:
|
Make this compile with the following restrictions:
|
||||||
|
|
||||||
- **Can't edit definitions of `A`, `test`, `tes1`, `test2`; can only add code outside of those items.**
|
- **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 `A1` but not `A2`.**
|
||||||
- **Some types are `A2` but not `A1`.**
|
- **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:
|
You may assume that `A`, `A1`, `A2` can be in separate crates with a dependency graph equivalent to this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# mod __ {
|
# mod __ {
|
||||||
mod a1 {
|
mod a1 {
|
||||||
|
@ -15,16 +15,12 @@
|
|||||||
# impl RcChars {
|
# impl RcChars {
|
||||||
# pub fn from_rc(rc: std::rc::Rc<String>) -> Self {
|
# pub fn from_rc(rc: std::rc::Rc<String>) -> Self {
|
||||||
# let mut new = Self { _rc: rc, chars: std::mem::MaybeUninit::uninit() };
|
# 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
|
# new
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
# impl From<std::rc::Rc<String>> for RcChars {
|
# impl From<std::rc::Rc<String>> for RcChars { fn from(value: std::rc::Rc<String>) -> Self { Self::from_rc(value) } }
|
||||||
# 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 Drop for RcChars {
|
|
||||||
# fn drop(&mut self) { unsafe { self.chars.assume_init_drop() } }
|
|
||||||
# }
|
|
||||||
# }
|
# }
|
||||||
# use std::rc::Rc;
|
# use std::rc::Rc;
|
||||||
# use rcchars::RcChars;
|
# use rcchars::RcChars;
|
||||||
@ -43,6 +39,7 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Solutions
|
## Solutions
|
||||||
|
|
||||||
- [Implementation] used in rattlescript.
|
- [Implementation] used in rattlescript.
|
||||||
- [Same solution] but with some extra comments.
|
- [Same solution] but with some extra comments.
|
||||||
- [Another solution]. I prefer this one.
|
- [Another solution]. I prefer this one.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
Make this compile and pass tests:
|
Make this compile and pass tests:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# mod __ {
|
# mod __ {
|
||||||
fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> {
|
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:
|
Try solving it in the playground:
|
||||||
|
|
||||||
```rust,editable,compile_fail
|
```rust,editable,compile_fail
|
||||||
fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> {
|
fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> {
|
||||||
match fa {
|
match fa {
|
||||||
@ -50,4 +52,3 @@ fn main() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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.**
|
**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.
|
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.
|
Those are intended for testing that the exercise is even possible to solve.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# /*
|
# /*
|
||||||
fn fix_me() { // Greyed out because the solution changes this line.
|
fn fix_me() { // Greyed out because the solution changes this line.
|
||||||
@ -19,7 +20,9 @@ fn do_not_change_this() {
|
|||||||
fixed_name()
|
fixed_name()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Some exercises may not have the replaced parts greyed out:
|
Some exercises may not have the replaced parts greyed out:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# mod __ {
|
# mod __ {
|
||||||
fn fix_me() {
|
fn fix_me() {
|
||||||
|
@ -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.
|
p.s. most of the exercises are actually about `trait`s, this categorisation isn't strict.
|
||||||
|
|
||||||
|
|
||||||
[explicit lifetimes]: ./exercises/refbind.md
|
[explicit lifetimes]: ./exercises/refbind.md
|
||||||
[`Fn`?]: ./exercises/bind.md
|
[`Fn`?]: ./exercises/bind.md
|
||||||
[`RcChars`]: ./exercises/rcchars.md
|
[`RcChars`]: ./exercises/rcchars.md
|
||||||
|
Loading…
Reference in New Issue
Block a user