Define and call a function
#-> introduces the return type. The final expression without a semicolon is returned; return can exit early. () is the unit type.
Fragment
Function declaration, then a call fragment inside main.
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
Rust docs Functions ↗Closures ↗
-> introduces the return type. The final expression without a semicolon is returned; return can exit early. () is the unit type.
Fragment
Function declaration, then a call fragment inside main.
main is the entry point. It can return (), or a compatible Result: Ok(()) represents successful completion in the I/O examples.
Fragment
Complete main.rs.
struct holds fields; impl holds methods. &mut self borrows the instance for mutation. derive(Default) generates a default value when fields support it. LinePrinter is exercise-defined.
Fragment
Custom declarations; use inside a function.
An associated function without a self parameter is called through the type, such as Token::new(24). new is a naming convention, not a keyword. Self names the implementing type; Self { value } constructs its fields using shorthand.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
Closures infer parameter/capture behavior from use. Fn, FnMut and FnOnce describe how they can be called. move captures by value; capturing a reference by value still holds a reference, not a clone of its referent.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
Ordinary Rust functions have no default arguments or general variadic parameter list. Slices, Option parameters, builders or macros can express the intended API. This example accepts a variable-length slice.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.