From 27514d58573ce1f844af4ea60afa72c7f58f1cd7 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Thu, 21 Aug 2025 22:09:29 +0200 Subject: vault backup: 2025-08-21 22:09:29 --- Rust struct associated functions.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Rust struct associated functions.md (limited to 'Rust struct associated functions.md') 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 -- cgit v1.2.3