liveBook · Manning

Type safety

As explained in Refactoring to Rust

A strong static type system enforces compile-time type constraints and does not generally permit incompatible values to be used interchangeably.

Type safety uses a strong static type system to know every value’s type at compile time and reject incompatible uses, such as passing a string where a Boolean is required. Rust usually infers local variable types while requiring function input and output types, preserving safety without repetitive annotations. section 1.4.3

Typed data without repetitive declarations

A Java map requires explicit generic and local declarations: `HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();`. Rust infers the corresponding types from insertion: `let mut map = HashMap::new(); map.insert(1, vec![4, 10]);`. Explicit types remain available when needed: `HashMap<i32, Vec<i32>>`. section 1.4.3

Listing 1.4 Initializing a map of numbers to lists of numbers in Rust
let mut map = HashMap::new();
map.insert(1, vec![4, 10]);

Shows a compact Rust map whose key and value types are inferred from the inserted data. section 1.4.3

How Refactoring to Rust covers it

How deep it goes
Defines static checking and inference with map and function examples, while deferring some annotation cases to later treatment.
Examples
Contrasts Java map declarations with inferred and explicitly typed Rust map initialization.
What it assumes
Contrasts dynamically typed languages with static typing but does not require explicit local annotations in most Rust code.
What you can do after
Evaluate/understand how Rust rejects incompatible arguments while inferring most local types.
How it connects
Connects static typing with type inference, compile-time checking, maintainability, memory safety, and WebAssembly.

Also explained in

Compare how 8 books explain type safety →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.