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
