Last active
August 6, 2019 12:06
-
-
Save dhirabayashi/e44ddb1505a0949d84a9b309a05f959a to your computer and use it in GitHub Desktop.
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 main() { | |
| for i in 1..10 { | |
| for j in 1..10 { | |
| print!("{} * {} = {}\t", i, j, i * j); | |
| } | |
| println!(); | |
| } | |
| } |
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 main() { | |
| let x = 1; | |
| let y = x; | |
| println!("{}", y); | |
| // Copyな型なので使える | |
| println!("{}", x); | |
| let a = "abc"; | |
| let b = a; | |
| // 参照型の場合も使える(文字列リテラルは&strなので) | |
| println!("{}", a); | |
| } |
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 factorial(n: usize) -> usize { | |
| if n == 0 { | |
| 1 | |
| } else { | |
| n * factorial(n - 1) | |
| } | |
| } | |
| fn main() { | |
| println!("{}", factorial(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
| fn fizzbuzz(n: usize) { | |
| for i in 0..n { | |
| if i % 15 == 0{ | |
| println!("FizzBuzz"); | |
| } else if i % 3 == 0 { | |
| println!("Fizz"); | |
| } else if i % 5 == 0 { | |
| println!("Buzz"); | |
| } else { | |
| println!("{}", i); | |
| } | |
| } | |
| } | |
| fn main() { | |
| fizzbuzz(20); | |
| } |
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 add(x: isize, y: isize) -> isize { | |
| x + y | |
| } | |
| fn main() { | |
| println!("{}", add(1, 2)); | |
| let f = add; | |
| println!("{}", f(3, 4)); | |
| } |
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 main() { | |
| println!("Hello, World"); | |
| } |
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 ref_string(s: &String) { | |
| println!("{}", s); | |
| } | |
| fn main() { | |
| let s = "This is a resource".to_string(); | |
| // 参照一つ目 | |
| let t = &s; | |
| ref_string(t); | |
| // 参照二つ目。イミュータブルなら同時に複数存在できる | |
| ref_string(&s); | |
| } |
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 main() { | |
| let n = 1 + 2; | |
| // n = 5; error[E0384]: cannot assign twice to immutable variable `n` | |
| println!("{}", n); | |
| } |
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 main() { | |
| let s = "owned data".to_string(); | |
| { | |
| let t = s; | |
| } | |
| // println!("{}", s); // error[E0382]: borrow of moved value: `s` | |
| // println!("{}", t); // error[E0425]: cannot find value `t` in this scope | |
| { | |
| let s = "owned data".to_string(); | |
| let ref_s = &s; | |
| // 以下のように`s`のライフタイムを`ref_s`より先に終わらせようとするとエラーになる...はずなのだがならない… | |
| let t = s; // cannot move out of `s` because it is borrowed | |
| } | |
| } |
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 main() { | |
| match 10 { | |
| 0 => println!("0"), | |
| 1...10 => println!("small number"), | |
| n => println!("big number: {}", n), | |
| } | |
| match (1.0, 1) { | |
| (0.0, 0) => println!("all zero"), | |
| (f, 1...10) => println!("float: {} with small number", f), | |
| _ => println!("other tuple"), | |
| } | |
| } |
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 main() { | |
| let mut x = 1 + 2; | |
| x = 5; | |
| println!("{}", x); | |
| } |
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 refmut_string(s: &mut String) { | |
| println!("{}", s); | |
| } | |
| fn main() { | |
| let mut s = "this is a resource".to_string(); | |
| // ミュータブルな参照一つ目 | |
| let t = &mut s; | |
| refmut_string(t); | |
| // ミュータブルな参照二つ目はエラー | |
| // refmut_string(&s); // error[E0499]: cannot borrow `s` as mutable more than once at a time | |
| } |
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 rebind() { | |
| let sum = 0; | |
| for i in 0..10 { | |
| let sum = sum + i; | |
| } | |
| println!("{}", sum); | |
| } | |
| fn reassign() { | |
| let mut sum = 0; | |
| for i in 0..10 { | |
| sum += i; | |
| } | |
| println!("{}", sum); | |
| } | |
| fn main() { | |
| rebind(); | |
| reassign(); | |
| } |
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 main() { | |
| let mut a = 1; | |
| let b = &mut a; | |
| *b = 2; | |
| // println!("{}", a); error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable | |
| println!("{}", *b); | |
| } |
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 print_string(s: String) { | |
| println!("{}", s); | |
| } | |
| fn main() { | |
| let s = "This is a resource".to_string(); | |
| println!("{}", s); | |
| let t = s; | |
| //println!("{}", s); error[E0382]: borrow of moved value: `s` | |
| print_string(t); | |
| //print_string(t); error[E0382]: use of moved value: `t` | |
| } |
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 main() { | |
| let a: [isize;3] = [1, 2, 3]; | |
| let b: &[isize] = &a; | |
| println!("{:?}", a); | |
| println!("{:?}", b); | |
| for elm in b { | |
| println!("{}", elm); | |
| } | |
| println!("{}", b[0]); | |
| } |
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 main() { | |
| let mut a: String = "abc".to_string(); | |
| a += "def"; | |
| println!("{}", a); | |
| let x = 1.0.to_string(); | |
| println!("{}", x); | |
| a += x.as_str(); | |
| println!("{}", a); | |
| } |
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
| fn main() { | |
| let a: (isize, f64, &str) = (1, 1.0, "abc"); | |
| println!("{}, {}, {}", a.0, a.1, a.2); | |
| } |
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 main() { | |
| let x: i32 = 1 + 2; | |
| println!("{}", x); | |
| let x: &str = "abc"; | |
| println!("{}", x); | |
| } |
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 main() { | |
| let mut vec = vec![1, 2, 3]; | |
| // ミュータブルなのでここで変更可能 | |
| vec.push(4); | |
| println!("{:?}", vec); | |
| // ここでイミュータブルな参照が発生している(らしい) | |
| for i in vec.iter() { | |
| // 元はミュータブルだが、イミュータブルな参照が発生しているので変更できない | |
| // vec.push(i * 2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment