Skip to content

Examples and recipes

The Beans repository ships a folder of example programs under examples/. Each one is a real, runnable .b file that shows one part of the language. This page tells you how to run them and points you at the ones worth reading.

Point beansc run at a file:

Terminal window
beansc run examples/hello.b

Most examples work through both beansc run (the interpreter) and beansc build (a native binary), and give the same output either way.

The multi-package example is a project, so run it from its main.b:

Terminal window
beansc run examples/shop/main.b

Some examples are target-gated: they need a special target or CPU and will not run on a plain desktop build. Those are marked below.

ExampleShowsRunnable
hello.bHello world and string interpolationyes
tour.bA one-file tour of every language ideayes

Walked through in Hello and the tour.

ExampleShowsRunnable
oop_classes.bpriv methods and fields, static fields, abstract methods, interfaces, and a singletonyes
generic_structs.bGeneric structs, defaults, read methods, inout fn, and static factoriesyes

Walked through in OOP classes and value types.

ExampleShowsRunnable
threads.bOS threads, generics, enums, Option/Result, decimalyes
atomics.bTyped Atomic<T> with explicit MemoryOrderyes
wide_concurrency.bStruct and enum values across channelsyes
wide_sync.bStruct and enum values through a Mutexyes

Walked through in Threads and channels and Atomics.

ExampleShowsRunnable
files.bFile/Dir statics, positional I/O, errorsyes
reader.bBuffered line readingyes
kv.bAppend-only key-value store with durable commityes
locks.bAdvisory file locks (flock), single-writer patternyes
mmap.bWhole-file memory mappingyes
shared_memory.bPOSIX shared memory as an MMapyes

Walked through in Files and a KV store.

ExampleShowsRunnable
net.bTCP and UDP on loopback in one processyes
poller.bWaiting on many descriptors (epoll/kqueue)yes
signals.bSignals as data, via the polleryes

Walked through in Networking.

ExampleShowsRunnable
ffi.bextern "C" calls to libc, RawPtr in unsafeyes
c_layout_structs.bextern "C" struct layoutyes
c_layout_unions.bextern "C" union layoutyes
dynamic_library.bLoad a shared library at run time, call an addressyes (needs a library to load)
packed.bpacked and align(N)yes
layout.bsize_of / align_of / offset_ofyes
raw_slices.bSlice<T>yes
unsafe_raw.bRawPtr null / alloc / offset / read / writeyes

Walked through in C interop (FFI).

ExampleShowsRunnable
box.bGeneric move-only handlesyes
arena.bGeneric move-only handlesyes
shared_weak.bShared and Weakyes
ordered_map.bOrderedMapyes
cycles.bReference cycles freed by the collectoryes
ctors.bThe init / deinit contractyes
generic_deinit.bA generic class with deinit and a closure factoryyes
ExampleShowsRunnable
child_process.bCommand.start() returning a Child (uses async)yes
processes.bCommand.run()yes
clocks_random.bTime and randomyes
cpu_dispatch.bcpu.has and a feature-gated functionyes (feature-gated code needs the CPU feature)
intrinsics.bstd.intrinsicyes
inline_asm.bstd.asmyes
target_info.bstd.targetyes
ExampleShowsRunnable
simd.bSimd4f32 fused multiply-addyes (some SIMD needs CPU features)
simd_families.bSIMD family naming, width and feature rulesyes (some SIMD needs CPU features)
ExampleShowsRunnable
fmt.bstd.fmtyes
strings.bString operationsyes
bytes.bBytes operationsyes
containers.bCollectionsyes
stdlib_beans.bA stdlib touryes

Some of these tours end in a deliberate panic. That is part of their job as a test.

ExampleShowsRunnable
shop/Three packages, cross-package interfaces and genericsyes, via examples/shop/main.b

Walked through in A local-package project.

These need a specific target or runtime and will not run on a plain desktop build:

ExampleNeeds
embedded.bA 32-bit no-OS target (decimal is refused there)
freestanding.b--runtime freestanding

The language guide covers every feature in order, and the builtin reference and standard library reference are there to look things up.