rust_core 1.3.2 copy "rust_core: ^1.3.2" to clipboard
rust_core: ^1.3.2 copied to clipboard

Pure Dart implementation of patterns in Rust's core library. Types include Result, Option, Cell, Slice, Array, Iterator, etc. Facilitates functional programming and error handling.

example/main.dart

// ignore_for_file: unused_local_variable

import 'package:rust_core/rust_core.dart';

void main() {
  usingTheEarlyReturnKeyExample();
  usingRegularPatternMatchingExample();
  usingFunctionChainingExample();
  iteratorExample();
  sliceExample();

  /// Visit the book to see more!
}

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

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

Result<int, String> usingFunctionChainingExample() =>
    willAlwaysReturnErr().map((e) => e.toInt());

void iteratorExample() {
  List<int> list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  Iter<int> iter = list.iter();
  List<int> collect = [];
  for (final e in iter.take(5).map((e) => e * e)) {
    if (e.isEven) {
      collect.add(e);
    }
  }
  Option<int> next = iter.next();
  collect.add(next.unwrap());
  next = iter.next();
  collect.add(next.unwrap());
  while (iter.moveNext()) {
    collect.add(iter.current * iter.current);
  }
}

void sliceExample() {
  var list = [1, 2, 3, 4, 5];
  var slice = Slice(list, 1, 4);
  var taken = slice.takeLast();
  slice[1] = 10;
  assert(list[2] == 10);
}

Result<double, String> willAlwaysReturnErr() => Err("error");
25
likes
0
pub points
70%
popularity

Publisher

verified publishervoyver.com

Pure Dart implementation of patterns in Rust's core library. Types include Result, Option, Cell, Slice, Array, Iterator, etc. Facilitates functional programming and error handling.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on rust_core