fromref
This commit is contained in:
parent
0c8babfcf3
commit
61971da7d2
@ -6,6 +6,7 @@
|
||||
- [test0.rs (bind)](./exercises/bind.md)
|
||||
- [test1.rs (refbind)](./exercises/refbind.md)
|
||||
- [Duration](./exercises/duration.md)
|
||||
- [FromRef](./exercises/fromref.md)
|
||||
- [A/A1/A2](./exercises/multiple_blanket.md)
|
||||
- [BoolStream](./exercises/bool_stream.md)
|
||||
- [RcChars](./exercises/rcchars.md)
|
||||
|
29
src/exercises/fromref.md
Normal file
29
src/exercises/fromref.md
Normal 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());
|
||||
}
|
||||
```
|
@ -5,9 +5,9 @@ Rows are ordered based on estimated difficulty.
|
||||
| lifetimes\[-adjacent\] | `trait`s | `struct`s |
|
||||
|------------------------|--------------------|---------------|
|
||||
| [explicit lifetimes] | [`Fn`?] | [`Duration`] |
|
||||
| [extracting lifetimes] | [exclusive traits] | [All Errors?] |
|
||||
| [composition] | [merging traits] | [`RcChars`] |
|
||||
| | | [`AnyStr`] |
|
||||
| [from reference] | [exclusive traits] | [All Errors?] |
|
||||
| [extracting lifetimes] | [merging traits] | [`RcChars`] |
|
||||
| [composition] | | [`AnyStr`] |
|
||||
|
||||
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
|
||||
[`Duration`]: ./exercises/duration.md
|
||||
[All Errors?]: ./exercises/all_errors.md
|
||||
[from reference]: ./exercises/fromref.md
|
||||
|
Loading…
Reference in New Issue
Block a user