Introduction to Rust
Rust is a systems programming language that emphasizes safety and performance. It's known for its strong type system, memory safety, and zero-cost evaluation.
// Example Rust code
fn main() {
println!("Hello, world!");
}
Types and Variables
Rust has several basic types including integers, floating point numbers, booleans, and strings.
- i32: 32-bit integer
- f64: 64-bit floating-point number
- bool: true/false values
- String: immutable string slice
Flow Control
Rust uses control flow statements such as if, match, and loop constructs to manage program execution.
let age = 25;
if age >= 18 {
println!("Welcome!");
} else {
println!("Sorry, you're too young.");
}
Functions and Modules
Rust allows defining functions and modules to organize code logically and improve readability.
// Function definition
fn greet(name: &str) -> String {
return format!("Hello, {}!", name);
}
// Module declaration
mod math {
pub fn add(a: i32, b: i32) -> i32 {
return a + b;
}
}
Conclusion
Rust provides a modern and safe approach to systems programming, making it an excellent choice for developers who want to create fast and reliable software.
// Final statement
println!("Rust is a powerful language for building secure applications.");