Last active
October 9, 2017 05:30
-
-
Save chansuke/bf85c7d72e215e7e539445d155eb0fbb to your computer and use it in GitHub Desktop.
Rust basics
This file contains hidden or 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
fn square_sum(n: isize) -> isize { | |
(0..n) | |
.filter(|i| i % 2 == 0) | |
.map(|i| i * i) | |
.sum() | |
} | |
fn main() { | |
println!("{}", square_sum(10)); | |
} |
This file contains hidden or 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
trait WanLike { | |
fn wan(&self); | |
fn walk(&self) { | |
println!("walking"); | |
} | |
} | |
struct Dog; | |
impl WanLike for Dog { | |
fn wan(&self) { | |
println!("wan"); | |
} | |
} | |
struct Nyan; | |
impl WanLike for Nyan { | |
fn wan(&self) { | |
println!("nyan"); | |
} | |
fn walk(&self) { | |
println!("nyaos"); | |
} | |
} | |
impl WanLike for i64 { | |
fn wan(&self) { | |
for _ in 0..*self { | |
println!("nyanyanyan"); | |
} | |
} | |
} | |
fn main() { | |
let inu = Dog; | |
let neko = Nyan; | |
let i = 15; | |
inu.wan(); | |
neko.wan(); | |
i.wan(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment