From 7f67fdf4fcf751dc6a42008f55567907edb7df2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Dantas?= Date: Wed, 20 Nov 2019 07:21:33 -0300 Subject: [PATCH] Remove main function from doctests (#752) * Remove main function from doctests * Add a explicit Ok return in doctest In order to use the ? operator, we need to make it clear the function returns a result instead of unit: https://doc.rust-lang.org/rustdoc/documentation-tests.html#using--in-doc-tests * Return unit error in order to extract the correct error type * Use ndarray::ShapeError error * Unwrap some Options * Remove empty main function from the prelude docs * Undo changes at the impl_constructors submodule * Remove needless main --- src/free_functions.rs | 60 ++++++++++++++++------------------------ src/impl_constructors.rs | 8 ++---- src/lib.rs | 15 ++-------- src/prelude.rs | 2 -- 4 files changed, 28 insertions(+), 57 deletions(-) diff --git a/src/free_functions.rs b/src/free_functions.rs index 8319865dd..ff7984ee6 100644 --- a/src/free_functions.rs +++ b/src/free_functions.rs @@ -16,23 +16,18 @@ use crate::{dimension, ArcArray1, ArcArray2}; /// three dimensions. /// /// ``` -/// extern crate ndarray; -/// /// use ndarray::array; +/// let a1 = array![1, 2, 3, 4]; /// -/// fn main() { -/// let a1 = array![1, 2, 3, 4]; -/// -/// let a2 = array![[1, 2], -/// [3, 4]]; +/// let a2 = array![[1, 2], +/// [3, 4]]; /// -/// let a3 = array![[[1, 2], [3, 4]], -/// [[5, 6], [7, 8]]]; +/// let a3 = array![[[1, 2], [3, 4]], +/// [[5, 6], [7, 8]]]; /// -/// assert_eq!(a1.shape(), &[4]); -/// assert_eq!(a2.shape(), &[2, 2]); -/// assert_eq!(a3.shape(), &[2, 2, 2]); -/// } +/// assert_eq!(a1.shape(), &[4]); +/// assert_eq!(a2.shape(), &[2, 2]); +/// assert_eq!(a3.shape(), &[2, 2, 2]); /// ``` /// /// This macro uses `vec![]`, and has the same ownership semantics; @@ -115,19 +110,14 @@ pub fn aview2>(xs: &[V]) -> ArrayView2<'_, A> { /// Create a one-dimensional read-write array view with elements borrowing `xs`. /// /// ``` -/// extern crate ndarray; -/// /// use ndarray::{aview_mut1, s}; -/// /// // Create an array view over some data, then slice it and modify it. -/// fn main() { -/// let mut data = [0; 1024]; -/// { -/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap(); -/// a.slice_mut(s![.., ..;3]).fill(5); -/// } -/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]); +/// let mut data = [0; 1024]; +/// { +/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap(); +/// a.slice_mut(s![.., ..;3]).fill(5); /// } +/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]); /// ``` pub fn aview_mut1(xs: &mut [A]) -> ArrayViewMut1<'_, A> { ArrayViewMut::from(xs) @@ -143,20 +133,18 @@ pub fn aview_mut1(xs: &mut [A]) -> ArrayViewMut1<'_, A> { /// ``` /// use ndarray::aview_mut2; /// -/// fn main() { -/// // The inner (nested) array must be of length 1 to 16, but the outer -/// // can be of any length. -/// let mut data = [[0.; 2]; 128]; -/// { -/// // Make a 128 x 2 mut array view then turn it into 2 x 128 -/// let mut a = aview_mut2(&mut data).reversed_axes(); -/// // Make the first row ones and second row minus ones. -/// a.row_mut(0).fill(1.); -/// a.row_mut(1).fill(-1.); -/// } -/// // look at the start of the result -/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]); +/// // The inner (nested) array must be of length 1 to 16, but the outer +/// // can be of any length. +/// let mut data = [[0.; 2]; 128]; +/// { +/// // Make a 128 x 2 mut array view then turn it into 2 x 128 +/// let mut a = aview_mut2(&mut data).reversed_axes(); +/// // Make the first row ones and second row minus ones. +/// a.row_mut(0).fill(1.); +/// a.row_mut(1).fill(-1.); /// } +/// // look at the start of the result +/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]); /// ``` pub fn aview_mut2>(xs: &mut [V]) -> ArrayViewMut2<'_, A> { let cols = V::len(); diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index cb39f8e9b..1f8b9708e 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -143,7 +143,7 @@ where /// # Some(()) /// # } /// # - /// # fn main() { example().unwrap() } + /// # example().unwrap(); /// ``` pub fn geomspace(start: A, end: A, n: usize) -> Option where @@ -485,8 +485,6 @@ where /// ### Examples /// /// ``` - /// extern crate ndarray; - /// /// use ndarray::{s, Array2}; /// /// // Example Task: Let's create a column shifted copy of a in b @@ -503,9 +501,7 @@ where /// b /// } /// - /// # fn main() { - /// # shift_by_two(&Array2::zeros((8, 8))); - /// # } + /// # shift_by_two(&Array2::zeros((8, 8))); /// ``` pub unsafe fn uninitialized(shape: Sh) -> Self where diff --git a/src/lib.rs b/src/lib.rs index 9f9d11bed..f9a208df1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -483,12 +483,9 @@ pub type Ixs = isize; /// [`.multi_slice_move()`]: type.ArrayViewMut.html#method.multi_slice_move /// /// ``` -/// extern crate ndarray; /// /// use ndarray::{arr2, arr3, s}; /// -/// fn main() { -/// /// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`. /// /// let a = arr3(&[[[ 1, 2, 3], // -- 2 rows \_ @@ -546,7 +543,6 @@ pub type Ixs = isize; /// [5, 7]]); /// assert_eq!(s0, i); /// assert_eq!(s1, j); -/// } /// ``` /// /// ## Subviews @@ -579,11 +575,9 @@ pub type Ixs = isize; /// [`.outer_iter_mut()`]: #method.outer_iter_mut /// /// ``` -/// extern crate ndarray; /// /// use ndarray::{arr3, aview1, aview2, s, Axis}; /// -/// # fn main() { /// /// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`. /// @@ -617,7 +611,6 @@ pub type Ixs = isize; /// // You can take multiple subviews at once (and slice at the same time) /// let double_sub = a.slice(s![1, .., 0]); /// assert_eq!(double_sub, aview1(&[7, 10])); -/// # } /// ``` /// /// ## Arithmetic Operations @@ -1063,7 +1056,6 @@ pub type Ixs = isize; /// ```rust /// use ndarray::{array, Array2}; /// -/// # fn main() -> Result<(), Box> { /// let ncols = 3; /// let mut data = Vec::new(); /// let mut nrows = 0; @@ -1075,8 +1067,7 @@ pub type Ixs = isize; /// } /// let arr = Array2::from_shape_vec((nrows, ncols), data)?; /// assert_eq!(arr, array![[0, 0, 0], [1, 1, 1]]); -/// # Ok(()) -/// # } +/// # Ok::<(), ndarray::ShapeError>(()) /// ``` /// /// If neither of these options works for you, and you really need to convert @@ -1089,7 +1080,6 @@ pub type Ixs = isize; /// ```rust /// use ndarray::{array, Array2, Array3}; /// -/// # fn main() -> Result<(), Box> { /// let nested: Vec> = vec![ /// array![[1, 2, 3], [4, 5, 6]], /// array![[7, 8, 9], [10, 11, 12]], @@ -1102,8 +1092,7 @@ pub type Ixs = isize; /// [[1, 2, 3], [4, 5, 6]], /// [[7, 8, 9], [10, 11, 12]], /// ]); -/// # Ok(()) -/// # } +/// # Ok::<(), ndarray::ShapeError>(()) /// ``` /// /// Note that this implementation assumes that the nested `Vec`s are all the diff --git a/src/prelude.rs b/src/prelude.rs index 4d3887971..8662a4d34 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -12,10 +12,8 @@ //! and macros that you can import easily as a group. //! //! ``` -//! extern crate ndarray; //! //! use ndarray::prelude::*; -//! # fn main() { } //! ``` #[doc(no_inline)]