diff options
Diffstat (limited to 'Rust struct associated functions.md')
-rw-r--r-- | Rust struct associated functions.md | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Rust struct associated functions.md b/Rust struct associated functions.md new file mode 100644 index 0000000..4c5e660 --- /dev/null +++ b/Rust struct associated functions.md @@ -0,0 +1,22 @@ +Associated functions are an umbrella term for all `fn` declared in an `impl` block. +A method is just an associated `fn` with `Self` as its first argument but it's not mandatory, for ie constructors or what in PHP would be a static class function. + +```rust +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn area(&self) -> u32 { + self.width * self.height + } + + fn square(size: u32) -> Rectangle { + Rectangle { + width: size, + height: size, + } + } +} +```
\ No newline at end of file |