Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2016 14:47
Show Gist options
  • Save anonymous/83ab4aae57ed41e793c7126d2492cf40 to your computer and use it in GitHub Desktop.
Save anonymous/83ab4aae57ed41e793c7126d2492cf40 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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