How to do it...

In just a few steps, we will be working with different modules:

  1. First, we are going to implement the rust-pilib crate. As a simple example, it estimates the constant pi using the Monte Carlo method. This method is somewhat similar to throwing darts at a dartboard and counting the hits. Read more on Wikipedia (https://en.wikipedia.org/wiki/Monte_Carlo_method). Add to the tests submodule this snippet:
use rand::prelude::*;pub fn monte_carlo_pi(iterations: usize) -> f32 {    let mut inside_circle = 0;     for _ in 0..iterations {        // generate two random coordinates between 0 and 1        let x: f32 = random::<f32>();        let y: f32 = random::<f32>();                // calculate the circular distance from 0, 0        if x.powi(2) + y.powi(2) <= 1_f32 { // if it's ...

Get Rust Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.