Radiance vs Rust: Syntactic Differences
This document outlines the key syntactic differences between Radiance and Rust.
Type Declarations
Structs
| Feature |
Rust |
Radiance |
| Keyword |
struct |
record |
| Tuple structs |
struct Pair(i32, i32); |
record Pair(i32, i32); |
| Deriving traits |
#[derive(Eq)] |
record Point: Eq { ... } |
| Field alignment |
Not in syntax |
field: u32 align(8) |
| Default values |
Not in syntax |
field: bool = true |
// Rust
#[derive(Eq, PartialEq)]
struct Point {
x: i32,
y: i32,
}
// Radiance
record Point: Eq {
x: i32,
y: i32,
}
Enums
| Feature |
Rust |
Radiance |
| Keyword |
enum |
union |
| Variant syntax |
Same |
Same |
// Rust
enum Result {
Ok(u32),
Err { code: i32, msg: String },
}
// Radiance
union Result {
Ok(u32),
Err { code: i32, msg: *[u8] },
}
Pointer and Reference Types
| Type |
Rust |
Radiance |
| Immutable reference |
&T |
*T |
| Mutable reference |
&mut T |
*mut T |
| Immutable slice |
&[T] |
*[T] |
| Mutable slice |
&mut [T] |
*mut [T] |
| Raw pointer |
*const T / *mut T |
N/A (pointers are the default) |
// Rust
fn process(data: &[u8], buffer: &mut [u8]) { }
// Radiance
fn process(data: *[u8], buffer: *mut [u8]) { }
Optional Types
| Feature |
Rust |
Radiance |
| Optional type |
Option<T> |
?T |
| None value |
None |
nil |
| Some value |
Some(x) |
Just the value x |
// Rust
fn find(id: u32) -> Option<User> {
if found { Some(user) } else { None }
}
// Radiance
fn find(id: u32) -> ?User {
if found { user } else { nil }
}
Logical Operators
| Operation |
Rust |
Radiance |
| Logical AND |
&& |
and |
| Logical OR |
\|\| |
or |
| Logical NOT |
! |
not |
// Rust
if a && b || !c { }
// Radiance
if a and b or not c { }
Attributes and Annotations
| Purpose |
Rust |
Radiance |
| Test function |
#[test] |
@test |
| Entry point |
fn main() |
@default fn main() |
| External function |
extern "C" fn |
extern fn or @extern fn |
| Derive traits |
#[derive(...)] |
Inline: record T: Trait { } |
| Public visibility |
pub fn |
pub fn (same) |
// Rust
#[test]
fn my_test() { }
fn main() { }
// Radiance
@test fn myTest() -> bool { return true; }
@default fn main() -> u32 { return 0; }
Match Expressions
| Feature |
Rust |
Radiance |
| Keyword |
match |
match (same) |
| Arm syntax |
pattern => expr, |
case pattern => expr, |
| Default arm |
_ => expr |
else => expr |
| Guard syntax |
if cond |
if cond (same) |
| Multiple patterns |
1 \| 2 \| 3 |
1, 2, 3 |
// Rust
match value {
1 | 2 | 3 => true,
x if x > 10 => true,
_ => false,
}
// Radiance
match value {
case 1, 2, 3 => true,
case x if x > 10 => true,
else => false,
}
Error Handling
| Feature |
Rust |
Radiance |
| Propagate error |
expr? |
try? expr or try! expr |
| Try with recovery |
N/A (use match) |
try expr catch { ... } |
| Throw error |
return Err(e) or Err(e)? |
throw Error |
| Panic |
panic!("msg") |
panic "msg" |
| Throws declaration |
-> Result<T, E> |
throws (Error) -> T |
// Rust
fn read_file() -> Result<String, io::Error> {
let content = fs::read_to_string(path)?;
Ok(content)
}
// Radiance
fn readFile() throws (IoError) -> *[u8] {
let content = try! fsRead(path);
return content;
}
// Rust
if let Some(value) = optional {
use(value);
}
let Some(x) = opt else { return; };
// Radiance
if let value = optional {
use(value);
}
let x = opt else { return; };
// With explicit pattern matching:
if let case Result::Ok(value) = result {
use(value);
}
Uninitialized Values
| Feature |
Rust |
Radiance |
| Uninitialized |
MaybeUninit::uninit() |
undefined |
// Rust
let mut buffer: MaybeUninit<[u8; 256]> = MaybeUninit::uninit();
// Radiance
let mut buffer: [u8; 256] = undefined;
Function Syntax
| Feature |
Rust |
Radiance |
| Return type |
-> T |
-> T (same) |
| No return |
implicit () |
implicit or explicit void |
| Return statement |
return x; or trailing x |
return x; |
Radiance requires explicit return statements; trailing expressions without return are not automatically returned.
// Rust
fn add(a: i32, b: i32) -> i32 {
a + b // implicit return
}
// Radiance
fn add(a: i32, b: i32) -> i32 {
return a + b; // explicit return required
}
Summary of Key Differences
- Keywords:
record vs struct, union vs enum
- Logical operators: Word-based (
and, or, not) vs symbolic (&&, ||, !)
- Pointer syntax:
*T instead of &T, *[T] instead of &[T]
- Optional types:
?T with nil instead of Option<T> with None
- Attributes:
@attr prefix instead of #[attr]
- Match arms:
case pattern => instead of pattern =>
- Error handling:
try!/try?/try catch and throws in signature
- No generics: Radiance lacks parametric polymorphism
- Explicit returns: No implicit return of trailing expressions
- Loop else: Radiance supports
for/while ... else constructs
- Indexed iteration: Built-in
for x, i in array syntax