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.
How to run an example
Section titled “How to run an example”Point beansc run at a file:
beansc run examples/hello.bMost 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:
beansc run examples/shop/main.bSome examples are target-gated: they need a special target or CPU and will not run on a plain desktop build. Those are marked below.
Language basics
Section titled “Language basics”| Example | Shows | Runnable |
|---|---|---|
| hello.b | Hello world and string interpolation | yes |
| tour.b | A one-file tour of every language idea | yes |
Walked through in Hello and the tour.
Object-oriented code and value types
Section titled “Object-oriented code and value types”| Example | Shows | Runnable |
|---|---|---|
| oop_classes.b | priv methods and fields, static fields, abstract methods, interfaces, and a singleton | yes |
| generic_structs.b | Generic structs, defaults, read methods, inout fn, and static factories | yes |
Walked through in OOP classes and value types.
Concurrency
Section titled “Concurrency”| Example | Shows | Runnable |
|---|---|---|
| threads.b | OS threads, generics, enums, Option/Result, decimal | yes |
| atomics.b | Typed Atomic<T> with explicit MemoryOrder | yes |
| wide_concurrency.b | Struct and enum values across channels | yes |
| wide_sync.b | Struct and enum values through a Mutex | yes |
Walked through in Threads and channels and Atomics.
Files and storage
Section titled “Files and storage”| Example | Shows | Runnable |
|---|---|---|
| files.b | File/Dir statics, positional I/O, errors | yes |
| reader.b | Buffered line reading | yes |
| kv.b | Append-only key-value store with durable commit | yes |
| locks.b | Advisory file locks (flock), single-writer pattern | yes |
| mmap.b | Whole-file memory mapping | yes |
| shared_memory.b | POSIX shared memory as an MMap | yes |
Walked through in Files and a KV store.
Networking
Section titled “Networking”| Example | Shows | Runnable |
|---|---|---|
| net.b | TCP and UDP on loopback in one process | yes |
| poller.b | Waiting on many descriptors (epoll/kqueue) | yes |
| signals.b | Signals as data, via the poller | yes |
Walked through in Networking.
C interop and low-level
Section titled “C interop and low-level”| Example | Shows | Runnable |
|---|---|---|
| ffi.b | extern "C" calls to libc, RawPtr in unsafe | yes |
| c_layout_structs.b | extern "C" struct layout | yes |
| c_layout_unions.b | extern "C" union layout | yes |
| dynamic_library.b | Load a shared library at run time, call an address | yes (needs a library to load) |
| packed.b | packed and align(N) | yes |
| layout.b | size_of / align_of / offset_of | yes |
| raw_slices.b | Slice<T> | yes |
| unsafe_raw.b | RawPtr null / alloc / offset / read / write | yes |
Walked through in C interop (FFI).
Memory and ownership
Section titled “Memory and ownership”| Example | Shows | Runnable |
|---|---|---|
| box.b | Generic move-only handles | yes |
| arena.b | Generic move-only handles | yes |
| shared_weak.b | Shared and Weak | yes |
| ordered_map.b | OrderedMap | yes |
| cycles.b | Reference cycles freed by the collector | yes |
| ctors.b | The init / deinit contract | yes |
| generic_deinit.b | A generic class with deinit and a closure factory | yes |
Processes, time, and the machine
Section titled “Processes, time, and the machine”| Example | Shows | Runnable |
|---|---|---|
| child_process.b | Command.start() returning a Child (uses async) | yes |
| processes.b | Command.run() | yes |
| clocks_random.b | Time and random | yes |
| cpu_dispatch.b | cpu.has and a feature-gated function | yes (feature-gated code needs the CPU feature) |
| intrinsics.b | std.intrinsic | yes |
| inline_asm.b | std.asm | yes |
| target_info.b | std.target | yes |
| Example | Shows | Runnable |
|---|---|---|
| simd.b | Simd4f32 fused multiply-add | yes (some SIMD needs CPU features) |
| simd_families.b | SIMD family naming, width and feature rules | yes (some SIMD needs CPU features) |
Standard library tours
Section titled “Standard library tours”| Example | Shows | Runnable |
|---|---|---|
| fmt.b | std.fmt | yes |
| strings.b | String operations | yes |
| bytes.b | Bytes operations | yes |
| containers.b | Collections | yes |
| stdlib_beans.b | A stdlib tour | yes |
Some of these tours end in a deliberate panic. That is part of their job as a test.
Multi-package project
Section titled “Multi-package project”| Example | Shows | Runnable |
|---|---|---|
| shop/ | Three packages, cross-package interfaces and generics | yes, via examples/shop/main.b |
Walked through in A local-package project.
Target-gated examples
Section titled “Target-gated examples”These need a specific target or runtime and will not run on a plain desktop build:
| Example | Needs |
|---|---|
| embedded.b | A 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.