This commit is contained in:
AF 2023-08-11 13:24:55 +00:00
parent 0c8babfcf3
commit 61971da7d2
3 changed files with 34 additions and 3 deletions

View File

@ -6,6 +6,7 @@
- [test0.rs (bind)](./exercises/bind.md) - [test0.rs (bind)](./exercises/bind.md)
- [test1.rs (refbind)](./exercises/refbind.md) - [test1.rs (refbind)](./exercises/refbind.md)
- [Duration](./exercises/duration.md) - [Duration](./exercises/duration.md)
- [FromRef](./exercises/fromref.md)
- [A/A1/A2](./exercises/multiple_blanket.md) - [A/A1/A2](./exercises/multiple_blanket.md)
- [BoolStream](./exercises/bool_stream.md) - [BoolStream](./exercises/bool_stream.md)
- [RcChars](./exercises/rcchars.md) - [RcChars](./exercises/rcchars.md)

29
src/exercises/fromref.md Normal file
View File

@ -0,0 +1,29 @@
Make this compile and pass tests:
```rust
fn with_slice<T>(f: impl FnOnce(&str) -> T) -> T {
f("test")
}
# pub trait FromRef<T: ?Sized>: for<'a> From<&'a T> { fn from_ref(value: &T) -> Self { Self::from(value) } }
# impl<T: ?Sized, U: for<'a> From<&'a T>> FromRef<T> for U {}
let string = with_slice(
# /*
String::from
# */ String::from_ref
);
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")
}
fn main() {
let string = with_slice(
String::from // Change this
);
assert_eq!(string, "test".to_string());
}
```

View File

@ -5,9 +5,9 @@ Rows are ordered based on estimated difficulty.
| lifetimes\[-adjacent\] | `trait`s | `struct`s | | lifetimes\[-adjacent\] | `trait`s | `struct`s |
|------------------------|--------------------|---------------| |------------------------|--------------------|---------------|
| [explicit lifetimes] | [`Fn`?] | [`Duration`] | | [explicit lifetimes] | [`Fn`?] | [`Duration`] |
| [extracting lifetimes] | [exclusive traits] | [All Errors?] | | [from reference] | [exclusive traits] | [All Errors?] |
| [composition] | [merging traits] | [`RcChars`] | | [extracting lifetimes] | [merging traits] | [`RcChars`] |
| | | [`AnyStr`] | | [composition] | | [`AnyStr`] |
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.
@ -22,3 +22,4 @@ p.s. most of the exercises are actually about `trait`s, this categorisation isn'
[composition]: ./exercises/composition.md [composition]: ./exercises/composition.md
[`Duration`]: ./exercises/duration.md [`Duration`]: ./exercises/duration.md
[All Errors?]: ./exercises/all_errors.md [All Errors?]: ./exercises/all_errors.md
[from reference]: ./exercises/fromref.md