Rust is a modern, systems-level programming language, developed by Mozilla in 2010. It is desingned with a focus on safety, performance, and concurrency.
Rust syntax:
fn add(x: i32, y:i32) -> i32 {
x + y
}
|
# This is a single-line comment
"""
This is
a multi-line
comment
"""
|
let a: i32 = 10;
let b: i64 = 1000;
let c = 5; // Rust can infer type i32 here
|
let pi: f64 = 3.14159;
let e = 2.71828; // Rust infers type f64 here
|
let is_rust_cool: bool = true;
let is_python_better = false; // Rust infers type bool here
|
let letter_a: char = 'a';
let unicode_heart = '❤'; // Rust infers type char here
|
let greeting: String = String::from("Hello, world!");
let language = "Rust".to_string(); // Rust infers type String here
|
let numbers: [i32; 3] = [1, 2, 3];
let vowels = ['a', 'e', 'i', 'o', 'u']; // Rust infers type and length here
|
let person: (String, i32, bool) = ("Alice".to_string(), 30, true);
let point = (10, 20); // Rust infers types here
|
struct Person {
name: String,
age: i32,
}
let alice = Person {
name: "Alice".to_string(),
age: 30,
};
struct Point {
x: f64,
y: f64,
}
let origin = Point { x: 0.0, y: 0.0 };
|
enum Direction {
Up,
Down,
Left,
Right,
}
let player_direction = Direction::Up;
|
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert("Alice", 42);
scores.insert("Bob", 56);
// Accessing values
if let Some(score) = scores.get("Alice") {
println!("Alice's score: {}", score);
}
// Updating values
scores.insert("Alice", 50);
// Iterating over key-value pairs
for (name, score) in &scores {
println!("{} has a score of {}", name, score);
}
|
We also can use it with specific data types:
let mut inventory: HashMap<String, i32> = HashMap::new();
inventory.insert("Apples".to_string(), 10);
inventory.insert("Bananas".to_string(), 5);
// Accessing values
if let Some(quantity) = inventory.get("Apples") {
println!("Quantity of Apples: {}", quantity);
}
|
let mut numbers: Vec<i32> = Vec::new();
numbers.push(1);
|
let optional_value: Option<i32> = Some(42);
let no_value: Option<i32> = None;
|
let result: Result<i32, String> = Ok(42);
|
- Memory Safety: the concept of ownership system ensures memory safety without the need of a garbage collector, making it ideal for system programming.
- Concurrency: with features like ownership and borrowing, that prevents data races, it makes the concurrent programming safe and manageable.
- Performance: the language offers the close-to-the-metal performance, making it suitable for performance-critical applications.
- Ecosystem: there is a growing ecosystem of libraries and a package manager called Cargo.
- Community: the community is known for the friendliness and willingness to help newcomers, making it a welcoming environment for learning.
- Learning Curve: Rust’s strict ownership and borrowing system can be challenging for begginers and may require some time to grasp.
- Verbosity: the syntax is clear and expressive, it can be more verbose than some other languages, leading to longer code.
fn main(){
println!("Hello, World!");
}
|
The minimal examples is the basic hello world.
fn main(){
println!("Hello, World!");
}
|