Rust · 936 bytes Raw Blame History
1 use std::collections::HashMap;
2
3 // A simple struct
4 #[derive(Debug)]
5 struct Person {
6 name: String,
7 age: u32,
8 }
9
10 impl Person {
11 fn new(name: &str, age: u32) -> Self {
12 Person {
13 name: name.to_string(),
14 age,
15 }
16 }
17
18 fn greet(&self) {
19 println!("Hello, I'm {} and I'm {} years old", self.name, self.age);
20 }
21 }
22
23 fn main() {
24 // Create a person
25 let person = Person::new("Alice", 30);
26 person.greet();
27
28 // Use a HashMap
29 let mut scores = HashMap::new();
30 scores.insert("Blue", 10);
31 scores.insert("Red", 50);
32
33 // Pattern matching
34 match scores.get("Blue") {
35 Some(score) => println!("Blue team score: {}", score),
36 None => println!("No score found"),
37 }
38
39 // Iterate with closure
40 let numbers = vec![1, 2, 3, 4, 5];
41 let doubled: Vec<i32> = numbers.iter().map(|x| x * 2).collect();
42 println!("Doubled: {:?}", doubled);
43 }