4.1 Value lifecycles
A Mojo value has a beginning and an end. Mojo creates and initializes the value before you use it, then destroys it when it's no longer needed.
Lifecycle methods define what happens at each stage. Use them to control how your types initialize their state, transfer values, and release resources.
- Creating values — Construct values with generated and custom initializers.
- Initialization state — Understand when a value is initialized and ready to use.
- Automatic destruction — Let Mojo destroy values automatically after their last use.
- Explicit destruction — Require values to be deliberately destroyed in code.
Adapted from the Mojo Manual and the MAX GPU programming guides, part of the Modular Platform repository (github.com/modular/modular), Copyright (c) Modular Inc., licensed under the Apache License v2.0 with LLVM Exceptions. Changes were made: reformatted as an accessible XYZ web edition with build-verified example outputs. Mojo and MAX are trademarks of Modular Inc.; this edition is not affiliated with or endorsed by Modular. License: Apache-2.0 WITH LLVM-exception.