-
-
Save anka-213/1b6310586f6b2a993109ef21dfa7a43b to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
use std::borrow::Cow; | |
#[allow(dead_code)] | |
fn abs_all(input: &mut Cow<[i32]>) { | |
for i in 0..input.len() { | |
let v = input[i]; | |
if v < 0 { | |
// clones into a vector the first time (if not already owned) | |
input.to_mut()[i] = -v; | |
} | |
} | |
} | |
let v : Cow<[i32]> = Cow::Owned(vec![1,-4,5]); | |
let mut v2 : Cow<[i32]> = v.clone(); | |
abs_all(&mut v2); | |
println!("v: {:?}, v2: {:?}", v, v2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment