Skip to content

Instantly share code, notes, and snippets.

@jaredly
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredly/fc9a06d9f3fe0d2553df to your computer and use it in GitHub Desktop.
Save jaredly/fc9a06d9f3fe0d2553df to your computer and use it in GitHub Desktop.
Rust Optimization Opportunity
use std::kinds::marker::NoCopy;
struct WillMove {
boxed: NoCopy,
large: [uint, ..100000],
}
/*******************************/
/** VARIANT 1 : with movement **/
/*******************************/
fn show(x: WillMove) {
println!("The 5th element is {}", x.large[5]);
}
fn main() {
let x = WillMove {
boxed: NoCopy,
large: [20, ..100000],
};
show(x);
// let y = x.large[10]; // uncommenting this line will trigger a compiler error "can't use moved value"
}
/**********************************/
/** VARIANT 2 : with & borrowing **/
/**********************************/
fn show(x: &WillMove) {
println!("The 5th element is {}", x.large[5]);
}
fn main() {
let x = WillMove {
boxed: NoCopy,
large: [20, ..100000],
};
show(&x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment