async to_string
All checks were successful
buildbot/mdbook test Build done.

This commit is contained in:
AF 2023-09-28 17:53:55 +00:00
parent 433cdaae46
commit 2e0a59a70b
5 changed files with 40 additions and 16 deletions

9
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"rust-analyzer.showUnlinkedFileNotification": false,
"[rust]": {
"editor.rulers": [100]
},
"[markdown]": {
"editor.rulers": [86, 99, 100]
}
}

View File

@ -2,7 +2,7 @@
name = "exercises" name = "exercises"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
futures = "0.3"

View File

@ -10,6 +10,7 @@
- [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)
- [Async Fn](./exercises/async_fn.md)
- [Chapter 2](./chapter_2.md) - [Chapter 2](./chapter_2.md)
- [AnyStr](./exercises/anystr.md) - [AnyStr](./exercises/anystr.md)
- [Mode](./exercises/mode.md) - [Mode](./exercises/mode.md)

28
src/exercises/async_fn.md Normal file
View File

@ -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<Output = String>; fn to_string(&'a self, s: &'a str) -> Self::F; }
# impl<'a, F: 'a + Future<Output = String>, 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<T: for<'a> 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())
```

View File

@ -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);
}
}