Rust
programming languages that we learn
Contents
Rust Language
A System Programming Language
Rust is a modern, systems-level programming language, developed by Mozilla in 2010. It is desingned with a focus on safety, performance, and concurrency.
Basic Sintax
Rust syntax:
- variables
let mut my_var = 42;
- functions
fn add(x: i32, y:i32) -> i32 {
x + y
}
- comments
# This is a single-line comment
"""
This is
a multi-line
comment
"""
Examples on Assigning Variables
- Integers:
let a: i32 = 10;
let b: i64 = 1000;
let c = 5; // Rust can infer type i32 here
- Floating-point numbers:
let pi: f64 = 3.14159;
let e = 2.71828; // Rust infers type f64 here
- Booleans:
let is_rust_cool: bool = true;
let is_python_better = false; // Rust infers type bool here
- Characters:
let letter_a: char = 'a';
let unicode_heart = '❤'; // Rust infers type char here
- Strings:
let greeting: String = String::from("Hello, world!");
let language = "Rust".to_string(); // Rust infers type String here
- Arrays:
let numbers: [i32; 3] = [1, 2, 3];
let vowels = ['a', 'e', 'i', 'o', 'u']; // Rust infers type and length here
- Tuples:
let person: (String, i32, bool) = ("Alice".to_string(), 30, true);
let point = (10, 20); // Rust infers types here
- Custom Struct Variables:
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 };
- Enums:
enum Direction {
Up,
Down,
Left,
Right,
}
let player_direction = Direction::Up;
- HashMaps:
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);
}
- Vector:
let mut numbers: Vec<i32> = Vec::new();
numbers.push(1);
- Option Type:
let optional_value: Option<i32> = Some(42);
let no_value: Option<i32> = None;
- Result Type:
let result: Result<i32, String> = Ok(42);
Pros
- 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.
Cons
- 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.
Minimal program
fn main(){
println!("Hello, World!");
}
Basic hello world
The minimal examples is the basic hello world.
fn main(){
println!("Hello, World!");
}