liveBook · Manning

Runtime logic error

As explained in Code Like a Pro in Rust

A logic error is a defect in a program's intended computation that can produce incorrect results even when the program compiles and runs.

A runtime logic error produces an incorrect computation even when Rust compiles and the program runs. The compiler cannot determine whether the implementation matches the intended operation. section 6.1

Reduce the risk of logic errors by making computation easier to test:

  • Break computation into small functions. section 6.1
  • Prefer stateless and deterministic functions. section 6.1
  • Use unit tests to validate intended results, and return Result or Option for expected failure and absence cases. section 6.1

Wrong operator, valid program

The compiler may accept a function that uses division where the intended computation requires multiplication. Both operations are valid Rust; only testing or review can expose the incorrect result. section 6.1

How Code Like a Pro in Rust covers it

How deep it goes
Defines the compiler boundary and gives a division-versus-multiplication example, without developing a broader error taxonomy.
Examples
Uses mistaken division instead of multiplication and recommends small, stateless, deterministic functions as testable units.
What you can do after
Diagnose logic errors with unit tests and reduce them by structuring computation into small, deterministic functions.
How it connects
Ties logic errors to unit testing, deterministic functions, Result, Option, and early error propagation with the question-mark operator.

Also explained in

Compare how 3 books explain runtime logic error →

Related concepts

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