use std::collections::HashMap;
use std::sync::Arc;
use std::thread;
#[derive(Debug, Clone)]
struct Point<T> {
x: T,
y: T,
}
trait Area {
fn area(&self) -> f64;
}
struct Circle {
radius: f64,
}
impl Area for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
fn divide(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err("Division by zero".to_string())
} else {
Ok(a / b)
}
}
fn describe_number(n: i32) -> &'static str {
match n {
0 => "zero",
1..=9 => "single digit",
n if n < 0 => "negative",
_ => "large number",
}
}
fn main() {
let s1 = String::from("hello");
let s2 = s1;
let mut vec = vec![1, 2, 3, 4, 5];
let first = &vec[0];
println!("First: {}", first);
vec.push(6);
let shared_data = Arc::new(vec![1, 2, 3]);
let mut handles = vec![];
for _ in 0..10 {
let data = Arc::clone(&shared_data);
handles.push(thread::spawn(move || {
println!("Data length: {}", data.len());
}));
}
for handle in handles {
handle.join().unwrap();
}
let mut map: HashMap<String, i32> = HashMap::new();
map.insert("key".to_string(), 42);
match map.get("key") {
Some(value) => println!("Found: {}", value),
None => println!("Not found"),
}
let result = divide(10.0, 2.0)?;
println!("Result: {}", result);
let sum: i32 = (1..=100)
.filter(|x| x % 2 == 0)
.map(|x| x * x)
.sum();
println!("Sum of even squares: {}", sum);
}