| Rust is a systems programming language focused on memory safety, performance, and concurrency, invented by Graydon Hoare at Mozilla Research and developed since roughly 2006. Its syntax is expressive yet similar to C++ and influenced by functional languages, emphasizing explicit memory management with ownership and borrowing to prevent data races and dangling pointers without garbage collection. Rust excels in areas requiring performance and reliability, such as operating systems, embedded systems, game development, and web servers. Rust is available on a wide range of platforms, including: - Operating Systems: Windows, macOS, Linux (various distributions), BSD variants, and others. It's highly portable.
- Embedded Systems: Rust is increasingly popular for embedded development, targeting microcontrollers and other resource-constrained devices. Specific support depends on the microcontroller architecture (e.g., ARM Cortex-M, RISC-V).
- WebAssembly (Wasm): Rust can compile to WebAssembly, allowing it to run in web browsers.
- Cloud Platforms: Rust is supported on all major cloud providers (AWS, Azure, Google Cloud) and often used for serverless functions and other backend services.
Essentially, if you have a reasonably modern operating system or a supported embedded environment, there's a good chance you can use Rust. The exact level of support (e.g., features, libraries) can vary slightly depending on the specific platform. Here are a few simple Rust code examples, demonstrating different aspects of the language: 1. Hello, world! This is the quintessential first program in any language: fn main()
{
println!("Hello, world!");
}
This code defines a main function, which is the entry point of the program. println! is a macro that prints text to the console. 2. Variables and Data Types: This example shows how to declare variables with different data types: fn main()
{
let x: i32 = 10; // Integer
let y: f64 = 3.14; // Floating-point number
let z: bool = true; // Boolean
let name: &str = "Rust"; // String slice (immutable)
println!("Integer: {}, Float: {}, Boolean: {}, String: {}", x, y, z, name);
}
Rust is statically typed, meaning you must specify the data type of a variable. &str is a string slice, which is a reference to a string literal (immutable). 3. Functions: This demonstrates a simple function that adds two numbers: fn add(a: i32, b: i32) -> i32 { a + b }
fn main()
{
let sum = add(5, 3);
println!("Sum: {}", sum);
}
The add function takes two i32 arguments and returns an i32. The -> i32 specifies the return type. Note that Rust can infer the return type if it's obvious, but it's good practice to be explicit. 4. Control Flow (if/else): This example shows a simple if/else statement: fn main() {
let number = 7;
if number < 5 {
println!("Number is less than 5");
} else if number == 5 {
println!("Number is 5");
} else {
println!("Number is greater than 5");
}
}
This code checks the value of number and prints a different message based on the condition. These are just basic examples. Rust is a powerful language with many more features, including ownership, borrowing, generics, and more. These examples provide a starting point to explore the language further. Remember to compile and run these examples using a Rust compiler (like rustc). |