🔧 Error Fixes
· 1 min read

Rust: Expected Type X, Found Y — How to Fix It


error[E0308]: mismatched types. expected `String`, found `&str`

Rust has strict types — you need to convert between them explicitly.

Fix 1: &str to String

// ❌ Expected String, got &str
let name: String = "Alice";

// ✅ Convert
let name: String = "Alice".to_string();
// or
let name: String = String::from("Alice");

Fix 2: String to &str

fn greet(name: &str) { println!("Hello, {}", name); }

let name = String::from("Alice");
// ❌ Expected &str, got String
greet(name);

// ✅ Borrow it
greet(&name);

Fix 3: Number type mismatches

let x: i32 = 42;
let y: f64 = x as f64;  // Explicit cast

Related: Rust: Cannot Borrow as Mutable — Borrow Checker Errors Explained