# Learn Rust with Golang Example by David Jr. 2022/08/01 --- # TOC * Topics * Mutable, Reference and Mutable Reference * Trait, Struct, Implementation * Closure * Example - Anchor * Key Takeaways --- # Topics --- ## 1. Mutable, Reference and Mutable Reference Variable bindings are immutable by default, but this can be overridden using the mut modifier. ![](https://i.imgur.com/uzbNdBc.png) ---- ![](https://i.imgur.com/mFfiD9G.png) ---- ![](https://i.imgur.com/8rgGfTZ.png) ---- ![](https://i.imgur.com/dGPAKOh.png) ---- ![](https://i.imgur.com/26TKU0I.png) --- ## 2. Trait, Struct, Implementation ```go type geometry interface { area() float64 perim() float64 } type rect struct { width, height float64 } type circle struct { radius float64 } func (r rect) area() float64 { return r.width * r.height } func (r rect) perim() float64 { return 2*r.width + 2*r.height } func (c circle) area() float64 { return math.Pi * c.radius * c.radius } func (c circle) perim() float64 { return 2 * math.Pi * c.radius } func measure(g geometry) { fmt.Println(g) fmt.Println(g.area()) fmt.Println(g.perim()) } func main() { r := rect{width: 3, height: 4} c := circle{radius: 5} measure(r) measure(c) } ``` ---- ```rust use core::f64::consts::PI; use core::fmt::Debug; trait Geometry { fn area(&self) -> f64; fn perim(&self) -> f64; } #[derive(Debug)] struct Rect { width: f64, height: f64, } #[derive(Debug)] struct Circle { radius: f64, } impl Geometry for Rect { fn area(&self) -> f64 { self.width * self.height } fn perim(&self) -> f64 { 2.0 * self.height + 2.0 * self.width } } impl Geometry for Circle { fn area(&self) -> f64 { PI * self.radius * self.radius } fn perim(&self) -> f64 { 2.0 * PI * self.radius } } fn main() { fn measure<T>(g: &T) where T: Geometry + Debug { println!("{:?}", g); println!("{:?}", g.area()); println!("{:?}", g.perim()); } let r = Rect{width: 3.0, height: 4.0}; let c = Circle{radius: 5.0}; measure(&r); measure(&c); } ``` --- ## 3. Closure ```go func intSeq() func() int { i := 0 return func() int { i++ return i } } func main() { nextInt := intSeq() fmt.PrintLn(nextInt()) fmt.PrintLn(nextInt()) fmt.PrintLn(nextInt()) } 1 2 3 ``` ---- ```rust fn main() { fn int_seq() -> Box<dyn FnMut() -> i32> { let mut i = 0; Box::new(move || { i += 1; i }) } let mut next_int: Box<_> = int_seq(); assert_eq!([1,2,3], [next_int(), next_int(), next_int()]); ``` --- # Example <https://github.com/coral-xyz/anchor> --- # Key Takeaways * Mutable, Reference and Mutable Reference: vars are immutable by default * Trait, Struct, Implementation: Similar to Go's struct * Closure: Need a heap of pointers --- # Refs 1. [ref](https://rustwiki.org/en/rust-by-example/variable_bindings/mut.html)] 2. https://askeing.github.io/rust-book/mutability.html 3. https://segmentfault.com/a/1190000021901693 4. https://studygolang.com/articles/26944 5. https://studygolang.com/articles/26933
{"metaMigratedAt":"2023-06-17T06:00:21.261Z","metaMigratedFrom":"YAML","title":"Learn Rust with Golang Example","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"20f67950-8a5d-4f1d-a303-568606ddda38\",\"add\":5509,\"del\":2121}]"}
    245 views