109 words
1 minutes
240604_FreeFn_VS_AssociatedFn

link#


Free Functions vs. Associated Functions (Methods)#

FeatureFree FunctionAssociated Function (Method)
DefinitionDefined at the module level, not within an impl block.Defined within an impl block for a struct or a trait.
Calling Syntaxfunction_name(args) or module::function_name(args).instance.method_name(args) or Struct::method_name(args) (if it doesn’t take &self, &mut self, or self).
self ParameterDoes not take &self, &mut self, or self as a parameter.Takes &self, &mut self, or self to operate on an instance of the struct.
Use CaseOften used for static utility functions, operations where no single data “owner” is clear, or generic functions.Used when the function’s logic is tightly coupled to a specific data structure.

같이 보면 좋은 글#

.:: 차이점 | asar blog

240604_FreeFn_VS_AssociatedFn
https://younghakim7.github.io/blog/posts/240604_freefn_vs_associatedfn/
Author
YoungHa
Published at
2024-06-04