Hello world
This page walks through your first Beans program from an empty file to a native binary.
Write the program
Section titled “Write the program”Save this as hello.b:
import std.io
fn main() { let name: string = "beans" io.println("hello from {name}")}A few things to notice:
import std.iopulls in the standard I/O package.io.printlnlives there.fn main()is where the program starts.let name: string = "beans"declares a value.letmeans it does not change. The type,string, is written out. Beans does not infer it for you."hello from {name}"is an interpolated string.{name}is replaced with the value ofname.
Check it
Section titled “Check it”Type-check the program without running it:
beansc check hello.bhello.b: okcheck catches type errors and other mistakes fast. It does not run anything.
Run it
Section titled “Run it”Run the program on the reference interpreter, with no build step:
beansc run hello.bhello from beansrun is the quickest way to see output while you work.
Build a native binary
Section titled “Build a native binary”Compile to a real executable through LLVM:
beansc build hello.b -o hello./hellohello from beansThe interpreter (run) and the native binary (build) produce the same
output. The two backends behave identically.
Build an optimized binary
Section titled “Build an optimized binary”For a release build, turn on optimizations, link-time optimization, and tuning for your own CPU:
beansc build --release --lto --cpu native hello.b -o helloAn example that does not compile
Section titled “An example that does not compile”Beans requires a function with a return type to return on every path. This program is intentionally wrong. The docs example checker confirms it fails to compile:
fn total() -> int { var sum: int = 0 sum // a trailing expression is discarded, not returned}error: 'total' must return int — the body can finish without a returnThe fix is to write return sum. See Functions and closures.
To grow past a single file, create a project. For more on the commands used here, see Checking and running and Building.