Last active
October 12, 2016 04:59
-
-
Save tera3939/3e31f871006f9d4ca1dfd6cf8035e45c to your computer and use it in GitHub Desktop.
ゼロコピーなのでは!?
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
[package] | |
name = "rust_de_zundoko" | |
version = "0.1.0" | |
[dependencies] | |
rand="0.3.0" |
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
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