IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

@staticmethod

You can add the @staticmethod decorator on a struct method to declare a static method.

For example:

from std.pathlib import Path


struct MyStruct(Movable):
var data: List[UInt8]

def __init__(out self):
self.data = List[UInt8]()

@staticmethod
def load_from_file(file_path: Path) raises -> Self:
var new_struct = MyStruct()
new_struct.data = file_path.read_bytes()
return new_struct ^

Unlike an instance method, a static method doesn't take an implicit self argument. It's not attached to a specific instance of a struct, so it can't access instance data.

For more information see the documentation on static methods.