Last active
August 29, 2015 14:10
-
-
Save jaredly/fc9a06d9f3fe0d2553df to your computer and use it in GitHub Desktop.
Rust Optimization Opportunity
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
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