Your first Zevic program
A one-page tour of the basics, for people new to programming. Every piece of code here was compiled and run to check it.
1. Setup
Install Zevic and check that zevic --version works. Programs go in files with the .zev extension. Any editor will do.
2. Hello, World
Create hello.zev.
module Main
import Zevic.Console
fn Main() {
Console.Println("Hello, World!")
}
Compile and run it.
zevic hello.zev -o hello
./hello
Hello, World!
module Mainsays this file is the entry module of the programimport Zevic.Consolemakes the standard library for console input and output availablefn Main()is where the program starts runningConsole.Println(...)prints a string on one line
3. Variables and types
Declare a variable with let (cannot be changed later) or mut (can be changed). You always write the type and the initial value.
module Main
import Zevic.Console
fn Main() {
let name: String = "Zevic"
let year: I32 = 2026
mut count: I32 = 0
count += 1
let pi: F64 = 3.14
let ok: Bool = true
Console.Println("${name} ${year} ${count} ${pi} ${ok}")
}
Zevic 2026 1 3.14 true
- Inside a string,
${ ... }takes an expression, and its value is placed into the string - The main types: integers are
I8I16I32I64(unsigned:U8…U64), decimals areF32F64, and there areBoolCharString - The contents of a
letvariable cannot be changed either (you cannotPushonto an array). Make itmutif you need to change it
4. Functions and arrays
module Main
import Zevic.Console
fn Square(n: I32) -> I32 {
n * n
}
fn Sum(xs: Array<I32>) -> I32 {
mut total: I32 = 0
for (x in xs) {
total += x
}
total
}
fn Main() {
let nums: Array<I32> = [1, 2, 3, 4, 5]
Console.Println("${Square(7)}")
Console.Println("sum = ${Sum(nums)}")
mut xs: Array<I64> = []
for (i in 0..5) {
xs.Push(i * i)
}
Console.Println("${xs.Len()} items, first ${xs[0]}, last ${xs[4]}")
}
49
sum = 15
5 items, first 0, last 16
- A function is
fn name(param: Type) -> ReturnType { ... }. The value of the last expression is the result (returncan be left out) 0..5is the range from 0 to 4, and0..=5includes 5- The
iinfor (i in 0..5)is anI64. To put it into anArray<I32>, convert the type explicitly, as ini.ToI32()
5. Handling failure: Option and Result
Zevic has no exceptions and no null. “There might be nothing” is an Option, and “this might fail” is a Result.
module Main
import Zevic.Console
enum Shape {
Circle(F64),
Rect(F64, F64),
fn Area() -> F64 {
match (this) {
Shape.Circle(r) => 3.14 * r * r,
Shape.Rect(w, h) => w * h,
}
}
}
fn Half(n: I32) -> Result<I32, Error> {
if (n % 2 != 0) {
return Err(Error.New(ErrorKind.Invalid, "${n} is odd"))
}
Ok(n / 2)
}
fn Quarter(n: I32) -> Result<I32, Error> {
let h: I32 = Half(n)?
Half(h)
}
fn Main() {
Console.Println("${Shape.Rect(2.0, 3.0).Area()}")
match (Quarter(12)) {
Ok(v) => Console.Println("ok ${v}"),
Err(e) => Console.Println("error: ${e.Message()}"),
}
match (Quarter(6)) {
Ok(v) => Console.Println("ok ${v}"),
Err(e) => Console.Println("error: ${e.Message()}"),
}
let words: Array<String> = ["a", "bb"]
match (words.Get(5)) {
Some(w) => Console.Println(w),
None => Console.Println("no such item"),
}
}
6.0
ok 3
error: 3 is odd
no such item
- An
enumis a type that is one of several shapes.matchhandles each shape separately, and the compiler rejects amatchthat does not cover them all expression?takesxout ofOk(x), and onErrreturns from the function right there- Reading outside an array with
xs[10]is a bug, so it is aPanic(the program stops). When the index might be out of range, useGet, which gives anOption
6. Passing functions around
module Main
import Zevic.Console
fn Main() {
let nums: Array<I32> = [1, 2, 3, 4, 5, 6]
let double: fn(I32) -> I32 = (x: I32) => x * 2
Console.Println("${double(21)}")
mut total: I32 = 0
nums.Each((x: I32) => { total += x })
Console.Println("total ${total}")
mut m: Map<String, I32> = Map.New()
m.Set("a", 1)
m.Set("b", 2)
match (m.Get("b")) {
Some(v) => Console.Println("b = ${v}"),
None => Console.Println("none"),
}
}
42
total 21
b = 2
(x: I32) => x * 2 is an anonymous function (a lambda). It can capture mut variables of the surrounding function, and changes are visible outside.
7. Writing tests
A test "name" { ... } block is run by zevpm test.
module Main
import Zevic.Console
fn Add(a: I32, b: I32) -> I32 { a + b }
test "Add adds" {
if (Add(2, 3) != 5) { Panic("bad") }
}
fn Main() {
Console.Println("${Add(2, 3)}")
}
Create a project with zevpm.
zevpm new myapp
cd myapp
zevpm run # build and run
zevpm test # run the tests
zevfmt -w . # format the code
8. Next steps
struct/class/interface: 05_shapes in the examples- Files, external programs, JSON and HTTP: examples
- Working with C: 06_c_interop
- The language specification
- If you know other languages, see From other languages too.