← Back to README | Examples | Built-ins →
The old /examples tree was consolidated into this document and docs/samples/ so language usage is documented in one place.
fn double(n: int) -> int {
return n * 2;
}
print("Hello, Carv!");
let x = 10;
x |> double |> print; // pipe operator: works with carv run (VM) and carv build (C codegen)
let name = "Carv";
let version = "0.4.0";
println(f"Welcome to {name} v{version}!");
println(f"2 + 3 = {2 + 3}");
let s = "hello";
let moved = s;
fn print_len(v: &string) {
println(len(v));
}
let msg = "world";
print_len(&msg);
fn divide(a: int, b: int) {
if b == 0 {
return Err("division by zero");
}
return Ok(a / b);
}
match divide(10, 2) {
Ok(v) => println(v),
Err(e) => println(e),
};
// math.carv
pub fn add(a: int, b: int) -> int {
return a + b;
}
// main.carv
require { add } from "./math";
println(add(1, 2));
require "net" as net;
let listener = net.tcp_listen("127.0.0.1", 8080);
let conn = net.tcp_accept(listener);
let req = net.tcp_read(conn, 4096);
println(req);
let body = "Hello from Carv TCP server!\n";
let response =
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: 28\r\n" +
"Connection: close\r\n\r\n" +
body;
net.tcp_write(conn, response);
net.tcp_close(conn);
net.tcp_close(listener);
All 20 sample programs in docs/samples/ work with both carv run (VM) and carv build (C codegen):
# Run with the built-in VM (no C compiler needed)
carv run docs/samples/01-hello.carv
carv run docs/samples/10-classes.carv
# Build to native binary via C
carv build docs/samples/01-hello.carv
./docs/samples/01-helloSamples cover: basics, variables, functions, control flow, for loops, strings, compound assignment, arrays, maps, classes, methods, interfaces, impl blocks, result types, match expressions, borrowing, function composition, modules, embedded features, and advanced concepts.
← Back to README | Examples | Built-ins →