From 0a1522b281939dcd4923e0724d46ae672d11db4b Mon Sep 17 00:00:00 2001 From: Shankmukh Kiran Sagar Date: Sun, 31 May 2026 01:46:39 +0530 Subject: [PATCH] feat: add contains() method to check if index is within array bounds --- src/impl_methods.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 2170a8d93..a54e512d1 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -815,6 +815,24 @@ impl ArrayRef { unsafe { self.get_ptr(index).map(|ptr| &*ptr) } } + + /// Return `true` if the index is within the array bounds. + /// + /// ``` + /// use ndarray::arr2; + /// + /// let a = arr2(&[[1., 2.], + /// [3., 4.]]); + /// + /// assert!(a.contains((0, 1))); + /// assert!(!a.contains((2, 0))); + /// assert!(!a.contains((0, 2))); + /// ``` + pub fn contains(&self, index: I) -> bool + where I: NdIndex + { + index.index_checked(self._dim(), self._strides()).is_some() + } } impl RawRef