Rust Syntax Fundamentals
Rust Syntax Fundamentals
Rust syntax is designed to be both expressive and safe, emphasizing clarity through its unique combination of static typing, memory safety, and idiomatic conventions. This article explores the core syntax elements that form the foundation of Rust programming.
Basic Syntax
Rust syntax is built on a set of rules that ensure consistency and readability. Key elements include:
- Semicolons: End statements to avoid implicit newlines.
- Indentation: Use four spaces or a tab for code blocks.
- Braces: Enclose blocks (e.g., functions, loops) to define scope.
- Comments:
//for single-line comments,/* ... */for block comments.
Data Types
Rust uses a statically typed system with a rich set of data types.
Primitive Types
- i8, i16, i32, i64: Signed integers.
- u8, u16, u32, u64: Unsigned integers.
- f32, f64: Floating-point numbers.
- bool:
trueorfalse.
Compound Types
- Enums: Define custom types with variants.
- Structs: User-defined types with fields.
- Tuples: Collections of values of any type.
- Arrays: Fixed-size collections.
Control Structures
Rust provides a clear and safe syntax for control flow.
Conditional Statements
ifstatements useelse iffor multiple conditions.matchexpressions match values against patterns.
Loops
forloops iterate over ranges or collections.whileloops execute repeatedly based on a condition.
Functions
Functions are defined with fn and include parameters, return types, and body code.
Function Definitions
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
&stris a reference to a string slice.-> Stringspecifies the return type.
Modules
Modules organize code into logical groups.
Module Structure
mod my_module {
pub fn greet() { /* ... */ }
}
pubmakes the function accessible outside the module.moddefines a new module.
Error Handling
Rust uses panic! and Result to manage errors safely.
Panic Handling
fn divide(a: i32, b: i32) -> i32 {
if b == 0 {
panic!("Division by zero");
}
a / b
}
panic!terminates the program with a panic message.
Result Type
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
if b == 0 {
return Err("Division by zero");
}
Ok(a / b)
}
Result<T, E>encapsulates a value and an error.Okindicates success,Errindicates failure.
Syntax Rules
Rust syntax enforces strict rules to prevent common errors.
Type Safety
- Explicit type annotations are required for non-primitive types.
- Type inference is supported for simple cases.
Ownership and Borrowing
- Rust's ownership model ensures safe memory management.
- Borrowing uses
&for references and&mutfor mutable references.
Best Practices
- Use
constfor compile-time values. - Prefer
letovervarfor readability. - Avoid
pubunless necessary; usepub(crate)for internal modules.
Conclusion
Rust syntax is designed to balance expressiveness with safety, leveraging static typing, ownership, and a clean syntax. Understanding these fundamentals is crucial for writing efficient and maintainable Rust code.