diff --git a/book.toml b/book.toml index 8c6b8f8..b3e1e7d 100644 --- a/book.toml +++ b/book.toml @@ -15,3 +15,6 @@ create-missing = false [output.html] default-theme = "navy" mathjax-support = true + +[output.html.playground] +editable = true diff --git a/src/exercises/bind.md b/src/exercises/bind.md index 8c94344..e7a5342 100644 --- a/src/exercises/bind.md +++ b/src/exercises/bind.md @@ -1,6 +1,7 @@ +Make this compile and pass tests. ```rust # mod __ { -fn bind Option>(f: F, fa: Option) -> Option { +fn bind Option>(f: F, fa: Option) -> Option { 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 Option>(f: F, fa: Option) -> Option { + 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()), + ); +} +``` diff --git a/src/exercises/refbind.md b/src/exercises/refbind.md index 078237f..4f55b6a 100644 --- a/src/exercises/refbind.md +++ b/src/exercises/refbind.md @@ -1,3 +1,4 @@ +Make this compile and pass tests. ```rust # mod __ { fn refbind 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 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) + ); +} +``` +