liveBook · Manning

Literal

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

A literal form is Rust syntax that directly initializes or denotes a value according to the type's literal representation.

A literal is syntax for writing a value directly. In `let a = 10;`, `10` supplies the value for a variable binding. Numeric literals can carry a type suffix: `30i32` and `30_i32` both specify `i32`; the underscore improves readability without changing the value. section 2.2.1

Integer literals can use binary (`0b11`), octal (`0o36`), or hexadecimal (`0x12C`) notation. These values are 3, 30, and 300 in decimal. The notation changes how you write the integer, not the value it represents. section 2.3.2

Listing 2.4 Using base 2, base 8, and base 16 numeric literals
 fn main() {
   let three = 0b11;             #1
   let thirty = 0o36;            #2
   let three_hundred = 0x12C;    #3
 
   println!("base 10: {} {} {}", three, thirty, three_hundred);
   println!("base 2:  {:b} {:b} {:b}", three, thirty, three_hundred);
   println!("base 8:  {:o} {:o} {:o}", three, thirty, three_hundred);
   println!("base 16: {:x} {:x} {:x}", three, thirty, three_hundred);
 }

Compare the three integer-literal prefixes with the formatting options that print each value in different bases. section 2.3.2

A value with named fields can also be initialized directly. `Complex { re: 2.1, im: -1.2 }` assigns its real and imaginary fields. `Complex::new(11.1, 22.2)` is an alternative supplied by the type’s library; `new()` is a convention, not literal syntax built into Rust. section 2.3.4

Listing 2.6 Calculating values with complex numbers
 use num::complex::Complex;                     #1
 
 fn main() {
   let a = Complex { re: 2.1, im: -1.2 };       #2
   let b = Complex::new(11.1, 22.2);            #3
   let result = a + b;
 
   println!("{} + {}i", result.re, result.im)   #4
 }

See field-based initialization and `Complex::new()` side by side in a program that adds two complex numbers. section 2.3.4

Quotes distinguish another pair of forms: `b'a'` creates one `u8` byte, while `b"a"` creates a one-byte slice. section 10.6

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

How deep it goes
Explains numeric suffixes, separators, integer bases, and field initialization through examples rather than a complete literal-syntax reference.
Examples
Compares binary, octal, and hexadecimal integers, then initializes complex numbers with fields and `Complex::new()`.
What it assumes
Introduces variable bindings and explicit types before using numeric literals; supplies Cargo steps for the complex-number example.
What you can do after
Apply it via the `num` crate to initialize a complex value with field syntax or `Complex::new()`.
How it connects
Connects literals to type annotations, integer representation, struct fields, and conventional `new()` methods.

Also explained in

Compare how 6 books explain literal →

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.