editable playground
This commit is contained in:
parent
51622451bd
commit
833647455b
@ -15,3 +15,6 @@ create-missing = false
|
||||
[output.html]
|
||||
default-theme = "navy"
|
||||
mathjax-support = true
|
||||
|
||||
[output.html.playground]
|
||||
editable = true
|
||||
|
@ -1,6 +1,7 @@
|
||||
Make this compile and pass tests.
|
||||
```rust
|
||||
# mod __ {
|
||||
fn bind<T, F: FnOnce(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> {
|
||||
fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> {
|
||||
match fa {
|
||||
Some(a) => f(a),
|
||||
None => None,
|
||||
@ -25,3 +26,29 @@ assert_eq!(
|
||||
Some("banana".to_string()),
|
||||
);
|
||||
```
|
||||
|
||||
Try solving it in the playground:
|
||||
```rust,editable,compile_fail
|
||||
fn bind<T, F: Fn(T) -> Option<T>>(f: F, fa: Option<T>) -> Option<T> {
|
||||
match fa {
|
||||
Some(a) => f(a),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(bind(|x: i32| Some(x + 3), Some(2)), Some(5));
|
||||
assert_eq!(bind(|x: i32| Some(x + 3), None), None);
|
||||
assert_eq!(bind(|_: i32| None, Some(2)), None);
|
||||
assert_eq!(bind(|_: i32| None, None), None);
|
||||
|
||||
assert_eq!(bind(|x: &str| Some(x), Some("apple")), Some("apple"));
|
||||
assert_eq!(bind(|_: &str| Some("banana"), Some("apple")), Some("banana"));
|
||||
|
||||
let banana = "banana".to_string();
|
||||
assert_eq!(
|
||||
bind(|_: String| Some(banana), Some("apple".to_string())),
|
||||
Some("banana".to_string()),
|
||||
);
|
||||
}
|
||||
```
|
||||
|
@ -1,3 +1,4 @@
|
||||
Make this compile and pass tests.
|
||||
```rust
|
||||
# mod __ {
|
||||
fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> {
|
||||
@ -24,3 +25,29 @@ assert_eq!(
|
||||
Some(banana)
|
||||
);
|
||||
```
|
||||
|
||||
Try solving it in the playground:
|
||||
```rust,editable,compile_fail
|
||||
fn refbind<T, F: Fn(&T) -> Option<&T>>(f: F, fa: Option<&T>) -> Option<&T> {
|
||||
match fa {
|
||||
Some(a) => f(a),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let apple = "apple".to_string();
|
||||
let banana = "banana".to_string();
|
||||
assert_eq!(
|
||||
refbind(|_: &String| Some(&banana), Some(&apple)),
|
||||
Some(&banana)
|
||||
);
|
||||
|
||||
let banana = "banana";
|
||||
assert_eq!(
|
||||
refbind(|_: &str| Some(banana), Some("apple")),
|
||||
Some(banana)
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user