diff --git a/src/SUMMARY.md b/src/SUMMARY.md index adba62b..4d621e0 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/exercises/fromref.md b/src/exercises/fromref.md new file mode 100644 index 0000000..d029092 --- /dev/null +++ b/src/exercises/fromref.md @@ -0,0 +1,29 @@ +Make this compile and pass tests: +```rust +fn with_slice(f: impl FnOnce(&str) -> T) -> T { + f("test") +} + +# pub trait FromRef: for<'a> From<&'a T> { fn from_ref(value: &T) -> Self { Self::from(value) } } +# impl From<&'a T>> FromRef 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(f: impl FnOnce(&str) -> T) -> T { + f("test") +} + +fn main() { + let string = with_slice( + String::from // Change this + ); + assert_eq!(string, "test".to_string()); +} +``` diff --git a/src/topics.md b/src/topics.md index e8f0cc3..232e27f 100644 --- a/src/topics.md +++ b/src/topics.md @@ -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