2.3 The main Function: Program Entry Point

As in C, a Rust program starts executing in the main function, typically found in a file named main.rs. Larger projects can have multiple .rs files and libraries, all compiled under one "crate."

2.3.1 A Simple Rust Program

fn main() {
    println!("Hello, world!");
}
  • fn defines a function.
  • main is Rust's entry point.
  • println! is a macro (not a function) that prints to standard output.

2.3.2 Comparison with C

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}
  • C's main returns an integer, usually 0 upon success.
  • Rust's main returns (), the unit type, by default.