Syntax Reference
Complete quick reference for Nova language syntax. Copy-paste ready examples.
Keywords
Nova has 32 reserved keywords:
as
async
await
break
const
continue
else
enum
false
fn
for
if
impl
in
let
loop
match
mod
mut
pub
return
self
Self
static
struct
trait
true
type
unsafe
use
where
while
Literals
Integers
42 // decimal
1_000_000 // with separators
0xFF // hexadecimal
0o77 // octal
0b1010 // binary
Floats
3.14 // basic
1e10 // scientific
2.5e-3 // with exponent
1_000.5 // with separators
Strings
"hello" // basic string
"line\nbreak" // escape sequences
"tab\there" // tab character
"quote: \"" // escaped quote
Characters
'a' // character
'\n' // newline
'\t' // tab
'\\' // backslash
Booleans
true
false
Operators
Arithmetic
| Operator | Description | Example |
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder | a % b |
Comparison
| Operator | Description | Example |
== | Equal | a == b |
!= | Not equal | a != b |
< | Less than | a < b |
> | Greater than | a > b |
<= | Less or equal | a <= b |
>= | Greater or equal | a >= b |
Logical
| Operator | Description | Example |
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
Bitwise
| Operator | Description | Example |
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
Assignment
| Operator | Description | Example |
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Remainder and assign | a %= b |
Range
| Operator | Description | Example |
.. | Exclusive range | 0..10 |
..= | Inclusive range | 0..=10 |
Other
| Operator | Description | Example |
-> | Return type | fn foo() -> i32 |
=> | Match arm | x => x + 1 |
:: | Path separator | std::io::Read |
. | Field access | point.x |
? | Try operator | result? |
& | Reference | &value |
* | Dereference | *ptr |
Types
Named Types
i32 // 32-bit signed integer
u64 // 64-bit unsigned integer
f64 // 64-bit float
bool // boolean
String // string type
Vec<i32> // generic type
HashMap<String, i32> // multiple type parameters
Compound Types
(i32, i32) // tuple
[i32; 10] // array with size
[i32] // slice
Reference Types
&i32 // immutable reference
&mut i32 // mutable reference
Function Types
fn(i32) -> i32 // function type
fn(i32, i32) -> bool // multiple parameters
Special Types
! // never type (function doesn't return)
_ // inferred type (let compiler decide)
Functions
Basic Function
fn greet() {
println("Hello!");
}
With Parameters
fn add(a: i32, b: i32) -> i32 {
a + b
}
With Generics
fn first<T>(items: Vec<T>) -> Option<T> {
items.get(0)
}
With Where Clause
fn print_all<T>(items: Vec<T>)
where
T: Display
{
for item in items {
println(item);
}
}
Closures
let add = |a, b| a + b;
let square = |x: i32| -> i32 { x * x };
let sum = numbers.fold(0, |acc, x| acc + x);
Structs
Basic Struct
struct Point {
x: f64,
y: f64,
}
Generic Struct
struct Pair<T, U> {
first: T,
second: U,
}
Creating Instances
let origin = Point { x: 0.0, y: 0.0 };
let pair = Pair { first: "hello", second: 42 };
Impl Block
impl Point {
fn new(x: f64, y: f64) -> Self {
Point { x, y }
}
fn distance(&self, other: &Point) -> f64 {
let dx = self.x - other.x;
let dy = self.y - other.y;
(dx * dx + dy * dy).sqrt()
}
}
Enums
Unit Variants
enum Direction {
North,
South,
East,
West,
}
Tuple Variants
enum Color {
Rgb(u8, u8, u8),
Hex(String),
}
Struct Variants
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
}
Generic Enum
enum Option<T> {
Some(T),
None,
}
enum Result<T, E> {
Ok(T),
Err(E),
}
Traits
Defining a Trait
trait Drawable {
fn draw(&self);
// Default implementation
fn clear(&self) {
println("Clearing...");
}
}
Implementing a Trait
impl Drawable for Point {
fn draw(&self) {
println("Drawing point at ({}, {})", self.x, self.y);
}
}
Trait Bounds
fn draw_all<T: Drawable>(items: Vec<T>) {
for item in items {
item.draw();
}
}
Control Flow
If Expression
if x > 0 {
println("positive");
} else if x < 0 {
println("negative");
} else {
println("zero");
}
// If as expression
let sign = if x >= 0 { 1 } else { -1 };
Match Expression
match value {
0 => println("zero"),
1 | 2 => println("one or two"),
3..10 => println("three to nine"),
n if n > 100 => println("big: {}", n),
_ => println("something else"),
}
While Loop
while count > 0 {
println(count);
count -= 1;
}
For Loop
for i in 0..10 {
println(i);
}
for item in items {
process(item);
}
Loop
loop {
if done {
break;
}
do_work();
}
// Loop with value
let result = loop {
if found {
break value;
}
};
Break and Continue
for i in 0..100 {
if i % 2 == 0 {
continue; // skip even numbers
}
if i > 50 {
break; // stop at 50
}
}
Patterns
Pattern Types
// Wildcard
let _ = ignored_value;
// Variable binding
let x = 42;
let mut y = 0;
// Tuple destructuring
let (a, b) = (1, 2);
let (first, _, third) = triple;
// Struct destructuring
let Point { x, y } = point;
let Point { x: px, y: py } = point;
Match Patterns
match option {
Some(value) => use_value(value),
None => default(),
}
match result {
Ok(data) => process(data),
Err(e) => handle_error(e),
}
// Or patterns
match x {
1 | 2 | 3 => small(),
_ => other(),
}
// Guard clauses
match x {
n if n < 0 => negative(),
n if n > 0 => positive(),
_ => zero(),
}
Expressions
Variables
let x = 42; // immutable
let mut y = 0; // mutable
let z: i32 = 100; // with type
Function Calls
foo(); // no arguments
add(1, 2); // with arguments
object.method(); // method call
object.method(arg); // method with argument
Field Access
point.x // struct field
tuple.0 // tuple index
Array/Index
[1, 2, 3] // array literal
array[0] // indexing
array[1..3] // slicing
References
&value // immutable reference
&mut value // mutable reference
*ptr // dereference
Block Expressions
let result = {
let a = 1;
let b = 2;
a + b // last expression is the value
};
Async/Await
async fn fetch_data() -> Result<Data, Error> {
let response = client.get(url).await?;
let data = response.json().await?;
Ok(data)
}
Copy-Paste Ready!
All examples on this page use Nova syntax and can be used in your code.
The language is designed to be familiar to Rust developers while adding verification features.