Hello, World!
It's traditional to begin learning a new programming language by writing a little program that prints the text "Hello, world!" to the screen, so we'll do that here! You'll also write your first Oxide program.
Creating a Project Directory
First, create a directory where you'll store your Oxide code. It doesn't matter to Oxide where your code lives, but for the exercises and projects in this book, we suggest creating a projects directory in your home directory and keeping all your projects there.
Open a terminal and run the following commands to create a projects directory and a hello_world project within it.
On Linux, macOS, and PowerShell on Windows, enter this:
$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world
On Windows CMD, enter this:
> mkdir %USERPROFILE%\projects
> cd /d %USERPROFILE%\projects
> mkdir hello_world
> cd hello_world
Writing and Running the Program
Next, make a new source file and call it main.ox. Oxide files always end with the .ox extension. If you're using more than one word in your filename, the convention is to use an underscore to separate them. For example, you'd use hello_world.ox rather than helloworld.ox.
Now open the main.ox file you just created and enter the code in Listing 1-1:
Filename: main.ox
fn main() {
println!("Hello, world!")
}
Listing 1-1: A program that prints "Hello, world!"
Save the file and go back to your terminal window in the ~/projects/hello_world directory. On Linux or macOS, enter the following commands to compile and run the file:
$ oxidec main.ox
$ ./main
Hello, world!
On Windows, enter:
> oxidec main.ox
> .\main.exe
Hello, world!
Regardless of your operating system, the string Hello, world! should print to the terminal. If you didn't see this output, refer to the Troubleshooting section for ways to get help.
Anatomy of an Oxide Program
Let's review this "Hello, world!" program in detail. Here's the first piece:
fn main() {
}
These lines define a function named main. The main function is special: it's always the first code that runs in every executable Oxide program. We declare it using the keyword fn, and it takes no parameters and returns nothing. If there were parameters, they would go inside the parentheses (). Also notice that the function body is wrapped in curly brackets {}. Oxide requires curly brackets around all function bodies.
Next is this line:
println!("Hello, world!")
This line does all the work in this little program. It calls the println! macro with the string "Hello, world!" as an argument. The ! indicates that println! is a macro rather than a normal function. (We'll learn more about macros in detail in Chapter 19.) The macro prints the string to the screen.
Semicolons and Optional Syntax
You might notice this program doesn't have a semicolon at the end of the println! line. In Oxide, semicolons are optional in most contexts. They're not required at the end of function calls or statements. You can write it either way:
fn main() {
println!("Hello, world!")
}
Or with semicolons, if you prefer:
fn main() {
println!("Hello, world!");
}
Both are valid Oxide. The choice is stylistic—pick what feels most natural to you.
Comparison with Rust
If you're familiar with Rust, you might notice some differences. In Rust, you would write:
fn main() { println!("Hello, world!"); }
The key differences between Oxide and Rust in this example:
- File extension: Oxide uses
.oxinstead of.rs - Semicolons: Oxide makes them optional; Rust requires them
- Compiler: Oxide uses
oxidec(the Oxide compiler, which is a rustc fork); Rust usesrustc
Everything else—the function syntax, the println! macro, and the overall program structure—is identical to Rust. This is because Oxide is fundamentally Rust with a different surface syntax.
Compiling and Running Are Separate Steps
You've just seen how to run a new program, but let me explain the process more fully.
Before running an Oxide program, you must compile it using the Oxide compiler, even for simple programs. The command is oxidec followed by your source filename:
$ oxidec main.ox
If you're on Windows, use:
> oxidec main.ox
This command compiles your main.ox file and creates an executable called main (or main.exe on Windows). You can then run the executable:
On Linux or macOS:
$ ./main
Hello, world!
On Windows:
> .\main.exe
Hello, world!
Even for one-line programs, you need to explicitly compile before running. This follows Rust's ahead-of-time compilation approach, which differs from interpreted languages like Python or JavaScript where you can run code directly. The benefit is performance: compiled code runs much faster than interpreted code, and you catch errors at compile time rather than runtime.
Troubleshooting
The most common problems beginners encounter:
Command not found: oxidec
This usually means the Oxide compiler isn't installed or isn't in your system's PATH. Refer to the Installation chapter to properly install Oxide.
Compilation errors
If you see errors when running oxidec, double-check that:
- Your file is saved as
main.ox(or another.oxfilename) - You're running the command from the directory containing your source file
- Your code matches Listing 1-1 exactly, including the curly brackets and parentheses
Program output doesn't appear
- On macOS or Linux, make sure you're using
./main(with the dot-slash) to run the executable - On Windows, make sure you're using
.\main.exe(with the backslash) - If the window closes immediately on Windows, try running it from PowerShell or Command Prompt directly
Congratulations! You've officially written your first Oxide program. Next, we'll look at Oxide's package manager and build system, Cargo, which makes creating more complex programs much easier.