From b428ce3d1055d786ad0ef27bb5a2a0507e6c9384 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 6 Mar 2019 11:43:38 -0500 Subject: [PATCH] Add doctest example --- mandelbrot/src/main.rs | 2 +- ranges/Cargo.toml | 7 +++++++ ranges/src/lib.rs | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 ranges/Cargo.toml create mode 100644 ranges/src/lib.rs diff --git a/mandelbrot/src/main.rs b/mandelbrot/src/main.rs index eaa89fb..8599ea4 100644 --- a/mandelbrot/src/main.rs +++ b/mandelbrot/src/main.rs @@ -80,7 +80,7 @@ fn test_parse_complex() { /// /// `bounds` is a pair giving the width and height of the image in pixels. /// `pixel` is a (column, row) pair indicating a particular pixel in that image. -/// The `upper_left` and `lower_right` parameters are pionts on the complex +/// The `upper_left` and `lower_right` parameters are points on the complex /// plain designating the area our image covers fn pixel_to_point( bounds: (usize, usize), diff --git a/ranges/Cargo.toml b/ranges/Cargo.toml new file mode 100644 index 0000000..4e34069 --- /dev/null +++ b/ranges/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ranges" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] diff --git a/ranges/src/lib.rs b/ranges/src/lib.rs new file mode 100644 index 0000000..c303619 --- /dev/null +++ b/ranges/src/lib.rs @@ -0,0 +1,23 @@ +use std::ops::Range; + +/// Return true if two ranges overlap +/// +/// assert_eq!(ranges::overlap(0..7, 3..10), true); +/// assert_eq!(ranges::overlap(1..5, 101..105), false); +/// +/// If either range is empty, they don't count as overlapping. +/// +/// assert_eq!(ranges::overlap(0..0, 0..10), false); +/// +pub fn overlap(r1: Range, r2: Range) -> bool { + r1.start < r1.end && r2.start < r2.end && + r1.start < r2.end && r2.start < r1.end +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +}