Examples Gallery

184+ example programs organized by category and difficulty

This gallery provides an overview of the Vais example programs in the examples/ directory. Examples range from basic "hello world" programs to advanced GPU, async, and self-hosting compiler demos.


Basics

Introductory programs demonstrating core syntax and features.

ExampleDescriptionKey Concepts
hello.vaisMinimal program returning exit codeF main()
hello_world.vaisPrint "Hello, World!"println, strings
fib.vaisFibonacci with self-recursion@ operator, recursion
math.vaisArithmetic operationsOperators, expressions
math_test.vaisMath function testsFunction calls
putchar_var.vaisCharacter outputVariables, FFI
printf_test.vaisFormatted printingprintf, format strings

Featured: Fibonacci

# Self-recursion with @ operator
F fib(n:i64)->i64 = n<2 ? n : @(n-1) + @(n-2)

F main()->i64 = fib(10)   # Returns 55

Control Flow

If/else, loops, match, and branching patterns.

ExampleDescriptionKey Concepts
control_flow.vaisMax, countdown, factorialI/E, ternary ?:
loop_break_test.vaisLoop with breakL, B (break)
loop_opt_test.vaisLoop optimizationL, C (continue)
match_test.vaisPattern matching basicsM (match)
match_binding.vaisMatch with variable bindingM, bindings
range_test.vaisRange iteration.. operator
range_comprehensive_test.vaisComplete range testsRange types, inclusive
defer_test.vaisDeferred executionD (defer)
defer_simple.vaisSimple deferD

Featured: Pattern Matching

F describe(x: i64) -> i64 {
    M x {
        0 => 100,
        1 => 200,
        _ => 999
    }
}

Functions and Closures

Functions, lambdas, closures, and the pipe operator.

ExampleDescriptionKey Concepts
pipe_operator.vaisPipe chaining |>Pipe operator
closure_simple.vaisSimple closure|x| expr
closure_test.vaisClosure capturingCapture, closures
lambda_test.vaisLambda expressionsLambdas
inline_test.vaisInline functions#[inline]
tco_tail_call.vaisTail call optimizationTCO, @
tco_stress.vaisTCO stress testDeep recursion

Featured: Pipe Operator

F double(x: i64) -> i64 = x * 2
F add_ten(x: i64) -> i64 = x + 10

F main() -> i64 {
    result := 5 |> double |> add_ten   # 20
    result
}

Types and Structs

Struct definitions, methods, enums, and type features.

ExampleDescriptionKey Concepts
enum_test.vaisEnum variantsE (enum), variants
enum_struct_variant_test.vaisStruct-like enum variantsE, struct variants
method_test.vaisStruct methodsX (impl), methods
destructuring.vaisDestructuringPattern destructuring
union_test.vaisUnion typesO (union)
slice_test.vaisSlice operations&[T], fat pointers
type_infer_params.vaisType inference:= inference
linear_types_test.vaisLinear typesOwnership, move

Generics and Traits

Generic programming and trait-based polymorphism.

ExampleDescriptionKey Concepts
generic_test.vaisBasic generics<T>
generic_struct_test.vaisGeneric structsS Pair<T>
generic_bounds_test.vaisTrait bounds<T: Trait>
generic_vec_test.vaisGeneric Vec usageVec<T>
const_generic_test.vaisConst generics<const N: i64>
trait_test.vaisTrait definitionW (trait)
trait_advanced_test.vaisAdvanced traitsDefault methods
trait_iter_test.vaisIterator traitW Iterator
gat_container.vaisGAT containersGATs
gat_functor.vaisGAT functorsGATs
gat_iterator.vaisGAT iteratorsGATs

Collections

Standard library collection types.

ExampleDescriptionKey Concepts
simple_vec_test.vaisVec basicsVec<T>, push/pop
minimal_vec_test.vaisMinimal VecAllocation
simple_hashmap_test.vaisHashMap basicsHashMap<K,V>
map_literal.vaisMap literal syntaxMap literals
btreemap_test.vaisBTreeMap ordered mapBTreeMap
set_test.vaisSet operationsSet<T>
deque_test.vaisDouble-ended queueDeque<T>
priority_queue_test.vaisPriority queuePriorityQueue
arrays.vaisArray operationsArrays
iter_test.vaisIterator patternsIterator trait

Error Handling

Option, Result, and error patterns.

ExampleDescriptionKey Concepts
option_test.vaisOption basicsOption<T>
option_test2.vaisOption advancedSome/None
option_test3.vaisOption chaining? operator
result_test.vaisResult typeResult<T,E>
option_result_test.vaisCombined patternsOption + Result
pattern_full_test.vaisFull pattern matchingGuards, nested
pattern_alias.vaisPattern aliasx @ pattern

I/O and Networking

File, network, and HTTP operations.

ExampleDescriptionKey Concepts
io_test.vaisI/O operationsstd/io
file_test.vaisFile read/writestd/file
http_test.vaisHTTP clientstd/http
http_server_example.vaisHTTP serverstd/http_server
websocket_example.vaisWebSocketstd/websocket
ipv6_test.vaisIPv6 networkingIPv6
ipv6_dual_stack.vaisDual-stack networkingIPv4/v6

Data Formats

JSON, TOML, templates, and serialization.

ExampleDescriptionKey Concepts
json_test.vaisJSON builder APIstd/json
template_example.vaisString templatesstd/template
compress_example.vaisData compressionstd/compress
crc32.vaisCRC32 checksumsstd/crc32
pilot_json2toml.vaisJSON to TOML converterJSON, TOML

Async and Concurrency

Async operations, threads, and synchronization.

ExampleDescriptionKey Concepts
async_test.vaisAsync basicsA (async), Y (await)
async_reactor_test.vaisAsync reactorEvent loop
spawn_test.vaisTask spawningspawn
thread_test.vaisThread creationstd/thread
sync_test.vaisMutex/lockstd/sync
concurrency_stress.vaisConcurrency stressThread safety
lazy_test.vaisLazy evaluationlazy/force
lazy_simple.vaisSimple lazyThunks

Memory and System

Memory management, GC, and system operations.

ExampleDescriptionKey Concepts
memory_test.vaisMemory operationsmalloc/free
malloc_test.vaisManual allocationPointers
rc_test.vaisReference countingRc<T>
gc_test.vaisGarbage collectorstd/gc
gc_vec_test.vaisGC with VecGC + collections
gc_simple_demo.vaisGC demoGC basics
lifetime_test.vaisLifetime checkingBorrow checker
runtime_test.vaisRuntime systemstd/runtime

Databases

Database integration examples.

ExampleDescriptionKey Concepts
sqlite_example.vaisSQLite operationsstd/sqlite
postgres_example.vaisPostgreSQL clientstd/postgres
orm_example.vaisORM usagestd/orm

WebAssembly

WASM target and interop examples.

ExampleDescriptionKey Concepts
wasm_calculator.vaisWASM calculator#[wasm_export]
wasm_interop.vaisJS/WASM interop#[wasm_import]
wasm_api_client.vaisWASM API clientFetch, DOM
wasm_todo_app.vaisWASM todo appFull app
js_target.vaisJavaScript target--target js
js_target_advanced.vaisAdvanced JS outputESM modules

GPU

GPU computing examples.

ExampleDescriptionKey Concepts
gpu_vector_add.vaisGPU vector additionstd/gpu, kernels
simd_test.vaisSIMD operationsstd/simd
simd_distance.vaisSIMD distance calcVectorization

Macros and Metaprogramming

Macro system and compile-time features.

ExampleDescriptionKey Concepts
macro_test.vaisDeclarative macrosmacro!
comptime_test.vaisCompile-time evalcomptime
comptime_simple.vaisSimple comptimeConst evaluation
contract_test.vaisDesign by contractPre/postconditions

Benchmarks

Performance measurement programs.

ExampleDescriptionKey Concepts
bench_fibonacci.vaisFibonacci benchmarkRecursive performance
bench_compute.vaisCompute benchmarkArithmetic performance
bench_sorting.vaisSorting benchmarkAlgorithm performance
bench_matrix.vaisMatrix operationsDense computation
bench_tree.vaisTree benchmarkData structure perf
stress_memory.vaisMemory stress testAllocation patterns
stress_fd.vaisFile descriptor stressI/O limits

Self-Hosting

Self-hosting compiler components in Vais.

ExampleDescriptionKey Concepts
selfhost_arith.vaisArithmetic codegenBootstrap
selfhost_loop.vaisLoop codegenBootstrap
selfhost_cond.vaisConditional codegenBootstrap
selfhost_nested.vaisNested calls codegenBootstrap
selfhost_bitwise.vaisBitwise ops codegenBootstrap

Pilot Projects

Complete application examples.

ExampleDescriptionKey Concepts
pilot_rest_api.vaisREST API serverHTTP, routing
pilot_json2toml.vaisJSON-to-TOML converterData conversion
tutorial_wc.vaisWord count toolCLI tutorial
tutorial_pipeline.vaisData pipelineETL pattern

Running Examples

# Compile and run
cargo run --bin vaisc -- examples/fib.vais

# Compile to JavaScript
cargo run --bin vaisc -- --target js examples/js_target.vais

# Compile to WASM
cargo run --bin vaisc -- --target wasm32-unknown-unknown examples/wasm_calculator.vais