Created
April 4, 2022 07:27
-
-
Save ogrew/69cf3fd4e9c3f73a7cf68bfd4415539a to your computer and use it in GitHub Desktop.
とほほのRust入門を読む
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
// ref: とほほのRust入門(https://www.tohoho-web.com/ex/rust.html) | |
use rand::Rng; | |
use std::boxed::Box; | |
use std::collections::HashMap; | |
// enum Color { | |
// Red, | |
// Green, | |
// Blue, | |
// } | |
struct Point { | |
x: i32, | |
y: i32, | |
} | |
struct Rect { | |
width: u32, | |
height: u32, | |
} | |
impl Rect { | |
fn area(&self) -> u32 { | |
self.width * self.height | |
} | |
} | |
trait Printable { | |
fn print(&self); | |
} | |
impl Printable for Rect { | |
fn print(&self) { | |
println!("width : {}, height : {}", self.width, self.height) | |
} | |
} | |
struct Circle<T> { | |
radius: T, | |
} | |
impl<T> Printable for Circle<T> | |
where | |
T: std::fmt::Display, | |
{ | |
fn print(self: &Circle<T>) { | |
println!("ciecle radius : {}", self.radius); | |
} | |
} | |
struct Dog {} | |
struct Cat {} | |
trait Animal { | |
fn cry(&self); | |
} | |
impl Animal for Dog { | |
fn cry(&self) { | |
println!("Bow-wow"); | |
} | |
} | |
impl Animal for Cat { | |
fn cry(&self) { | |
println!("Miaow"); | |
} | |
} | |
fn get_animal(animal_type: &str) -> Box<dyn Animal> { | |
if animal_type == "dog" { | |
Box::new(Dog {}) | |
} else { | |
Box::new(Cat {}) | |
} | |
} | |
fn average(x: i32, y: i32) -> i32 { | |
(x + y) / 2 | |
} | |
macro_rules! log { | |
($x:expr) => { | |
println!("log(macro) -> {}", $x); | |
}; | |
} | |
fn test1() { | |
let mut name = String::from("Kurabayashi"); | |
println!("name(test1) : {}", name); | |
name = test2(name); | |
test3(name); | |
// println!("name(test3) : {}", name); // ERROR!! | |
} | |
fn test2(name: String) -> String { | |
println!("name(test2) : {}", name); | |
name | |
} | |
fn test3(name: String) { | |
println!("name(test3) : {}", name); | |
} | |
fn main() { | |
println!("Hello, world!"); | |
// Structure | |
let p = Point { x: 100, y: 200 }; | |
println!("struct -> {}, {}", p.x, p.y); | |
// Enum | |
// let c = Color::Red; | |
// Tuple | |
let t = (10, "game", 3.14); | |
println!("tuple -> {}, {}, {}", t.0, t.1, t.2); | |
// Array | |
let arr = [13, 51, 86]; | |
println!("array -> {}, {}, {}", arr[0], arr[1], arr[2]); | |
// Vector | |
let mut v = vec![4, 5, 7]; | |
v.push(11); | |
println!("vector -> {}, {}, {}, {}", v[0], v[1], v[2], v[3]); | |
// for i in &v { | |
// println!("{}", i); | |
// } | |
// HashMap | |
let mut map = HashMap::new(); | |
map.insert("a", 10); | |
map.insert("b", 20); | |
map.insert("c", 30); | |
println!("map -> a: {}, b: {}, c: {}", map["a"], map["b"], map["c"]); | |
// for (k, v) in &map { | |
// println!("{}, {}", k, v); | |
// } | |
// string | |
let mut name: &str = "Nakamura"; | |
println!("name : {}", name); | |
name = "Okawara"; | |
println!("name : {}", name); | |
let mut namae = String::from("Nakamura"); | |
println!("namae : {}", namae); | |
namae = "Okawara".to_string(); | |
namae.push_str(" Erika"); | |
println!("namae : {}", namae); | |
// Box | |
let p2: Box<Point> = Box::new(Point { x: 300, y: 400 }); | |
println!("box(point) -> {}, {}", p2.x, p2.y); | |
// slice | |
let s = String::from("abcdefghijklmn"); | |
let s1 = &s[2..6]; | |
let s2 = &s[5..9]; | |
println!("slice -> s1 :{}, s2 : {}", s1, s2); | |
let a = [1, 2, 3, 5, 8, 13, 21, 34, 55]; | |
let a1 = &a[..3]; | |
let a2 = &a[7..]; | |
println!("slice -> a1 :{:?}, a2 : {:?}", a1, a2); | |
// function | |
println!("average (32, 16) -> {}", average(32, 16)); | |
// clojure | |
let cube = |x: i32| x * x * x; | |
println!("clojure -> {}", cube(3)); | |
// macro | |
log!("Aoyama Book Center"); | |
// if segment | |
let n = 5; | |
let m = if n == 5 { "BOY" } else { "GIRL" }; | |
log!(m); | |
// for loop | |
let mut l = 0; | |
for i in 0..10 { | |
l += i; | |
} | |
log!(l); | |
// match | |
let k = 2; | |
print!("match -> "); | |
match k { | |
1 => println!("ONE"), | |
2 => println!("TWO"), | |
3 => println!("THREE"), | |
_ => println!("ELSE"), | |
} | |
// implementation | |
let r = Rect { | |
width: 12, | |
height: 35, | |
}; | |
println!("impl(area) -> {}", r.area()); | |
// trait | |
let c1: Circle<i32> = Circle { radius: 30 }; | |
let c2: Circle<i64> = Circle { radius: 70 }; | |
c1.print(); | |
c2.print(); | |
print!("DOG :"); | |
get_animal("dog").cry(); | |
print!("CAT :"); | |
get_animal("cat").cry(); | |
// crate | |
let mut rng = rand::thread_rng(); | |
for _i in 1..5 { | |
println!("random number : {}", rng.gen_range(1, 101)); | |
} | |
// 参照型 | |
let u = 123; | |
let v = &u; | |
println!("{}", *v); | |
let u = 456; | |
let ref v = u; | |
println!("{}", *v); | |
let mut u = 879; | |
let v = &mut u; | |
*v = 789; | |
println!("{}", *v); | |
// 所有権 | |
test1(); | |
let s1 = String::from("block"); | |
let s2 = s1; | |
// println!("{}", s1); // ERROR! | |
println!("{}", s2); | |
// 型エイリアス(type) | |
type Hour = u32; | |
type Minute = u32; | |
type Second = u32; | |
let hh: Hour = 11; | |
let mm: Minute = 21; | |
let ss: Second = 49; | |
println!("{}:{};{}", hh, mm, ss); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment