diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..503f68a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "rust-analyzer.showUnlinkedFileNotification": false, + "[rust]": { + "editor.rulers": [100] + }, + "[markdown]": { + "editor.rulers": [86, 99, 100] + } +} diff --git a/Cargo.toml b/Cargo.toml index d51cd24..61751ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "exercises" version = "0.1.0" edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +publish = false [dependencies] +futures = "0.3" diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 4d621e0..e0c1e46 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -10,6 +10,7 @@ - [A/A1/A2](./exercises/multiple_blanket.md) - [BoolStream](./exercises/bool_stream.md) - [RcChars](./exercises/rcchars.md) + - [Async Fn](./exercises/async_fn.md) - [Chapter 2](./chapter_2.md) - [AnyStr](./exercises/anystr.md) - [Mode](./exercises/mode.md) diff --git a/src/exercises/async_fn.md b/src/exercises/async_fn.md new file mode 100644 index 0000000..285de55 --- /dev/null +++ b/src/exercises/async_fn.md @@ -0,0 +1,28 @@ +# Async `to_string` + +Make this compile and pass tests: + +```rust,ignore,mdbook-runnable +# extern crate futures; +# use futures::{Future, executor::block_on}; +# trait AsyncToStringRef<'a> { type F: 'a + Future; fn to_string(&'a self, s: &'a str) -> Self::F; } +# impl<'a, F: 'a + Future, T: Fn(&'a str) -> F> AsyncToStringRef<'a> for T { +# type F = F; +# fn to_string(&'a self, s: &'a str) -> Self::F { self(s) } +# } +# trait AsyncToString: for<'a> AsyncToStringRef<'a> {} +# impl AsyncToStringRef<'a>> AsyncToString for T {} +async fn test_generic(s: &str, f: impl AsyncToString) -> String { + f.to_string(s).await +} + +async fn to_string_async(s: &str) -> String { + s.to_string() +} + +async fn test_concrete(s: &str) -> String { + test_generic(s, to_string_async).await +} + +assert_eq!(block_on(test_concrete("abc")), "abc".to_string()) +``` diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..e69de29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +0,0 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -}