Skip to content

Instantly share code, notes, and snippets.

@tera3939
Last active October 12, 2016 04:59
Show Gist options
  • Save tera3939/3e31f871006f9d4ca1dfd6cf8035e45c to your computer and use it in GitHub Desktop.
Save tera3939/3e31f871006f9d4ca1dfd6cf8035e45c to your computer and use it in GitHub Desktop.
ゼロコピーなのでは!?
[package]
name = "rust_de_zundoko"
version = "0.1.0"
[dependencies]
rand="0.3.0"
extern crate rand;
use rand::Rng;
struct Zundoko {
// ライフタイムを明記してみる
zun: &'static str,
doko: &'static str,
kiyoshi: &'static str,
}
impl Zundoko {
fn new(zun: &'static str, doko: &'static str, kiyoshi: &'static str) -> Zundoko {
Zundoko {
zun: zun,
doko: doko,
kiyoshi: kiyoshi,
}
}
fn go(&self) {
let mut zun_counter = 0;
loop{
// &で借用する気持ちを表明
let zundoko = if rand::thread_rng().gen() {&self.zun} else {&self.doko};
println!("{}", zundoko);
if zundoko == &self.zun {
zun_counter += 1;
} else if zun_counter > 4 {
println!("{}", self.kiyoshi);
break;
} else {
zun_counter = 0;
}
}
}
}
fn main() {
Zundoko::new("ズン", "ドコ", "キ・ヨ・シ!!").go();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment