Skip to content

Instantly share code, notes, and snippets.

@jaredly

jaredly/mem.rs

Last active Aug 29, 2015
Embed
What would you like to do?
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