Skip to content

Instantly share code, notes, and snippets.

@chansuke
Last active October 9, 2017 05:30
Show Gist options
  • Save chansuke/bf85c7d72e215e7e539445d155eb0fbb to your computer and use it in GitHub Desktop.
Save chansuke/bf85c7d72e215e7e539445d155eb0fbb to your computer and use it in GitHub Desktop.
Rust basics
fn square_sum(n: isize) -> isize {
(0..n)
.filter(|i| i % 2 == 0)
.map(|i| i * i)
.sum()
}
fn main() {
println!("{}", square_sum(10));
}
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