Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hello, Rock!

Create your first project with rock new:

rock new hello
cd hello

This creates a new directory containing rock.toml, src/main.rk, and a .gitignore that excludes build output. Existing directories are never overwritten. Project names start with an ASCII letter and can contain letters, digits, hyphens, and underscores; stdlib is reserved.

The generated rock.toml manifest is:

[crate]
name = "hello"
version = "0.1.0"

[lib]
path = "src/main.rk"

The generated src/main.rk contains this complete program:

main = !->
    "Hello, Rock!".println!

Run it from the project directory:

$ rock run
Hello, Rock!

The output contains one line, and the process exits with status zero.

Reading the program

main = !-> declares a function named main with no parameters. The arrow separates its parameter list from its body. The extra indentation makes the next line part of that body.

The string literal is borrowed string data. .println! calls the prelude’s printing method with no explicit arguments. The !-> body evaluates that call, discards its result, and returns unit (()). The runtime maps a unit-returning main to process exit status 0, so no final 0 expression is needed.

Use main = !-> for ordinary programs. Keep main = -> when you intentionally return an integer exit status, such as 1 to report failure; that arrow returns the body’s trailing value instead of discarding it.

Calls with arguments use spaces and commas:

main = !->
    max 10, 20 .println!

max is a prelude function. The first argument is 10 and the second is 20. The space before .println! makes the method apply to the result of the complete max 10, 20 call, so this prints 20 directly.

Expressions and statements

A one-expression function can stay on one line or use an indented body:

square = number ->
    number * number

main = !->
    square 5 .println!

The final expression of square is its return value. Earlier expressions can perform effects before the final value:

announce_square = number ->
    "squaring a number".println!
    number * number

main = !->
    announce_square 5 .println!

An if/else expression produces the value of the selected branch. When it is the final expression in a function, that value becomes the function’s return value:

absolute = number ->
    if number >= 0 
    then number
    else -number

main = !->
    absolute -5 .println!

For -5, the condition is false, so the else branch evaluates -number and produces 5. For a nonnegative value, the then branch produces number unchanged. The selected value is returned by absolute.

Comments

Rock supports line and block comments:

main = !->
    // This line explains why the next value is printed.
    /* A block comment can
       cover several source lines. */
    "comments do not produce values".println!

Comments are ignored by the compiler. Use them for intent, constraints, or a non-obvious reason, not as a translation of every line.

Common mistakes

  • Using !-> when you intend to return a custom integer exit status; use -> instead.
  • Writing println("text"); the current call form is "text".println!.
  • Indenting a statement at the same level as main = !->; that makes it a separate top-level item instead of part of main.
  • Running rock outside the directory containing rock.toml.