When learning Rust after writing TypeScript, you frequently run into the question “what is the Rust equivalent of this?” This page is a 1-to-1 reference mapping TypeScript concepts to their Rust counterparts.
String is an owned string allocated on the heap. &str is a read-only slice that points to a string that already exists somewhere. For function parameters, accepting &str is more flexible; use String only when ownership is required.
fngreet(name:&str) { // &str: borrow and read without owning
Types that implement the Copy trait (i32, bool, f64, char, etc.) are automatically copied on assignment, leaving the original intact. The Clone trait requires an explicit .clone() call and is used for types with heap data like String or Vec.
leta:i32=5;
letb=a; // Copy: a is still valid
lets1=String::from("hello");
lets2=s1.clone(); // Clone: s1 is still valid
// let s2 = s1; // this would move s1, making it unusable
Box<T> is used to place a type of unknown size on the heap or to simply move ownership to the heap. Rc<T> enables multiple owners via reference counting, but is single-threaded only. Arc<T> is the thread-safe version of Rc<T>, used to share data across threads.
// impl Trait: type is fixed at compile time (fast)
fnmake_adder(x:i32) ->implFn(i32) ->i32 {
move|y|x+y
}
// dyn Trait: type determined at runtime (flexible)
fnget_shape(kind:&str) ->Box<dynShape> {
matchkind {
"circle"=>Box::new(Circle::new()),
"rect"=>Box::new(Rectangle::new()),
_=>panic!("unknown shape"),
}
}
Come back to this page whenever you get stuck on a concept. It’s here to help you quickly find the Rust equivalent of something familiar from TypeScript and keep your momentum going.