Introduction
A .rs file holds source code in Rust,
a compiled systems programming language. The Rust compiler, rustc, usually
run through the Cargo build tool, turns a crate (a tree of .rs files) into a
program or library.
A crate’s root file is normally src/main.rs for a program or
src/lib.rs for a library, with a Cargo.toml file in the
folder above that names the crate and its dependencies.
Structure
A Rust source file is plain text and must be UTF-8. It holds items: functions,
structs, enums, traits, impl blocks and modules. A line such as
mod parser; tells the compiler to read the module from
parser.rs or parser/mod.rs next to the current file, so the
files of a crate form a tree that starts at its root file. Comments starting
/// and //! are documentation, which the
rustdoc tool turns into web pages.
use std::env;
/// Prints a greeting for each name given on the command line.
fn main() {
for name in env::args().skip(1) {
println!("Hello, {name}");
}
}
Links
Software
- Rust, with
rustcand Cargo (Windows, macOS, Linux) - rust-analyzer, the language server most editors use for Rust
- Visual Studio Code (Windows, macOS, Linux)
- RustRover (Windows, macOS, Linux)
- Vim (Windows, macOS, Linux)
Developers
- syn, Rust, parses Rust source into a syntax tree
- tree-sitter-rust, Rust grammar for the Tree-sitter parser used by editors
Reference
- The Rust Reference
- The Rust Programming Language, the official book