Skip to content

Forcing The Associated Function Syntax

In some cases, function names might overlap with other std functions that need to be reserved for clarity and safety.

For example, we might want to implement our custom type Foo to have a method called get_mut.

This would be confused with the slice ([T]) method get_mut

rust
impl<T> Foo<T> {
	pub fn get_mut(&mut self) -> Option<&mut T> {
		// ...
	}
}
rust
impl<T> Foo<T> {
	pub fn get_mut(this: &mut Self) -> Option<&mut T> {
		// ...
	}
}

So, to enforce the associated function syntax Foo::get_mut(instance) we need to remove &mut self from the function fingerprint.

Since this is not a reserved keyword in Rust, we can use it freely in place of self.