Merge pull request #30 from lucis-fluxum/patch-1

Edits to rust Types sheet
pull/32/head
Igor Chubin 6 years ago committed by GitHub
commit ad8106bc98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,15 +43,30 @@
// Methods //
impl<T> Foo<T> {
// 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::<i32>::do_baz("Hello", 24);
// Traits (known as interfaces or typeclasses in other languages) //
trait Frobnicate<T> {

Loading…
Cancel
Save