- Rust Standard Library Cookbook
- Jan Nils Ferner Daniel Durante
- 388字
- 2021-08-27 19:45:01
How it works...
In all functions, we start by allocating memory for a string of variable length.
We do this by creating a string slice (&str) and applying the to_string function on it [8, 18 and 28].
The first way to concatenate strings in Rust, as shown in the by_moving function, is by taking said allocated memory and moving it, together with an additional string slice, into a new variable [12]. This has a couple of advantages:
- It's very straightforward and clear to look at, as it follows the common programming convention of concatenating with the + operator
- It uses only immutable data. Remember to always try to write code in a style that creates as little stateful behavior as possible, as it results in more robust and reusable code bases
- It reuses the memory allocated by hello [8], which makes it very performant
As such, this way of concatenating should be preferred whenever possible.
So, why would we even list other ways to concatenate strings? Well, I'm glad you asked, dear reader. Although elegant, this approach comes with two downsides:
- hello is no longer usable after line [12], as it was moved. This means you can no longer read it in any way
- Sometimes you may actually prefer mutable data in order to use state in small, contained environments
The two remaining functions address one concern each.
by_cloning[17] looks nearly identical to the first function, but it clones the allocated string [22] into a temporary object, allocating new memory in the process, which it then moves, leaving the original hello untouched and still accessible. Of course, this comes at the price of redundant memory allocations at runtime.
by_mutating[27] is the stateful way of solving our problem. It performs the involved memory management in-place, which means that the performance should be the same as in by_moving. In the end, it leaves hello mutable, ready for further changes. You may notice that this function doesn't look as elegant as the others, as it doesn't use the + operator. This is intentional, as Rust tries to push you through its design towards moving data in order to create new variables without mutating existing ones. As mentioned before, you should only do this if you really need mutable data or want to introduce state in a very small and manageable context.
- 雷達(dá)微波新技術(shù)
- 基于集成學(xué)習(xí)的角反射體目標(biāo)雷達(dá)識(shí)別理論與方法
- 通信系統(tǒng)仿真設(shè)計(jì)與應(yīng)用
- 太赫茲超表面的理論基礎(chǔ)與應(yīng)用
- 輕松學(xué)iPhone開(kāi)發(fā)
- 探地雷達(dá)雙曲波智能提取與可視化
- 2018—2019年中國(guó)無(wú)線(xiàn)電應(yīng)用與管理藍(lán)皮書(shū)
- SMT工藝與PCB制造(電子技術(shù)輕松學(xué))
- 5G無(wú)線(xiàn)系統(tǒng)設(shè)計(jì)與國(guó)際標(biāo)準(zhǔn)
- 手機(jī)故障維修技巧與實(shí)例
- 創(chuàng)客電子制作實(shí)例精選:36個(gè)趣味電子DIY項(xiàng)目
- 雷達(dá)數(shù)據(jù)處理及應(yīng)用
- 電子裝配工藝實(shí)訓(xùn):項(xiàng)目教程
- Cloud-Native Applications in Java
- 射頻與微波晶體管功率放大器工程