
Rust: examples explained
Maturity and boundaries: experimental
Implemented scope: Modules, structs, enums, generics, traits, closures, selected macros, Vec/Box/Option/Result and C FFI appear in the examples.
Boundary: There is no complete borrow/lifetime checker, Cargo, full standard library, procedural macros or unwinding; arbitrary Cargo-project compatibility is not claimed.
Rust supported forms and scope
Browse the language-specific supported-command and scope reference, with searchable categories and explicit boundaries.
Open scope referenceThe Rust input uses fn, let and explicit types. These small examples cover loops, integer arithmetic and tuples; they are not a promise of arbitrary Cargo-project compatibility.
Twelve short examples and one larger program. Choose a topic or work through the examples in order.
1. 1 + 2 + 3 + 4 + 5 = 15
The numbers 1 through 5 are added to 15. Return value 15 is checked as the process exit code.
fn main() -> i64 {
let mut sum: i64 = 0;
let mut i: i64 = 1;
loop {
sum = sum + i;
if i >= 5 { break; }
i = i + 1;
}
return sum; // 15
}Expected exit code: 15
PBXB64.exe sum15-rust.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Executed natively with the V22 candidate from 2026-09-30; final-compiler verification pending.
2. Integer division and remainder
20 / 4 is 5 and 17 % 5 is 2. main returns their sum 7 as i64. All operands here are integers.
fn main() -> i64 {
return (20 / 4) + (17 % 5);
}
Expected exit code: 7
PBXB64.exe test_rust_arith.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
3. Two values in a tuple
p has type (i64, i64) and contains 20 and 22. p.0 and p.1 access its fields; main returns 42.
fn main() -> i64 {
let p: (i64, i64) = (20, 22);
return p.0 + p.1;
}
Expected exit code: 42
PBXB64.exe test_rust_tuple.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Canonical test case with an expected result; no new native V22 check here. Final execution pending.
4. Two typed local values
Step-by-step flow
- let defines x as 10 and y as 32, both i64.
- Neither value is modified in this program.
- return x + y supplies 42 to the process without printing anything.
fn main() -> i64 {
let x: i64 = 10;
let y: i64 = 32;
return x + y;
}
Expected exit code: 42
PBXB64.exe rust-test_rust_vars.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
5. A function with two arguments
Step-by-step flow
- add declares two i64 parameters and an i64 result type.
- main passes 10 and 32.
- The sum is returned directly from main.
fn add(a: i64, b: i64) -> i64 {
return a + b;
}
fn main() -> i64 {
return add(10, 32);
}
Expected exit code: 42
PBXB64.exe rust-test_rust_fn.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
6. Factorial with a stopping condition
Step-by-step flow
- For n <= 1, fact returns base value 1.
- Other calls multiply n by fact(n - 1).
- fact(5) is 5 × 4 × 3 × 2 × 1 = 120. This small input stays within i64 range.
fn fact(n: i64) -> i64 {
if n <= 1 { return 1; }
return n * fact(n - 1);
}
fn main() -> i64 {
return fact(5);
}
Expected exit code: 120
PBXB64.exe rust-test_rust_rec.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
7. if/else with two return values
Step-by-step flow
- x contains 5, so x > 3 is true.
- The first branch returns 1 and ends main.
- In this example, 1 is the expected result rather than a reported test failure.
fn main() -> i64 {
let x: i64 = 5;
if x > 3 {
return 1;
} else {
return 0;
}
}
Expected exit code: 1
PBXB64.exe rust-test_rust_if.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
8. loop with an explicit break
Step-by-step flow
- let mut permits changing counter i.
- Each iteration increments i, then checks i >= 5.
- break exits at 5 and return supplies the reached counter value.
fn main() -> i64 {
let mut i: i64 = 0;
loop {
i = i + 1;
if i >= 5 { break; }
}
return i;
}
Expected exit code: 5
PBXB64.exe rust-test_rust_loop.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
9. while sums numbers 1 through 5
Step-by-step flow
- sum starts at 0 and i at 1; both are mutable.
- Condition i <= 5 includes 5; the body adds i and increments it.
- After the last iteration, i = 6 and sum = 15.
fn main() -> i64 {
let mut sum: i64 = 0;
let mut i: i64 = 1;
while i <= 5 {
sum = sum + i;
i = i + 1;
}
return sum;
}
Expected exit code: 15
PBXB64.exe rust-test_rust_while.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
10. Exclusive and inclusive range ends
Step-by-step flow
- 0..5 contains 0, 1, 2, 3 and 4, summing to 10.
- 1..=3 includes its end and adds 1 + 2 + 3 = 6.
- The combined total reaches 16.
fn main() -> i64 {
let mut total: i64 = 0;
for i in 0..5 {
total += i;
}
for j in 1..=3 {
total += j;
}
return total;
}
Expected exit code: 16
PBXB64.exe rust-test_rust_for_ranges.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
11. Array elements, list and repeated initialization
Step-by-step flow
- values receives 10 and 20 at the used indices 0 and 1.
- listed contains 30 and 40; [5i64; 2] creates two elements equal to 5.
- Other uninitialized values elements are not read. The used values sum to 110.
fn main() -> i64 {
let mut values: [i64; 4];
values[0] = 10;
values[1] = 20;
let listed = [30i64, 40i64];
let repeated = [5i64; 2];
return values[0] + values[1] + listed[0] + listed[1] + repeated[0] + repeated[1];
}
Expected exit code: 110
PBXB64.exe rust-test_rust_arrays.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
12. A struct with named fields
Step-by-step flow
- Point declares x and y as i64 fields.
- Both fields of local instance p are assigned before reading.
- p.x + p.y is 20 + 22 = 42; this is a PBXB64 Rust-input example.
struct Point {
x: i64,
y: i64,
}
fn main() -> i64 {
let p: Point;
p.x = 20;
p.y = 22;
return p.x + p.y;
}
Expected exit code: 42
PBXB64.exe rust-test_rust_named_struct.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
Larger example: Use a function, loops and match together
Step-by-step flow
- helper changes its parameter through compound assignments; for 15, b ends at 0 after %= 3.
- loop increases i to 3 and while then increases it to 5. The match branch for x = 15 combines the prepared values.
- For i64, !15 is -16, 15 << 1 is 30 and 15 >> 1 is 7. 0 - 5 - 16 + 30 + 7 + 5 gives exit code 21.
// Rust-subset lexer/parser coverage fixture.
fn helper(a: i64) -> i64 {
let mut b: i64 = a;
b += 1;
b -= 1;
b *= 2;
b /= 2;
b %= 3;
return b;
}
fn main() -> i64 {
let mut x: i64 = 15;
let neg: i64 = -5;
let not_x: i64 = !x;
let shl_v: i64 = x << 1;
let shr_v: i64 = x >> 1;
let mut i: i64 = 0;
loop {
if i >= 3 {
break;
}
i += 1;
continue;
}
while i < 5 {
i += 1;
}
if x > 10 {
match x {
15 => { return helper(x) + neg + not_x + shl_v + shr_v + i; }
_ => { return 0; }
}
}
return 99;
}
Expected exit code: 21
PBXB64.exe rust-test_rust_full.rs -O2 -o example.exe .\example.exe $LASTEXITCODE
Expected-result contract from tests/validate.toml. This website expansion adds no new native compiler verification. Execution with the qualified final compiler remains pending.
All sources for this tutorial
Each example has its own folder; the README lists compiler commands. Included support files retain matching paths.
Download all 13 examples as ZIPBridging
Cross-language calls require a matching extern and ABI contract. The B64/C case illustrates that agreement; Rust names or complex Rust types cannot be inferred automatically from it.
Explore complete source pairs