Skip to content

Commit 4bdd185

Browse files
authored
Add examples of binary arithmetic operators (#589)
* Add examples of binary arithmetic operators * Add note about element type implementing operator
1 parent 03552e2 commit 4bdd185

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,25 @@ pub type Ixs = isize;
638638
/// - `B @ &A` which consumes `B`, updates it with the result, and returns it
639639
/// - `C @= &A` which performs an arithmetic operation in place
640640
///
641+
/// Note that the element type needs to implement the operator trait and the
642+
/// `Clone` trait.
643+
///
644+
/// ```
645+
/// use ndarray::{array, ArrayView1};
646+
///
647+
/// let owned1 = array![1, 2];
648+
/// let owned2 = array![3, 4];
649+
/// let view1 = ArrayView1::from(&[5, 6]);
650+
/// let view2 = ArrayView1::from(&[7, 8]);
651+
/// let mut mutable = array![9, 10];
652+
///
653+
/// let sum1 = &view1 + &view2; // Allocates a new array. Note the explicit `&`.
654+
/// // let sum2 = view1 + &view2; // This doesn't work because `view1` is not an owned array.
655+
/// let sum3 = owned1 + view1; // Consumes `owned1`, updates it, and returns it.
656+
/// let sum4 = owned2 + &view2; // Consumes `owned2`, updates it, and returns it.
657+
/// mutable += &view2; // Updates `mutable` in-place.
658+
/// ```
659+
///
641660
/// ### Binary Operators with Array and Scalar
642661
///
643662
/// The trait [`ScalarOperand`](trait.ScalarOperand.html) marks types that can be used in arithmetic

0 commit comments

Comments
 (0)