mirror of https://github.com/sunface/rust-course
parent
ea642e7839
commit
04e89c5f32
@ -0,0 +1 @@
|
|||||||
|
# 对抗编译检查
|
@ -0,0 +1 @@
|
|||||||
|
# 大师之路
|
@ -0,0 +1,34 @@
|
|||||||
|
# 代码风格
|
||||||
|
|
||||||
|
## 使用[tap](https://github.com/myrrlyn/tap)库来实现`point-free`编程风格
|
||||||
|
```rust
|
||||||
|
use tap::{Tap, TapFallible};
|
||||||
|
|
||||||
|
type SomeValue = String;
|
||||||
|
type SomeOtherValue = String;
|
||||||
|
type SomeError = String;
|
||||||
|
|
||||||
|
fn foo() -> Result<SomeValue, SomeError> {
|
||||||
|
Ok("foo".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(input: &str) -> Result<SomeOtherValue, SomeError> {
|
||||||
|
if input == "bar" {
|
||||||
|
Ok("Success".into())
|
||||||
|
} else {
|
||||||
|
Err("This is a failure message".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn my_fun() -> Result<SomeOtherValue, SomeError> {
|
||||||
|
foo()
|
||||||
|
.tap_err(|err| println!("foo() failed with error: {}", err))
|
||||||
|
.and_then(|foo_val| bar(&foo_val))
|
||||||
|
.tap(|res| println!("bar() returned result: {:?}", res))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let result = my_fun();
|
||||||
|
println!("{:?}", result);
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in new issue