Skip to content

Instantly share code, notes, and snippets.

@gong023
Last active August 29, 2015 14:07
Show Gist options
  • Save gong023/b34b04ced12310ca3065 to your computer and use it in GitHub Desktop.
Save gong023/b34b04ced12310ca3065 to your computer and use it in GitHub Desktop.
/ 所有権帰ってくる
fn borrower_a(x: &int) -> int { *x }
// 所有権帰ってくる
fn borrower_b(x: &int) {
let a = x;
println!("{}", a);
}
// 所有権帰ってくる
fn borrower_c(v: &Vec<int>) {
for x in v.iter() {
println!("{}", x);
}
}
// 所有権帰ってくる
fn borrower_d(x: int) -> int {
// *x // type `int` cannot be dereferenced
x
}
// 所有権帰ってこない
//fn consumer_a(v: Vec<int>) {
// for x in v.iter() {
// println!("{}", x);
// }
//}
// 所有権帰ってこない
//fn consumer_b(v: Vec<int>) {
// for x in v.into_iter() {
// println!("{}", x);
// }
//}
fn main() {
// どうやったら所有権帰ってこないようになる?
let a = 1i;
borrower_a(&a);
borrower_b(&a);
borrower_d(a);
println!("{}", a);
let v = vec!(1i, 2, 3);
borrower_c(&v);
// consumer_a(v);
// consumer_b(v);
for x in v.iter() {
println!("{}", x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment