-
Notifications
You must be signed in to change notification settings - Fork 2
/
do_notation_style.rs
46 lines (38 loc) · 1.04 KB
/
do_notation_style.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use do_notation::m;
/**
Thanks to crates like [do_notation] https://github.com/phaazon/do-notation we can write our code
using [do notation] style like in Haskell or Scala with [for-comprehension]
*/
pub fn run() {
option_program();
result_program();
}
fn option_program() {
let maybe_user_info = m! {
user <- login("politrons");
account <- get_user_account(user);
get_user_info(account)
};
println!("{:?}", maybe_user_info)
}
fn result_program() {
let result_user_info: Result<Account, String> = m! {
user <- Ok(User("Politrons".to_string()));
account <- Ok(Account { info: user.0 });
Ok(account)
};
println!("{:?}", result_user_info.unwrap().info)
}
fn login(username: &str) -> Option<User> {
Some(User(username.to_string()))
}
fn get_user_account(user: User) -> Option<Account> {
Some(Account { info: user.0 })
}
fn get_user_info(account: Account) -> Option<String> {
Some(account.info)
}
struct User(String);
struct Account {
info: String,
}