MINK
Learn

Getting started with MINK.

From install to your first program, your first error, and the one rule that surprises everyone. Every command on this page was run, on MINK 1.0.5 unless a note says a newer version.

1. Install

Download the setup file and run it. It installs for your account with no administrator rights and offers to set up the .mink file type and your PATH. Or use npm:

npm install -g @p4inz-code/mink

Open a new terminal and check it worked:

mink --version

It should print mink 1.0.5.

2. Your first program

Make a file called hello.mink:

fn square(n: Int) -> Int {
    return n * n;
}

fn main() {
    let mut total = 0;
    for i in 1..6 {
        total = total + square(i);
    }
    rt_print_int(total);
}

Run it, or build it into a program of its own:

mink run hello.mink
mink build hello.mink
.\hello.exe

Both print 55. hello.exe needs nothing else installed, so you can give it to anyone on Windows 10 or newer.

3. Errors that explain themselves

Change let mut total to let total and check the file:

mink check hello.mink
mink: error[E-S03]: cannot assign to `total`: it is not mutable
  --> hello.mink:8:9

Every error has a code, a place in your file and an explanation: mink explain E-S03 tells you what it means and what to try. Put mut back to fix it.

4. The rule that surprises everyone: free what you allocate

MINK does not collect garbage. Strings and buffers that you create, or that a library call hands you, are yours to free. If any are still alive when the program ends, MINK stops with a memory-leak error and exit code 106. This program leaks one string:

fn main() {
    let s = rt_str_concat("hello", " world");
    rt_print_str(s);
}
hello world
mink: runtime error[E-R06]: memory leak: live allocations remain at exit

The fix is one line, after the last use of the string:

fn main() {
    let s = rt_str_concat("hello", " world");
    rt_print_str(s);
    rt_str_free(s);
}

Two more things to know. Most standard library functions consume the string you pass them, so using it again is error E-S10; copy it first with rt_str_concat(s, "") if you need it later. And a temporary that you pass straight into a call has nothing to free it, so give it its own variable. From MINK 1.0.6, mink explain E-R06 lists the usual causes.

5. Using the standard library

Load a module with mod. This reads a value out of JSON:

// Configuration: read a JSON document and print what it says.
mod json;

fn main() -> Int {
    let arena = rt_alloc(65536);
    let v = json_parse("{\"name\":\"mink\",\"workers\":4,\"debug\":true}", arena);
    if v == 0 {
        rt_print_str("bad json");
        return 1;
    }
    let name = json_as_str(arena, json_obj_get_val(arena, v, 0));
    rt_print_str(name);
    rt_str_free(name);
    rt_print_int(json_as_int(arena, json_obj_get_val(arena, v, 1)));
    let out = json_to_str(arena, v);
    rt_print_str(out);
    rt_str_free(out);
    rt_free(arena);
    return 0;
}

The modules are in the standard library reference: files, JSON, CSV, SQLite, HTTP, threads, regular expressions, hashing, compression and more.

6. One format, and an editor

mink fmt hello.mink puts the file in the one layout MINK has. It has no settings, and it checks that it never changes what your program does before it writes anything. Note that it needs a path: mink fmt . formats a whole folder.

For VS Code, the extension in the repository starts the language server for errors as you type, completion, hover and rename. See the VS Code guide.

7. Where next

  • Examples: twelve small programs, from a web server to a terminal game, to read and copy.
  • The language guide: values, functions, ownership and modules in full.
  • Error codes: what each one means.
  • FAQ: why MINK, and when to pick something else.