Skip to content

Commit

Permalink
docs: Improve ERKN example
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Dec 8, 2023
1 parent 4b29e13 commit 1ad2e24
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
23 changes: 12 additions & 11 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import 'package:rust_core/core.dart'; // Or import 'package:rust_core/<LIBRARY_NAME>.dart';

void main() {
void main(){
usingTheEarlyReturnKey();
usingRegularPatternMatching();
}

Result<int, String> usingTheEarlyReturnKey() => Result(($) {
double x = willAlwaysReturnErr()[$];
return Ok(x.toInt());
});
Result<int,String> usingTheEarlyReturnKey() => Result(($){ // Early Return Key
// Will return here with 'Err("error")'
int x = willAlwaysReturnErr()[$].toInt();
return Ok(x);
});

Result<int, String> usingRegularPatternMatching() {
double x;
switch (willAlwaysReturnErr()) {
Result<int,String> usingRegularPatternMatching(){
int x;
switch(willAlwaysReturnErr()){
case Err(:final err):
return Err(err);
case Ok(:final ok):
x = ok;
x = ok.toInt();
}
return Ok(x.toInt());
return Ok(x);
}

Result<double, String> willAlwaysReturnErr() => Err("error");
Result<double,String> willAlwaysReturnErr() => Err("error");
18 changes: 10 additions & 8 deletions lib/src/result/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ they are different.
Note: There also exists
`intoUnchecked` that does not require implicit cast of a `Result` Type.
### Early Return Key Notation
The "Early Return Key" is a take on "Do Notation" and functions the same way as the Early Return Operator. The Early
Return Key is typically denoted with `$` and when
"Early Return Key Notation" is a take on "Do Notation" and the "Early Return Key"
functions the same way as the "Early Return Operator". The
Early Return Key is typically denoted with `$` and when
passed to a Result it unlocks the inner value, or returns to the surrounding context. e.g.
```dart
Result<int, String> innerErrFn() => Err("message");
Expand Down Expand Up @@ -289,20 +290,21 @@ void main(){
usingRegularPatternMatching();
}
Result<int,String> usingTheEarlyReturnKey() => Result(($){
double x = willAlwaysReturnErr()[$];
return Ok(x.toInt());
Result<int,String> usingTheEarlyReturnKey() => Result(($){ // Early Return Key
// Will return here with 'Err("error")'
int x = willAlwaysReturnErr()[$].toInt();
return Ok(x);
});
Result<int,String> usingRegularPatternMatching(){
double x;
int x;
switch(willAlwaysReturnErr()){
case Err(:final err):
return Err(err);
case Ok(:final ok):
x = ok;
x = ok.toInt();
}
return Ok(x.toInt());
return Ok(x);
}
Result<double,String> willAlwaysReturnErr() => Err("error");
Expand Down

0 comments on commit 1ad2e24

Please sign in to comment.