Skip to content

Commit

Permalink
doc: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Jul 1, 2024
1 parent cd40372 commit 9f53005
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 175 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,66 @@

[Rust Core Book 📖](https://mcmah309.github.io/rust_core)

## Example
> Goal: Get the index of every "!" in a string not followed by a "?"
**Rust:**
```rust
use std::iter::Peekable;

fn main() {
let mut answer: Vec<usize> = Vec::new();
let string = "kl!sd!?!";
let mut iter: Peekable<_> = string
.chars()
.map_windows(|w: &[char; 2]| *w)
.enumerate()
.peekable();

while let Some((index, window)) = iter.next() {
match window {
['!', '?'] => continue,
['!', _] => answer.push(index),
[_, '!'] if iter.peek().is_none() => answer.push(index + 1),
_ => continue,
}
}
assert_eq!(answer, [2, 7]);
}
```
**Dart:**
```dart
import 'package:rust_core/rust_core.dart';
void main() {
List<int> answer = [];
String string = "kl!sd!?!";
Peekable<(int index, Arr<String> window)> iter = string
.chars()
.mapWindows(2, identity)
.enumerate()
.peekable();
while (iter.moveNext()) {
final (int index, Arr<String> window) = iter.current;
switch (window) {
case ["!", "?"]:
break;
case ["!", _]:
answer.add(index);
case [_, "!"] when iter.peek().isNone():
answer.add(index + 1);
}
}
expect(answer, [2, 7]);
}
```

# Project Goals

rust_core's primary goal is to bring Rust's features and ergonomics to Dart.

To accomplish this, Rust's functionalities are carefully adapted to Dart's paradigms, focusing on a smooth idiomatic language-compatible integration. As a result, a seamless developer experience is created for any developer using both languages, while adding powerful tools to the toolbox of any Dart developer.
To accomplish this, Rust's functionalities are carefully adapted to Dart's paradigms, focusing on a smooth idiomatic language-compatible integration.
The result is developers now have access powerful tools previously only available to Rust developers and developer using
both languages has a seamless experience.

True to the Rust philosophy, rust_core strives to bring reliability and performance in every feature. Every feature is robustly tested. Over 500 meaningful test suites and counting.
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Introduction

- [Introduction](./introduction/introduction.md)
- [Demo](./introduction/demo.md)
- [Quickstart](./introduction/quickstart.md)
- [New To Rust](./introduction/new_to_rust.md)
- [New To Dart](./introduction/new_to_dart.md)

Expand Down
35 changes: 0 additions & 35 deletions book/src/introduction/demo.md

This file was deleted.

52 changes: 1 addition & 51 deletions book/src/introduction/new_to_dart.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,7 @@ Welcome to Dart!

Dart is a great language choice for fast cross platform development and scripting.

rust_core is great start to learn the Dart semantics as you will feel like you are writing native rust. Just look at this Rust code:
> Goal: Get the index of every "!" in a string not followed by a "?"
```rust
use std::iter::Peekable;

fn main() {
let mut answer: Vec<usize> = Vec::new();
let string = "kl!sd!?!";
let mut iter: Peekable<_> = string
.chars()
.map_windows(|w: &[char; 2]| *w)
.enumerate()
.peekable();

while let Some((index, window)) = iter.next() {
match window {
['!', '?'] => continue,
['!', _] => answer.push(index),
[_, '!'] if iter.peek().is_none() => answer.push(index + 1),
_ => continue,
}
}
assert_eq!(answer, [2, 7]);
}
```
Dart equivalent with rust_core:
```dart
import 'package:rust_core/rust_core.dart';
void main() {
List<int> answer = [];
String string = "kl!sd!?!";
Peekable<(int index, Arr<String> window)> iter = string
.chars()
.mapWindows(2, identity)
.enumerate()
.peekable();
while (iter.moveNext()) {
final (int index, Arr<String> window) = iter.current;
switch (window) {
case ["!", "?"]:
break;
case ["!", _]:
answer.add(iter.current.$1);
case [_, "!"] when iter.peek().isNone():
answer.add(index + 1);
}
}
expect(answer, [2, 7]);
}
```
rust_core is great start to learn the Dart semantics as you will feel like you are writing native rust.

rust_core will introduce you to a few new types you may find useful as a Dart developer:

Expand Down
52 changes: 1 addition & 51 deletions book/src/introduction/new_to_rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,7 @@ Welcome to Rust!
Maybe you have heard of Rust and want to see what all the hype is about, maybe you know a little Rust
and want to improve your Rust while writing Dart, for whatever the reason, rust_core is here to help.

Rust has a solid reputation for writing safe, maintainable, and performant code. rust_core is great start to learn and improve your rust semantics/knowledge. You will write Dart and learn Rust along the way. Just look at this Dart code:
> Goal: Get the index of every "!" in a string not followed by a "?"
```dart
import 'package:rust_core/rust_core.dart';
void main() {
List<int> answer = [];
String string = "kl!sd!?!";
Peekable<(int index, Arr<String> window)> iter = string
.chars()
.mapWindows(2, identity)
.enumerate()
.peekable();
while (iter.moveNext()) {
final (int index, Arr<String> window) = iter.current;
switch (window) {
case ["!", "?"]:
break;
case ["!", _]:
answer.add(iter.current.$1);
case [_, "!"] when iter.peek().isNone():
answer.add(index + 1);
}
}
expect(answer, [2, 7]);
}
```
Rust equivalent:
```rust
use std::iter::Peekable;

fn main() {
let mut answer: Vec<usize> = Vec::new();
let string = "kl!sd!?!";
let mut iter: Peekable<_> = string
.chars()
.map_windows(|w: &[char; 2]| *w)
.enumerate()
.peekable();

while let Some((index, window)) = iter.next() {
match window {
['!', '?'] => continue,
['!', _] => answer.push(index),
[_, '!'] if iter.peek().is_none() => answer.push(index + 1),
_ => continue,
}
}
assert_eq!(answer, [2, 7]);
}
```
Rust has a solid reputation for writing safe, maintainable, and performant code. rust_core is great start to learn and improve your rust semantics/knowledge. You will write Dart and learn Rust along the way.

With rust_core you can expect all the usual types you see in Rust. Here is a quick matrix
comparing Rust, Dart, and Dart with rust_core:
Expand Down
113 changes: 113 additions & 0 deletions book/src/introduction/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Quickstart

## Setup
### Install

rust_core can be install like any other Dart package.

Dart:
```shell
dart pub add rust_core
```
Flutter:
```shell
flutter pub add rust_core
```

or add directly to your `pubspec.yaml`:
```yaml
dependencies:
rust_core: <version>
```
### Imports
rust_core follows the same library structure and naming as [Rust's rust_core](https://doc.rust-lang.org/core/).
To that extent, each library can be imported individually
```dart
import 'package:rust_core/result.dart';
```
or in it's entirety
```dart
import 'package:rust_core/rust_core.dart';
```
## General Notes

All of rust_core's classes and methods are well documented [docs](https://pub.dev/documentation/rust_core/latest/), but
being an implementation of Rust's core library, you can also refer the the [Rust core](https://doc.rust-lang.org/beta/core/index.html) if anything is unclear.
The functionally is the same.

## The Basics

### Result and Option

`Result<T, E>` is the type used for returning and propagating errors.

`Option<T>` represents a value that can be either some value of type `T` (`Some<T>`) or `None`.
It is used "in place" of `null` (implemented as a zero cost extension type of `T?`).

These types can be easily chained with other operations.

### The Rust `?` Operator and Early Return Key Notion

`Result<T,E>` and `Option<T>` both support early return key notation - which is a same as the rust `?` operator - It returns from
the scope if an [Err] or `None` is encountered, otherwise it retrieves the inner value.

Result example:
```dart
Result<double, String> divedBy(int num, int divisor) => divisor == 0 ? Err("Divide by zero error") : Ok(num / divisor);
Result<double, String> func(int x) => Result(($) { // Early Return Key
// The function will return here
int val = divedBy(x, 0)[$] + 10;
return Ok(val);
});
void main(){
assert(func(5) == Err("Divide by zero error"));
}
```

### `List` and `Arr`

`Arr` is a compliment to `List`, which represents a fixed sized `List`. No more runtime exceptions for trying to grow
a non-growable `List`. It also has zero runtime cost, as it is an extension type of `List`. `Arr` is more efficient than a growable `List`.

#### Iter
rust_core implements the entirety of Rust's stable and unstable [Iterator](https://doc.rust-lang.org/beta/core/iter/trait.Iterator.html) methods.
There are a lot of methods here that many Dart developers may not be familiar with. Definitely worth a look - [pub](https://pub.dev/documentation/rust_core/latest/iter/iter-library.html)

```dart
List<int> list = [1, 2, 3, 4, 5];
RIterator<int> filtered = list.iter().filterMap((e) {
if (e % 2 == 0) {
return Some(e * 2);
}
return None;
});
expect(filtered, [4, 8]);
```

`RIterator` functions the same as a Rust `Iterator`. For Dart developers, you can think of it as the union of Dart's `Iterator` and `Iterable`.
check [here](../libs/iter/iter.md) for more info.

#### Slice

A `Slice` is a contiguous sequence of elements in a `List` or `Arr`. Slices are a view into a list without allocating and copying to a new list,
thus slices are more efficient than creating a sub-list with `list.sublist(x,y)`.
```dart
var list = [1, 2, 3, 4, 5];
var slice = Slice(list, 1, 4);
slice = list.slice(1,4); // alternative
expect(slice, [2, 3, 4]);
var taken = slice.takeLast();
expect(taken, 4);
expect(slice, [2, 3]);
slice[1] = 10;
expect(list, [1, 2, 10, 4, 5]);
```
`Slice` also has <u>a lot</u> of efficient methods for in-place mutation within and between slices.

## Whats Next?

Checkout any of the other sections in the book for more details and enjoy!
Loading

0 comments on commit 9f53005

Please sign in to comment.