C and Rust, side by side
return, a nested ternary and an int that stands in for a boolean.#include <stdio.h>
static int clamp(int v, int lo, int hi) {
return v < lo ? lo : v > hi ? hi : v; /* nested ternary */
}
static int sign(int x) {
if (x > 0) return 1; /* `if` is a statement */
if (x < 0) return -1;
return 0;
}
static int is_even(int x) { return !(x % 2); } /* an int stands in for bool */
int main(void) {
printf("%d %d %d\n", clamp(-5, 0, 10), clamp(7, 0, 10), clamp(42, 0, 10));
printf("%d %d %d\n", sign(-3), sign(0), sign(9));
if (is_even(4)) printf("4 is even\n");
return 0;
}
clamp is one if expression, sign mixes an early return with a tail if, and is_even ends with its comparison.fn clamp(v: i32, lo: i32, hi: i32) -> i32 {
if v < lo { lo } else if v > hi { hi } else { v } // the whole `if` is the value
}
fn sign(x: i32) -> i32 {
if x > 0 {
return 1; // an early `return` still exists
}
if x < 0 { -1 } else { 0 }
}
fn is_even(x: i32) -> bool {
x % 2 == 0 // tail expression, no semicolon: the return value
}
fn main() {
println!("{} {} {}", clamp(-5, 0, 10), clamp(7, 0, 10), clamp(42, 0, 10));
println!("{} {} {}", sign(-3), sign(0), sign(9));
if is_even(4) {
println!("4 is even");
}
}
| C | Rust |
|---|---|
A value comes back only through return. | A body's last expression without ; is the return value. |
cond ? a : b, because if is a statement. | if cond { a } else { b } is an expression; both branches share one type. |
| Any non-zero integer is true in a condition. | A condition must be a bool; if n with an integer is E0308. |
Understand it
C separates statements from expressions: if is a statement, so choosing a value needs ?:, and a function hands back a value only through return. In Rust almost everything is an expression. A block { ... } evaluates to its last expression, and a function body is a block, so fn is_even(x: i32) -> bool can end with the comparison itself. return still exists for early exits.
if is an expression too, which is why Rust has no ternary operator: if v < lo { lo } else { v } is the value. Both branches must have the same type, and the condition must be a bool. C's habit of testing an integer, if (flags & MASK), is a type error; write flags & MASK != 0. Unlike C, & binds tighter than != in Rust, so that expression means what it says.
The semicolon is significant. x + 1 at the end of a block is the block's value; x + 1; is a statement that discards it, and the block then evaluates to (), the unit type. In a function that promises u32, that is error E0308, and rustc's help points at the semicolon to remove. Read semicolons this way and the rule becomes mechanical.
Parameters need full types, and arguments are never converted to fit them: passing an i32 where a u32 is expected does not compile. There are no prototypes and no forward declarations; functions in a module can be defined in any order.
The C habit, written in Rust
This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
fn days(leap: bool) -> u32 {
if leap { 29 } else { 28 };
}
fn main() {
println!("{}", days(true));
}
Try it
Port the commented C functions: is_leap applies the Gregorian rule (divisible by 4, except centuries, except every 400th year) and days_in_february returns 29 or 28.
What success looks like: 2024 and 2000 are leap years, 1900 and 2023 are not, and February has 29 days only in leap years.
Check your understanding
Why does rustc reject fn days(leap: bool) -> u32 { if leap { 29 } else { 28 }; }?
Reveal the answer
ifcannot produce a value; use amatch- The function is missing
return 29and28arei32, notu32- The semicolon makes the
ifa statement, so the body evaluates to()
Remove the semicolon and the if expression becomes the body's value. The literals take the type u32 from the signature.
Go deeper
Common misconception: “A Rust function without return hands back whatever is left over, like falling off the end of a C function.”
Using the result of a non-void C function that falls off its end is undefined. In Rust the end of the body is the return value; if that value is () but the signature promises another type, the program does not compile.
- Find the tail: In each block, locate the last expression and check whether a semicolon follows it.
- Type both branches: For every
ifused as a value, confirm that both branches produce the same type. - Replace integer tests: Rewrite C conditions such as
if (n)as explicit comparisons such asn != 0.
Transfer challenge: Write fn sign(x: i32) -> i32 as a single if/else if/else expression, then add a semicolon after it and read rustc's help line.
Connections
- Integer Overflow and Casts: Defined overflow policy and explicit conversions
- From for(;;) to Ranges: Ranges, iterators and loop with a value

Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Functions Are Expressions (courseId "rust-for-c-developers", lessonId "c-functions-are-expressions") Link: https://crabrix.com/learn/rust-for-c-developers/c-functions-are-expressions/ Goal: A block's value is its final expression without a semicolon; adding the semicolon turns it into a statement, and the block evaluates to (). Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-functions-are-expressions) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.