# Return Value Optimization in Rust

DevFeed: [Return Value Optimization in Rust](<https://devfeed.tech/articles/return-value-optimization-in-rust-35456.md>)

Original publisher: [Read original article](<https://darkcoding.net/software/return-value-optimization-in-rust/>)

Author: Graham King

Published: 2022-02-26T22:49:47Z

Content type: article

Language: en

Sources: [Graham King](<https://devfeed.tech/sources/graham-king.md>)

Topics: [Optimization](<https://devfeed.tech/topics/optimization.md>), [Rust](<https://devfeed.tech/topics/rust.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [function](<https://devfeed.tech/tags/function.md>), [memory](<https://devfeed.tech/tags/memory.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [rust](<https://devfeed.tech/tags/rust.md>), [software](<https://devfeed.tech/tags/software.md>)

## AI overview

This article explains return value optimization in Rust, showing how LLVM can place a returned value directly in the caller's stack frame to avoid memory copies. It also notes that behavior changed in Rust 1.71: reading the value's address can prevent the optimization, while inspecting the assembly reveals that optimization still occurs when the address is not accessed.

## Source excerpt

Rust avoids memory copies by optimizing return value placement.