diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
commit | 27514d58573ce1f844af4ea60afa72c7f58f1cd7 (patch) | |
tree | 4b16c31f73cb72435d03d5e105261143f8ca135f /Rust struct associated functions.md | |
parent | 22675cd8dc75d8b8d4b0f818f5b093efbc364802 (diff) |
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 |