Comparison: Deref Coercions
String to Str
OCaml โ one type, no conversion:
๐ช Show OCaml equivalent
let greet name = Printf.printf "Hello, %s!\n" name
let () = greet "world" (* just string *)
Rust โ auto deref:
fn greet(name: &str) { println!("Hello, {}!", name); }
let owned = String::from("world");
greet(&owned); // &String auto-derefs to &str
Explicit vs Implicit
OCaml โ must convert explicitly:
๐ช Show OCaml equivalent
let s = "hello" in
let bytes = Bytes.of_string s in (* explicit *)
Rust โ deref handles it:
let s = String::from("hello");
let r: &str = &s; // implicit via Deref
Smart Pointer Access
OCaml โ modules/abstract types need accessors:
๐ช Show OCaml equivalent
let v = Wrapper.get w (* explicit unwrap *)
Rust โ transparent via Deref:
let b = Box::new(42);
println!("{}", *b); // explicit deref
println!("{}", b); // auto-deref for Display