Skip to content

Commit

Permalink
Merge pull request #181 from Wodann/improvement/readme
Browse files Browse the repository at this point in the history
misc: update Mun code sample in README
  • Loading branch information
Wodann authored May 15, 2020
2 parents b1bd35b + d9c26c2 commit 6ab5300
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,64 @@
designed to support source code queries, allowing for powerful IDE integrations such as code
completion and refactoring tools.

## Examples
## Example

<!-- inline HTML is intentionally used to add the id. This allows retrieval of the HTML -->
<pre language="mun">
<code id="code-sample">fn fibonacci(n: i32) -> i32 {
if n <= 1 {
n
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}

```mun
fn main() {
// Comments: Mun natively supports bool, f32, f64, i8, u8, i16, etc.
// Comments: functions marked as `pub` can be called outside the module
pub fn main() {
// Native support for bool, f32, f64, i8, u8, u128, i128, usize, isize, etc
let is_true = true;
let var = 0.5;

let sum = add(30, 40);
// Type annotations are not required when a variable's type can be deduced
let n = 3;

let result = fibonacci(n);

// Adding a suffix to a literal restricts its type
let lit = 15u128;

let foo = record();
let bar = tuple();
let baz = on_heap();
}

// Both record structs and tuple structs are supported
struct Record {
n: i32,
}

// Struct definitions include whether they are allocated by a garbage collector
// (`gc`) and passed by reference, or passed by `value`. By default, a struct
// is garbage collected.
struct(value) Tuple(f32, f32);

struct(gc) GC(i32);

// The order of function definitions doesn't matter
fn add(a: i64, b: i64) -> i64 {
a + b
fn record() -> Record {
// Mun allows implicit returns
Record { n: 7 }
}
```

fn tuple() -> Tuple {
// Mun allows explicit returns
return Tuple(3.14, -6.28);
}

fn on_heap() -> GC {
GC(0)
}</code>
</pre>

## Documentation

Expand Down

0 comments on commit 6ab5300

Please sign in to comment.