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
| class ConcurrentModification { | |
| public static void main(String[] args) { | |
| var list = new ArrayList<>(Arrays.asList("A", "B", "C")); | |
| for (var str : list) { | |
| System.out.println("Doubling " + str); | |
| list.add(str + str); | |
| } | |
| } | |
| } |
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 reference() -> u32 { | |
| let x: u32 = 10; | |
| let ref y = &x; // reference to x | |
| let z: &u32; | |
| { | |
| let short_lived: u32 = 82; | |
| z = &short_lived; | |
| } |
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 destruction() -> String{ | |
| let string1 = String::from("Hello World"); | |
| let string2 = String::new(); | |
| let string3 = string2; // string2 moved to string3, no longer valid | |
| for i in 0..32 { | |
| let another_string = String::from("Yellow Submarine"); | |
| do_something(another_string); // another_string "given away" here, no longer our concern | |
| } |
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 match_it(x: Option<i32>, flag: bool) -> i32 { | |
| match x { | |
| None => 0, | |
| Some(3) => 3, | |
| Some(_) if !flag => 450, | |
| Some(x) if x > 900 => 900, | |
| _ => -1 | |
| } | |
| } |
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 loops(limit: u32, values: Vec<i32>, condition: bool) { | |
| // while (condition) | |
| while condition { | |
| } | |
| // for (var i: values) | |
| for i in values { | |
| } |
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 calculate(x: i64, y: f64, z: i32) -> f64 { | |
| let x = x + (if x < z as i64 { 2 } else { -5 }); | |
| let q = y * x; | |
| q + z | |
| } |
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
| class Foo { | |
| double calculate(long x, double y, int z) { | |
| var delta = x < z ? 2 : -5; | |
| x += delta; | |
| var q = y * x; | |
| return q + z; | |
| } | |
| } |
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 Stringify { | |
| fn stringify(self) -> String; | |
| } | |
| impl Stringify for String { | |
| fn stringify(self) -> String { | |
| self | |
| } | |
| } |
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
| pub struct Person { | |
| name: String, | |
| age: u32 | |
| } | |
| impl Person { | |
| pub fn new(name: String) -> Self { | |
| Person { name, age: 18 } | |
| } | |
| } |
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
| public class Person implements Named { | |
| private String name; | |
| private int age; | |
| public Person(String name) { | |
| this.name = name; | |
| this.age = 18; | |
| } | |
| @Override public String getName() { |
NewerOlder