Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vx for Rust programmers

Vx looks like Rust, so Rust that almost compiles is the first Vx most people write. This chapter lists what to change. Each item shows the Rust form and the Vx form.

Modules and items

  • import, not use. use core::option; is import core::option;. An import brings in everything the module declares.
  • pub exports a symbol. Every top-level item is visible to a module that imports it, with or without pub. A pub fn is also exported from the object file, so C can call it.
  • No attributes. There is no #[derive(..)], #[test] or #[inline].

Functions and methods

  • The return type is always written. fn log(x : i32) is fn log(x : i32) -> void, and main returns i32: fn main() -> i32 { .. return 0; }.
  • self has a type. fn get(&self) is fn get(self : &Self), &mut self is self : &mut Self, and self is self : Self. The type’s own name works too: self : &Point.
  • #[track_caller] is a parameter. End the parameters with at : Caller (import core::panic;). A call that leaves the argument out passes its own place, in at.location: a Location with file, line and column. To report the place of your own caller, give your at to the call. Location::here() is Rust’s Location::caller() and file!(), line!() and column!() in one.
  • Closure parameters have types. |x| x + 1 is | x : i64 | x + 1.
  • No async, and no tokio. A tasks block and spawn start tasks, and a function that waits is a normal function. Writing parallel code maps tokio to Vx.
  • No impl Trait parameters. fn show(g : &impl Area) is fn show<T : Area>(g : &T).
  • An impl is for the type itself. impl Area for &Rect and impl Area for *const Rect are errors. Write impl Area for Rect.

Types and values

  • No turbofish. Vec::<i32>::new() is Vec<i32>::new().
  • An enum variant is written with its type. Some(3) is Option<i64>::Some(3), and None is Option<i64>::None(). Option comes from import core::option;.
  • An integer variable with no written type is i32. Rust reads let mut total = 0; as an i64 when i64s are added to it later. Vx does not, so write let mut total : i64 = 0;.
  • Arrays are tensors. [i32; 4] is Tensor<i32, [4]>. The shape is part of the type, and a ? marks a size known only at run time: Tensor<f32, [?, ?]>.
  • The atomic orders are AtomicOrdering, because Ordering is the result of a comparison. A Relaxed read is compiled as an Acquire read, so no value comes out of thin air. -fweak-relaxed-atomics gives Rust’s Relaxed. See Memory orders.
  • Float math is correctly rounded on the CPU and on NVIDIA GPUs. x.exp() and the other float methods are written in Vx, and the f32 ones are correctly rounded. An Intel GPU uses its own functions instead. Rust’s f32::exp calls the platform’s C library, as Vx’s x.fast_exp() does. See Exact and fast math.
  • Uninitialized storage is unsafe. MaybeUninit has no equivalent. Write unsafe { Tensor<f32, [4]>::uninit() } and set every element before reading it, or use Tensor<f32, [4]>::new(), which is zeroed.

Control flow

  • A match arm’s body is a block. 0 => 7, is 0 => { 7 },.
  • No if let or while let. Use match.
  • ? does not convert the error. x? passes the Err up unchanged, so its type must be the error type of the function. Use .map_err(..) to convert it. ? takes only a Result.
  • ? in a tasks block fails the block, not the function. The block gives that Err, and it cancels its tasks. In a task, ? ends the task, and the task fails. In a closure, ? needs the closure’s type written: |x : i32| -> Result<i32, E> { .. }.

Printing

  • print(x) prints one value with no newline.
  • print!("text") prints text. println!("{} and {}", a, b) puts each value in place of a {} and ends the line. eprint! and eprintln! do the same on standard error.
  • There is no function called println: println(x) is an error. Write println!("{}", x).

What Rust does not have

Rust has one kind of memory. Vx describes several, and checks where data lives:

  • A tensor’s third type argument is the memory it lives in: Tensor<f32, [4, 4], Memory::NPU_HBM>.
  • transfer(t, Memory::NPU_HBM) moves data to another memory. It is required even when the move costs nothing on the hardware.
  • spawn on(Topology::NPU[0]) { .. } runs a block on a device. Everything the block uses must already be in memory that device can reach.
  • unsafe allows raw pointers and calls to unsafe fn, as in Rust. It does not allow reading device memory from the host: that stays a compile error.

Topologies and memory explains these.

A complete program

Most of the differences above, in one program:

import core::iter;
import core::option;

trait Area {
    fn area(self : &Self) -> i64;
}

struct Rect {
    w : i64,
    h : i64,
}

impl Rect {
    fn grow(self : &mut Rect, by : i64) -> void {
        self.w = self.w + by;
        self.h = self.h + by;
    }
}

impl Area for Rect {
    fn area(self : &Rect) -> i64 {
        return self.w * self.h;
    }
}

fn total<T : Area>(a : &T, b : &T) -> i64 {
    return a.area() + b.area();
}

fn half(n : i64) -> Option<i64> {
    if n % 2 == 0 {
        return Option<i64>::Some(n / 2);
    }
    return Option<i64>::None();
}

// Rust would write `half(half(n)?)`.
fn quarter(n : i64) -> Option<i64> {
    match half(n) {
        Option<i64>::Some(h) => { return half(h); },
        Option<i64>::None() => { return Option<i64>::None(); }
    }
}

fn main() -> i32 {
    let mut r = Rect { w : 2, h : 3 };
    r.grow(1);
    let s = Rect { w : 1, h : 1 };
    println!("area {}", total(&r, &s));

    let squares = range(1i64, 4i64).map(| x : i64 | x * x).sum();
    println!("squares {}", squares);

    match quarter(12) {
        Option<i64>::Some(q) => { println!("quarter {}", q); },
        Option<i64>::None() => { println!("no quarter"); }
    }

    let mut t : Tensor<f32, [3]> = unsafe { Tensor<f32, [3]>::uninit() };
    for i in 0..3 {
        t[i] = 0.5 * (i as f32);
    }
    println!("last {}", t[2]);
    return 0;
}

It prints:

area 13
squares 14
quarter 3
last 1