Rust Tip - Into String as Function Arguments @JeremyChone
Rust Tip - Into String as Function Arguments  @JeremyChone
Uploaded November 2022 | Updated November 2026, 3 hours ago
Here is a quick Rust Programming tutorial showing how to type function arguments to accept &str, &String, and String (which will be moved). This is useful when the body of the function needs the full String no matter what is passed.

Help support this channel: patreon.com/jeremychone

- When &str or &String is passed as an argument, new string allocation occurs on .into()
- When String is passed, no new allocation occurs on .into(). The String is moved to the function.

So, in a way, this technique gives allocation control to the caller.

Notes for this pattern:

1. Do not use this pattern if the function body does not need to own the String, and can get away &String or &str. Use &str in this case.

2. Some might think this pattern makes necessary allocation, but this is not the case (see video and points above). It is allocated only in the case of &str or &String are passed (which would have been anyway), and not if String is passed. The caaller has the control.

3 Downsides:

3.1 Binary size: As noted in a great comment from @JeffHanke below, the compiler will duplicate the function for each unique type passed (monomorphization). See (and vote) the comment for the binary size mitigation. For most app code, the cost should be negligible, moreover, if mitigated, compared to the ergonomic value (watch out for compiler inlining, but this can be mitigated as well). For embedded programming, this needs a little bit more scrutiny, as many other aspects of the code design anyway.

3.2 Function ergonomics could be a discussion, but it is subjective. I personally like this pattern as the method signature is clear, flexible, and efficient, as allocation will only occur if needed. But do what works for you and your team.

3.3 Unnecessary discussions about unnecessary allocations (see point 2 above).

Related external resources:

- philipdaniels.com/blog/2019/rust-api-design (Scroll down to Into section)

- rust-lang.github.io/api-guidelines/flexibility.html#caller-decides-where-to-copy-and-place-data-c-caller-control (more generic but related "caller-decides" topic, see generic paragraphs)

Related external resources:

- philipdaniels.com/blog/2019/rust-api-design (Scroll down to Into section)

- rust-lang.github.io/api-guidelines/flexibility.html#caller-decides-where-to-copy-and-place-data-c-caller-control (more generic but related "caller-decides" topic, see generic paragraphs)


Jeremy Chone:

- Twitter - twitter.com/jeremychone
- Discord On Rust - discord.gg/W2besKCzjx
- AWESOME-APP - https://awesomeapp.dev - Rust Templates for building awesome applications.
- Patreon - patreon.com/jeremychone - Any help is a big help (for Rust educational content)


Other popular Rust Programming videos:

- Quick Start Code Layout - youtube.com/watch?v=oxx7MmN4Ib0&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q
- AWESOME-APP Full Overview - Rust template for building Awesome Desktop Application: youtube.com/watch?v=BY_ZjPGqJJk&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q
- Tauri Quick Introduction (Desktop App wit Rust Programming): youtube.com/watch?v=kRoGYgAuZQE&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q
- Rust Web App tutorials series: youtube.com/watch?v=VIig9IcQ-w8&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q&list=PL7r-PXl6ZPcCLvwpdD2Vj1O4CyoFTiHKd
- Rust Bevy Full Tutorial - Game Development with Rust: youtube.com/watch?v=j7qHwb7geIM&list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q
- Rust for Java Developers series: youtube.com/watch?v=iFdh4sPC5Tg&list=PL7r-PXl6ZPcD63DS2djSiz4SlXkaTfobc&index=2&t=654s

Playlists:

- Rust For Desktop App: youtube.com/playlist?list=PL7r-PXl6ZPcAQAi1lfVrtHCSbaJbwZ4O8
- Everything Rust Programming - Tutorials, Courses, Tips, Examples: youtube.com/playlist?list=PL7r-PXl6ZPcCIOFaL7nVHXZvBmHNhrh_Q
- Rust Programming for Web Development: youtube.com/playlist?list=PL7r-PXl6ZPcCLvwpdD2Vj1O4CyoFTiHKd
- Rust Courses: youtube.com/playlist?list=PL7r-PXl6ZPcB4jn1_VR3D8tSK9DxOaiQE
- Furst for Java Developers: youtube.com/playlist?list=PL7r-PXl6ZPcD63DS2djSiz4SlXkaTfobc

AWESOME-APP ➜ https://awesomeapp.dev - Rust Templates for building awesome applications.

Rust AWESOME-APP GitHub - github.com/orgs/rust-awesome-app/repositories

Other notes:

- Tool used to do the green lines. ScreenBrush on Mac App Store (Gromit seems to be the equivalent on Linux)
- Edited with Davinci Resolve.

#rustprogramming #tutorial #rustlangn
Rust Tip - Into String as Function ArgumentsRust Lang Tutorial - if, else, for loopsRust Channels - Patterns and Best PracticesAnimating Custom Charts 2021 - #FrameworkLess - 20 lines code - Native Web ComponentsNative Web Components Dialog Box - Part 2 (bonus: multi-touch drag)JC Rust Live ➜ Quick Overview new LET-CHAIN Rust featureRust Stack vs. Heap ExplainedReleasing a Rust Library Live - Casual Coding5 Steps to Learn Rust ProgrammingRust BLAKE3 – Hashing at Speed for Large Files & Web TokensRust - spawn_block optimization for Argon 2Rust Lang Tutorial 2021 - Functions
Jeremy Chone |

Rust Tip - Into String as Function Arguments

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER