Skip to content

Commit 68f258c

Browse files
committed
Add examples to docs for .first() and .first_mut()
1 parent 516294b commit 68f258c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/impl_methods.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ where
256256

257257
/// Returns a reference to the first element of the array, or `None` if it
258258
/// is empty.
259+
///
260+
/// # Example
261+
///
262+
/// ```rust
263+
/// use ndarray::Array3;
264+
///
265+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
266+
/// a[[0, 0, 0]] = 42.;
267+
/// assert_eq!(a.first(), Some(&42.));
268+
///
269+
/// let b = Array3::<f64>::zeros([3, 0, 5]);
270+
/// assert_eq!(b.first(), None);
271+
/// ```
259272
pub fn first(&self) -> Option<&A>
260273
where
261274
S: Data,
@@ -269,6 +282,19 @@ where
269282

270283
/// Returns a mutable reference to the first element of the array, or
271284
/// `None` if it is empty.
285+
///
286+
/// # Example
287+
///
288+
/// ```rust
289+
/// use ndarray::Array3;
290+
///
291+
/// let mut a = Array3::<f64>::zeros([3, 4, 2]);
292+
/// *a.first_mut().unwrap() = 42.;
293+
/// assert_eq!(a[[0, 0, 0]], 42.);
294+
///
295+
/// let mut b = Array3::<f64>::zeros([3, 0, 5]);
296+
/// assert_eq!(b.first_mut(), None);
297+
/// ```
272298
pub fn first_mut(&mut self) -> Option<&mut A>
273299
where
274300
S: DataMut,

0 commit comments

Comments
 (0)