Skip to content

Commit 7f67fdf

Browse files
viniciusdLukeMathWalker
authored andcommitted
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
1 parent 4d6188a commit 7f67fdf

File tree

4 files changed

+28
-57
lines changed

4 files changed

+28
-57
lines changed

src/free_functions.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ use crate::{dimension, ArcArray1, ArcArray2};
1616
/// three dimensions.
1717
///
1818
/// ```
19-
/// extern crate ndarray;
20-
///
2119
/// use ndarray::array;
20+
/// let a1 = array![1, 2, 3, 4];
2221
///
23-
/// fn main() {
24-
/// let a1 = array![1, 2, 3, 4];
25-
///
26-
/// let a2 = array![[1, 2],
27-
/// [3, 4]];
22+
/// let a2 = array![[1, 2],
23+
/// [3, 4]];
2824
///
29-
/// let a3 = array![[[1, 2], [3, 4]],
30-
/// [[5, 6], [7, 8]]];
25+
/// let a3 = array![[[1, 2], [3, 4]],
26+
/// [[5, 6], [7, 8]]];
3127
///
32-
/// assert_eq!(a1.shape(), &[4]);
33-
/// assert_eq!(a2.shape(), &[2, 2]);
34-
/// assert_eq!(a3.shape(), &[2, 2, 2]);
35-
/// }
28+
/// assert_eq!(a1.shape(), &[4]);
29+
/// assert_eq!(a2.shape(), &[2, 2]);
30+
/// assert_eq!(a3.shape(), &[2, 2, 2]);
3631
/// ```
3732
///
3833
/// This macro uses `vec![]`, and has the same ownership semantics;
@@ -115,19 +110,14 @@ pub fn aview2<A, V: FixedInitializer<Elem = A>>(xs: &[V]) -> ArrayView2<'_, A> {
115110
/// Create a one-dimensional read-write array view with elements borrowing `xs`.
116111
///
117112
/// ```
118-
/// extern crate ndarray;
119-
///
120113
/// use ndarray::{aview_mut1, s};
121-
///
122114
/// // Create an array view over some data, then slice it and modify it.
123-
/// fn main() {
124-
/// let mut data = [0; 1024];
125-
/// {
126-
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
127-
/// a.slice_mut(s![.., ..;3]).fill(5);
128-
/// }
129-
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
115+
/// let mut data = [0; 1024];
116+
/// {
117+
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
118+
/// a.slice_mut(s![.., ..;3]).fill(5);
130119
/// }
120+
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
131121
/// ```
132122
pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
133123
ArrayViewMut::from(xs)
@@ -143,20 +133,18 @@ pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
143133
/// ```
144134
/// use ndarray::aview_mut2;
145135
///
146-
/// fn main() {
147-
/// // The inner (nested) array must be of length 1 to 16, but the outer
148-
/// // can be of any length.
149-
/// let mut data = [[0.; 2]; 128];
150-
/// {
151-
/// // Make a 128 x 2 mut array view then turn it into 2 x 128
152-
/// let mut a = aview_mut2(&mut data).reversed_axes();
153-
/// // Make the first row ones and second row minus ones.
154-
/// a.row_mut(0).fill(1.);
155-
/// a.row_mut(1).fill(-1.);
156-
/// }
157-
/// // look at the start of the result
158-
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
136+
/// // The inner (nested) array must be of length 1 to 16, but the outer
137+
/// // can be of any length.
138+
/// let mut data = [[0.; 2]; 128];
139+
/// {
140+
/// // Make a 128 x 2 mut array view then turn it into 2 x 128
141+
/// let mut a = aview_mut2(&mut data).reversed_axes();
142+
/// // Make the first row ones and second row minus ones.
143+
/// a.row_mut(0).fill(1.);
144+
/// a.row_mut(1).fill(-1.);
159145
/// }
146+
/// // look at the start of the result
147+
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
160148
/// ```
161149
pub fn aview_mut2<A, V: FixedInitializer<Elem = A>>(xs: &mut [V]) -> ArrayViewMut2<'_, A> {
162150
let cols = V::len();

src/impl_constructors.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ where
143143
/// # Some(())
144144
/// # }
145145
/// #
146-
/// # fn main() { example().unwrap() }
146+
/// # example().unwrap();
147147
/// ```
148148
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
149149
where
@@ -485,8 +485,6 @@ where
485485
/// ### Examples
486486
///
487487
/// ```
488-
/// extern crate ndarray;
489-
///
490488
/// use ndarray::{s, Array2};
491489
///
492490
/// // Example Task: Let's create a column shifted copy of a in b
@@ -503,9 +501,7 @@ where
503501
/// b
504502
/// }
505503
///
506-
/// # fn main() {
507-
/// # shift_by_two(&Array2::zeros((8, 8)));
508-
/// # }
504+
/// # shift_by_two(&Array2::zeros((8, 8)));
509505
/// ```
510506
pub unsafe fn uninitialized<Sh>(shape: Sh) -> Self
511507
where

src/lib.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,9 @@ pub type Ixs = isize;
483483
/// [`.multi_slice_move()`]: type.ArrayViewMut.html#method.multi_slice_move
484484
///
485485
/// ```
486-
/// extern crate ndarray;
487486
///
488487
/// use ndarray::{arr2, arr3, s};
489488
///
490-
/// fn main() {
491-
///
492489
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
493490
///
494491
/// let a = arr3(&[[[ 1, 2, 3], // -- 2 rows \_
@@ -546,7 +543,6 @@ pub type Ixs = isize;
546543
/// [5, 7]]);
547544
/// assert_eq!(s0, i);
548545
/// assert_eq!(s1, j);
549-
/// }
550546
/// ```
551547
///
552548
/// ## Subviews
@@ -579,11 +575,9 @@ pub type Ixs = isize;
579575
/// [`.outer_iter_mut()`]: #method.outer_iter_mut
580576
///
581577
/// ```
582-
/// extern crate ndarray;
583578
///
584579
/// use ndarray::{arr3, aview1, aview2, s, Axis};
585580
///
586-
/// # fn main() {
587581
///
588582
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
589583
///
@@ -617,7 +611,6 @@ pub type Ixs = isize;
617611
/// // You can take multiple subviews at once (and slice at the same time)
618612
/// let double_sub = a.slice(s![1, .., 0]);
619613
/// assert_eq!(double_sub, aview1(&[7, 10]));
620-
/// # }
621614
/// ```
622615
///
623616
/// ## Arithmetic Operations
@@ -1063,7 +1056,6 @@ pub type Ixs = isize;
10631056
/// ```rust
10641057
/// use ndarray::{array, Array2};
10651058
///
1066-
/// # fn main() -> Result<(), Box<std::error::Error>> {
10671059
/// let ncols = 3;
10681060
/// let mut data = Vec::new();
10691061
/// let mut nrows = 0;
@@ -1075,8 +1067,7 @@ pub type Ixs = isize;
10751067
/// }
10761068
/// let arr = Array2::from_shape_vec((nrows, ncols), data)?;
10771069
/// assert_eq!(arr, array![[0, 0, 0], [1, 1, 1]]);
1078-
/// # Ok(())
1079-
/// # }
1070+
/// # Ok::<(), ndarray::ShapeError>(())
10801071
/// ```
10811072
///
10821073
/// If neither of these options works for you, and you really need to convert
@@ -1089,7 +1080,6 @@ pub type Ixs = isize;
10891080
/// ```rust
10901081
/// use ndarray::{array, Array2, Array3};
10911082
///
1092-
/// # fn main() -> Result<(), Box<std::error::Error>> {
10931083
/// let nested: Vec<Array2<i32>> = vec![
10941084
/// array![[1, 2, 3], [4, 5, 6]],
10951085
/// array![[7, 8, 9], [10, 11, 12]],
@@ -1102,8 +1092,7 @@ pub type Ixs = isize;
11021092
/// [[1, 2, 3], [4, 5, 6]],
11031093
/// [[7, 8, 9], [10, 11, 12]],
11041094
/// ]);
1105-
/// # Ok(())
1106-
/// # }
1095+
/// # Ok::<(), ndarray::ShapeError>(())
11071096
/// ```
11081097
///
11091098
/// Note that this implementation assumes that the nested `Vec`s are all the

src/prelude.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
//! and macros that you can import easily as a group.
1313
//!
1414
//! ```
15-
//! extern crate ndarray;
1615
//!
1716
//! use ndarray::prelude::*;
18-
//! # fn main() { }
1917
//! ```
2018
2119
#[doc(no_inline)]

0 commit comments

Comments
 (0)