rust_like_result 1.0.2 copy "rust_like_result: ^1.0.2" to clipboard
rust_like_result: ^1.0.2 copied to clipboard

outdated

The Result<T> type implemented in Ok<T> and Err<T> types with unwrap methods.

example/example.dart

import 'package:rust_like_result/rust_like_result.dart';

void main() {
  var myGoodMap = getMap('Alice');
  var myBadMap = getMap('BlackQueen');

  if (myGoodMap.isOk) {
    String yes = myGoodMap.unwrap['Forward'];
    print('myGoodMap has the Forward key value: $yes');
  }

  if (myBadMap.isErr) {
    print('myBadMap has error. ${myBadMap.err}');
    print('Value of myBadMap is null, yes? : ${myBadMap.val}');
  }

  String defValFromBadMap =
      myBadMap.unwrapOrDefault({'Forward': 'Why?'})['Forward'];
  print('defValFromBadMap: $defValFromBadMap');

  String lazyDefValFromBadMapVia =
      myBadMap.unwrapOrElse((e) => {'Forward': 'Hm...'})['Forward'];
  print('lazyDefValFromBadMapVia: $lazyDefValFromBadMapVia');
}

Result<Map<String, String>> getMap(String name) {
  Result<Map<String, String>> result;
  if (name == 'Alice') {
    result = Ok({'Forward': 'yes', 'Back': 'no'});
  } else {
    result = Err(ArgumentError('Bad name'));
  }
  return result;
}
6
likes
0
pub points
14%
popularity

Publisher

unverified uploader

The Result<T> type implemented in Ok<T> and Err<T> types with unwrap methods.

Homepage

License

unknown (LICENSE)

More

Packages that depend on rust_like_result