# Continue-As-New - Rust SDK

> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.

> Use Temporal's Continue-As-New in Rust to manage large Event Histories by atomically creating new Workflow Executions with the same Workflow Id and fresh parameters.

This page covers the following for Rust developers:

- [What is Continue-As-New?](#what)
- [Use Continue-As-New](#how)
- [When is it right to Continue-As-New?](#when)

## What is Continue-As-New? 

[Continue-As-New](/workflow-execution/continue-as-new) lets a Workflow execution close successfully and creates a new Workflow execution. You can think of it as a checkpoint when your Workflow gets too long or approaches certain scaling limits.

The new Workflow execution is in the same [chain](/workflow-execution#workflow-execution-chain); it keeps the same Workflow Id but gets a new Run Id and a fresh Event History.
It also receives your Workflow's usual parameters.

## Use Continue-As-New with the Rust SDK 

First, design your Workflow parameters so that you can pass in the "current state" when you Continue-As-New into the next Workflow run.
This state is typically passed as a parameter or stored in the Workflow struct.

Inside your Workflow, call `ctx.continue_as_new()` and propagate its result:

```rust
use std::time::Duration;

use temporalio_macros::{workflow, workflow_methods};
use temporalio_sdk::{ActivityOptions, ContinueAsNewOptions, WorkflowContext, WorkflowResult};

use crate::activities::MyActivities;

#[workflow]
#[derive(Default)]
pub struct GreetingWorkflow;

#[workflow_methods]
impl GreetingWorkflow {
    #[run(name = "greeting-workflow-1")]
    pub async fn run(ctx: &mut WorkflowContext<Self>, name: String) -> WorkflowResult<String> {
        let greeting = ctx
            .execute_activity(
                MyActivities::greet,
                name.clone(),
                ActivityOptions::start_to_close_timeout(Duration::from_secs(30)),
            )
            .await?;

        println!("{}", greeting);

        if name == "Ziggy" {
            Ok(greeting)
        } else {
            ctx.continue_as_new("New Name".to_string(), ContinueAsNewOptions::default())?;
        }
    }
}
```

The `ctx.continue_as_new()` method accepts the input to pass to the next Workflow Run.

## When is it right to Continue-As-New with the Rust SDK? 

Use Continue-as-New when your Workflow might encounter degraded performance or [Event History Limits](/workflow-execution/event#event-history).

Temporal tracks your Workflow's progress against these limits to let you know when you should Continue-as-New. Call `ctx.continue_as_new_suggested()` to check if it's time.

## Test Continue-As-New with the Rust SDK 

Testing Workflows that naturally Continue-as-New may be time-consuming and resource-intensive. Instead, add a test hook to check your Workflow's Continue-as-New behavior faster in automated tests.

For example, if you have an internal value like `test_continue_as_new == True`, this sample takes a variable called `max_history_length` and that can be set to a small value. A helper method in the Workflow impl checks it each time it considers using Continue-as-New:

```rust
use serde::{Deserialize, Serialize};
use temporalio_macros::{workflow, workflow_methods};
use temporalio_sdk::{WorkflowContext, WorkflowResult};

#[derive(Serialize, Deserialize)]
pub struct GreetingInput {
    pub name: String,
    pub max_history_length: u32,
}

...

#[workflow_methods]
impl GreetingWorkflow {
    #[run]
    pub async fn run(
        ctx: &mut WorkflowContext<Self>,
        input: GreetingInput,
    ) -> WorkflowResult<String> {
        // your Workflow code here

        if Self::should_continue_as_new(ctx, input.max_history_length) {
            // Continue as new
        }
    }

    fn should_continue_as_new(
        ctx: &WorkflowContext<Self>,
        max_history_length: u32,
    ) -> bool {
        if ctx.continue_as_new_suggested() {
            return true;
        }

        // For testing
        if max_history_length > 0 && ctx.history_length() > max_history_length {
            return true;
        }

        false
    }
}
```

## Best practices 

1. Pass all necessary state: When continuing as new, include all state the next run needs.
2. Use meaningful iteration markers: Include iteration numbers or timestamps to track progress.
3. Test your state passing: Ensure parameters serialize and deserialize correctly.
4. Don't continue-as-new too frequently: It's better to have some Event History than to continue-as-new on every execution.
5. Consider batch sizes: Find the right balance between batch size and number of continues as new.
