Rust Functions and Return Values
Give a calculation a typed boundary.
How it works
Function signatures specify parameter and result types, helping callers understand the interface. The final expression can return a value without an explicit return keyword. Use a function for a coherent operation and keep its error policy visible. A count calculation that might fail should return Option or Result rather than silently inventing a default.
Run the code
Reviewed with Rust 1.97.0, edition 2024, using the standard library only. Save the complete download as lesson-name.rs in a separate learning folder, compile with rustc --edition=2024 lesson-name.rs and run the resulting program (lesson-name.exe on Windows). Use the actual lesson filename. The final project includes complete Cargo package files; run cargo test --offline and cargo run --offline from that separate package folder. There is no Rust browser runner; PHP, Apache and MySQL are not needed.
This complete example was executed in the stated local review environment.
Example
fn main() {
fn packets(trays: u32) -> Option<u32> { trays.checked_mul(3) }
assert_eq!(packets(4), Some(12));
assert_eq!(packets(u32::MAX), None);
println!("Four trays give 12 packets; overflowing count refused");
}
Expected result
Four trays give 12 packets; overflowing count refused
The helper uses checked multiplication to make an out-of-range result visible to the caller. The two assertions test a normal input and a numerical boundary. Later Result examples attach an explanation to failure when a caller needs to distinguish format, range and business-rule problems.
Primary technical reference: Rust documentation.