Skip to content

Latest commit

 

History

History
63 lines (51 loc) · 2.04 KB

File metadata and controls

63 lines (51 loc) · 2.04 KB

In Rust, static functions (or associated functions) in an impl block are accessed via Self instead of &self because they do not operate on an instance of the struct. Instead, they belong to the type itself.

Key Differences:

Function Type How It’s Called Access to Instance (self) Purpose
Instance Method (fn method(&self)) my_struct.method() ✅ Yes, has access to instance fields Used when modifying or reading instance data
Static Method (fn method()) Struct::method() or Self::method() ❌ No, does not require an instance Used for utility functions, constructors, etc.

Example:

struct MyStruct {
    value: i32,
}

impl MyStruct {
    // Instance method (needs `self`)
    fn instance_method(&self) {
        println!("Instance method: value = {}", self.value);
    }

    // Static method (doesn't use `self`)
    fn static_method() {
        println!("Static method called!");
    }
}

fn main() {
    let obj = MyStruct { value: 10 };
    
    obj.instance_method();  // ✅ Calls instance method (needs an instance)
    
    MyStruct::static_method(); // ✅ Calls static method (no instance needed)
}

Why Use Self in Static Methods?

In Rust, Self refers to the struct type within an impl block. This is useful because:

  1. It avoids repeating the struct name:
    impl MyStruct {
        fn create() -> Self {  // Instead of MyStruct
            Self { value: 42 }
        }
    }
  2. It allows generic and flexible implementations:
    impl<T> SomeTrait for SomeStruct<T> {
        fn new() -> Self {  // Works for any `T`
            Self { /* fields */ }
        }
    }

Why Not &self for Static Methods?

  • &self requires an instance, but static methods do not operate on an instance.
  • Static methods exist at the type level, not the instance level.

Would you like a more specific example related to your use case? 🚀