liveBook · Manning

Const qualifier

As explained in Rust in Action: Systems programming concepts and techniques

A const declaration defines a compile-time constant whose value cannot be changed.

A `const` declaration defines a compile-time constant: a value that cannot be changed. In global-state code, it contrasts with `static mut`, which represents mutable global data. section 3.4.1

Use the qualifier to mark values that never change:

  • Choose `const` for an unchanging value. section 3.4.1
  • Choose `static mut` when a global variable must be modified; such access appears inside `unsafe` code. section 3.4.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines const as an unchanging compile-time value and contrasts it with mutable global state, without developing its declaration details.
Examples
Contrasts an unchanging const value with the mutable ERROR global used to propagate file-reading errors.
What you can do after
Evaluate whether a global value should remain constant or be mutable in Rust error-handling code.
How it connects
Connects const with global variables, static mut, unsafe code, and error handling.

Also explained in

Compare how 7 books explain const qualifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.