In Chrono Realms, Time Keepers often deal with not just trees of timelines, but Chrono Chains-sequences of linked TimeNodes, each representing a specific moment in time. A Chrono Chain is a straight path of sequential moments, where each TimeNode connects to the next. These Chrono Chains are useful when traveling through a series of specific events, as they allow Time Keepers to follow a single timeline.
However, to handle these potentially long Chrono Chains, Time Keepers use Smart Pointers (Box<T>) to safely manage and traverse these lists of moments without causing unnecessary memory duplication or overflow. Each TimeNode holds a reference to the next node, forming a recursive structure.
Your task as an apprentice is to implement a Chrono Chain as a recursive list structure using smart pointers.
In this exercise, you will:
ChronoChain enum, representing a list of moments.Box<T> smart pointer to store the recursive nodes.ChronoChain from an array of u32 values.ChronoChain and sum up the values stored in the list.ChronoChain EnumCreate a recursive enum ChronoChain with two variants:
End: Represents the end of the list.Link: Contains a u32 value and a boxed reference to the next node in the chain.Write a function ChronoChain::build that takes an array of u32 values and returns a ChronoChain, linking the values sequentially using smart pointers.
Write a function ChronoChain::sum to recursively traverse the ChronoChain and sum the values of all nodes.
fn main() {
// Create a ChronoChain from an array of values
let chrono_chain = ChronoChain::build(array![10, 20, 30]);
// Sum the values in the ChronoChain
let total_sum = chrono_chain.sum();
println!("Total Time Power: {}", total_sum);
}In Chrono Realms, Time Keepers often deal with not just trees of timelines, but Chrono Chains-sequences of linked TimeNodes, each representing a specific moment in time. A Chrono Chain is a straight path of sequential moments, where each TimeNode connects to the next. These Chrono Chains are useful when traveling through a series of specific events, as they allow Time Keepers to follow a single timeline.
However, to handle these potentially long Chrono Chains, Time Keepers use Smart Pointers (Box<T>) to safely manage and traverse these lists of moments without causing unnecessary memory duplication or overflow. Each TimeNode holds a reference to the next node, forming a recursive structure.
Your task as an apprentice is to implement a Chrono Chain as a recursive list structure using smart pointers.
In this exercise, you will:
ChronoChain enum, representing a list of moments.Box<T> smart pointer to store the recursive nodes.ChronoChain from an array of u32 values.ChronoChain and sum up the values stored in the list.ChronoChain EnumCreate a recursive enum ChronoChain with two variants:
End: Represents the end of the list.Link: Contains a u32 value and a boxed reference to the next node in the chain.Write a function ChronoChain::build that takes an array of u32 values and returns a ChronoChain, linking the values sequentially using smart pointers.
Write a function ChronoChain::sum to recursively traverse the ChronoChain and sum the values of all nodes.
fn main() {
// Create a ChronoChain from an array of values
let chrono_chain = ChronoChain::build(array![10, 20, 30]);
// Sum the values in the ChronoChain
let total_sum = chrono_chain.sum();
println!("Total Time Power: {}", total_sum);
}