diff --git a/sheets/_rust/Types b/sheets/_rust/Types index cdf42fe..37f29ad 100644 --- a/sheets/_rust/Types +++ b/sheets/_rust/Types @@ -43,15 +43,30 @@ // Methods // impl Foo { - // Methods take an explicit `self` parameter + // Instance methods take an explicit `self` parameter. + // Using `self` on its own will consume the caller, while + // `&self` or `&mut self` will create immutable and mutable + // references, respectively. fn get_bar(self) -> T { self.bar } + + // Static methods don't take a `self` parameter, but can still + // use the generic `T` type. + fn do_baz(msg: &str, baz: T) -> T { + println!("{}", msg); + baz + } } + // Here `T` is inferred to be some integer type let a_foo = Foo { bar: 1 }; println!("{}", a_foo.get_bar()); // 1 + // `T` can be whatever you want, using "turbofish" syntax + // The statement below prints "Hello" and sets `result` to 24 + let result: i32 = Foo::::do_baz("Hello", 24); + // Traits (known as interfaces or typeclasses in other languages) // trait Frobnicate {