liveBook · Manning

Num crate

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

The num crate is a Rust library providing numeric types and operations beyond Rust's built-in numeric types.

The num crate extends Rust’s comparatively small standard library with numeric types for rational numbers, complex numbers, arbitrary-size integers, arbitrary-precision floating-point values, and fixed-point decimals. Cargo downloads it from crates.io as a package dependency. section 2.3.4

Adding complex numbers

Create a project, add num = "0.4" to Cargo.toml, and import Complex with use num::complex::Complex. Complex values can use a literal with re and im fields or Complex::new; ordinary addition then combines the real and imaginary parts. section 2.3.4

Rendering the Mandelbrot set

A second project adds num with cargo add num and uses Complex values while calculating and rendering the Mandelbrot set in a terminal. The project turns numeric types into a visible fractal image. section 2.7

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
 }

Inspect the complete complex-number program, including imports, two construction forms, addition, and formatted output. section 2.3.4

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

How deep it goes
Demonstrates dependency setup and complex arithmetic, then applies the type in a Mandelbrot renderer without surveying the crate comprehensively.
Examples
Uses complex-number addition and a terminal-rendered Mandelbrot set as concrete applications.
What it assumes
Assumes basic Rust project and Cargo usage while introducing complex-number notation and construction in context.
What you can do after
Apply it via Cargo to perform complex arithmetic and build a Mandelbrot renderer.
How it connects
Connects Rust, Cargo, crates.io, complex numbers, and the Mandelbrot set.

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.