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: Nightly
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).

struct_field_names

struct_field_names[T: AnyType]() -> InlineArray[StringSlice[StaticConstantOrigin], *?.field_count()]

Deprecated: use reflect[T]().field_names() instead.

Returns the names of all fields in struct T as an InlineArray.

This function works with both concrete types and generic type parameters.

Note: For best performance, assign the result to a comptime variable to ensure compile-time evaluation: comptime names = struct_field_names[T]()

Example:

from std.reflection import struct_field_names, struct_field_count

struct MyStruct:
var x: Int
var y: Float64

def print_field_names[T: AnyType]():
comptime names = struct_field_names[T]()
comptime for i in range(struct_field_count[T]()):
print(names[i])

def main():
print_field_names[MyStruct]() # Works with any struct!

Deprecated: Use reflect[T]().field_names() instead.

Constraints:

T must be a struct type. Passing a non-struct type results in a compile-time error.

Parameters:

Returns:

InlineArray[StringSlice[StaticConstantOrigin], *?.field_count()]: An InlineArray of StaticStrings, one for each field name in the struct.