Skip to main content
Glama
while-loops-while.md1.05 kB
## While Loops: `{#while}` Use `while` for loops that need to continue until a condition is false: Rust ``` let items = get_items(); let mut idx = 0; let code = ts_template! {     {$let mut i = 0}     {#while i < items.len()}         console.log("Item @{i}");         {$do i += 1}     {/while} }; ``` ### While-Let Pattern Matching Use `while let` for iterating with pattern matching, similar to `if let`: Rust ``` let mut items = vec!["a", "b", "c"].into_iter(); let code = ts_template! {     {#while let Some(item) = items.next()}         console.log("@{item}");     {/while} }; ``` **Generates:** TypeScript ``` console.log("a"); console.log("b"); console.log("c"); ``` This is especially useful when working with iterators or consuming optional values: Rust ``` let code = ts_template! {     {#while let Some(next_field) = remaining_fields.pop()}         result.@{next_field.name} = this.@{next_field.name};     {/while} }; ```

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/macroforge-ts/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server