Skip to content

Instantly share code, notes, and snippets.

@anka-213
Forked from anonymous/playground.rs
Created April 13, 2016 14:48
Show Gist options
  • Save anka-213/1b6310586f6b2a993109ef21dfa7a43b to your computer and use it in GitHub Desktop.
Save anka-213/1b6310586f6b2a993109ef21dfa7a43b 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