← 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;
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);
Use these maintained sample files:
docs/samples/hello.carvdocs/samples/showcase.carv
Run with:
./build/carv run docs/samples/hello.carv
./build/carv run docs/samples/showcase.carv← Back to README | Examples | Built-ins →