Skip to content

Functions

TypeScript functions are defined with function or arrow syntax, with optional type annotations. In Rust, all functions are defined with fn, and all parameter and return types are required — the compiler never infers them across function boundaries. This keeps function signatures as explicit, readable contracts.

TypeScript uses : Type for parameters and : ReturnType after the closing paren. Rust uses the same : Type syntax for parameters and -> ReturnType after the param list.

// TypeScript
function add(a: number, b: number): number {
return a + b;
}
// Rust
fn add(a: i32, b: i32) -> i32 {
a + b // no semicolon — this is the return value
}

Notice the last line has no semicolon. In Rust, a block’s final expression (without ;) is implicitly returned. Adding a semicolon turns it into a statement that returns () (unit), which would be a type error here.

TypeScript
function add(a: number, b: number): number {
return a + b;
}
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(add(3, 4)); // 7
console.log(greet("Alice")); // Hello, Alice!
Rust
fn add(a: i32, b: i32) -> i32 {
a + b // last expression, no semicolon = return value
}
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
fn main() {
println!("{}", add(3, 4)); // 7
println!("{}", greet("Alice")); // Hello, Alice!
}
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
fn main() {
let sum = add(3, 4);
println!("3 + 4 = {sum}");
let msg = greet("Rustacean");
println!("{msg}");
}
In Rust, what does a missing semicolon on the last line of a function body mean?
Are return type annotations required on Rust functions?
Which Rust type represents 'no meaningful return value' (equivalent to void)?