This commit is contained in:
parent
433cdaae46
commit
2e0a59a70b
9
.vscode/settings.json
vendored
Normal file
9
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"rust-analyzer.showUnlinkedFileNotification": false,
|
||||
"[rust]": {
|
||||
"editor.rulers": [100]
|
||||
},
|
||||
"[markdown]": {
|
||||
"editor.rulers": [86, 99, 100]
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
@ -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)
|
||||
|
28
src/exercises/async_fn.md
Normal file
28
src/exercises/async_fn.md
Normal 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())
|
||||
```
|
14
src/lib.rs
14
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user