Skip to content
01. Getting Started > Program Structure

Program Structure

Edit This Page

Hello, world!

Here’s one of the simplest vexide programs. It just prints a short message to the terminal:

main.rs
use vexide::prelude::*;#[vexide::main]async fn main(peripherals: Peripherals) {    println!("Hello, world!");}

We’ll go through what’s happening here one part at a time.

The vexide Prelude

use vexide::prelude::*;#[vexide::main]async fn main(peripherals: Peripherals) {    println!("Hello, world!");}

This line of code brings vexide’s prelude module into scope. All that this does is import a bunch of commonly used stuff, so you don’t have to type out their full name every time. For example, you can simply use Motor rather than vexide::devices::smart::motor::Motor.

For a full list of what’s imported by the prelude, see the module docs.

The Program Entrypoint

All vexide programs begin and end at the main function, which must be annotated with the #[vexide::main] macro.

use vexide::prelude::*;#[vexide::main]async fn main(peripherals: Peripherals) {    println!("Hello, world!");}

If you’re familiar with Rust, you might notice some key differences in vexide’s version of the main compared to Rust’s normal main function:

  1. main is an async fn. vexide ships with its own async runtime that serves as the basis for its multitasking features. In order to run other async functions, main itself must also be async.
  2. main takes a peripherals argument. This is an instance of the Peripherals struct that allows you to create devices and control your robot’s hardware. You can read more about that here.
  3. main is annotated with #[vexide::main]. This is a special attribute macro that sets up the real program entrypoint. Behind the scenes, this macro runs some additional low-level startup code and sets up vexide’s async runtime.

The println! Macro

This line does all of the work in our little program.

use vexide::prelude::*;#[vexide::main]async fn main(peripherals: Peripherals) {    println!("Hello, world!");}

println! simply prints some text for us to read. We’ll learn how to read this as it runs in a few pages. This macro isn’t a feature of vexide, but rather a part of Rust’s standard library.

If you’ve used Rust before, you should hopefully be familiar with this.