官术网_书友最值得收藏!

How it works...

Essentially, being a kind of vector, a string can be created the same way by combining new and push; however, because this is really inconvenient, a string, which is an owned chunk of memory, can be created from a string slice (&str), which is either a borrowed string or a literal. Both of the ways to do it, that are shown in this recipe, are equivalent:

    let s = "Hello".to_string();
println!("s: {}", s);
let s = String::from("Hello");
println!("s: {}", s);

Out of pure personal preference, we will use the first variant.

Before Rust 1.9, to_owned() was the fastest way to create a string. Now, to_string() is equally performant and should be preferred, because it offers more clarity over what is done. We mention this because many old tutorials and guides have not been updated since then, and still use to_owned().

All strings in Rust are valid Unicode in UTF-8 encoding. This can lead to some surprises, as a character, as we know it, is an inherently Latin invention. For instance, look at languages that have a modifier for a letter—is ? an own character, or is it merely a variation of a? What about languages that allow many combinations in extreme? What would that keyboard even look like? For this reason, Unicode lets you compose your characters from different Unicode scalar values. With .chars(), you can create an iterator that goes through these scalars [28]. If you work with non-Latin characters, you might get surprised by this when accessing composing characters —y? is not one, but two scalars, y and ? [36]. You can get around this by using the Unicode-segmentation crate, which supports iteration over graphemes: https://crates.io/crates/unicode-segmentation.

When splitting a string on a pattern that is at the beginning, is at the end, or occurs multiple times after each other, each instance gets split into an empty string ""[107]. This is especially nasty when splitting on spaces (' '). In this case, you should use split_whitespace instead [110]. Otherwise, split_terminator will remove the empty strings from the end of the string [68].

By the way, when we talk about a pattern in this recipe, we mean one of three things:
- A character
- A string
- A predicate that takes one char
主站蜘蛛池模板: 界首市| 共和县| 白玉县| 图们市| 庆元县| 泸水县| 安康市| 安仁县| 萝北县| 海原县| 永定县| 织金县| 绍兴市| 观塘区| 民权县| 基隆市| 中阳县| 高州市| 泽普县| 娱乐| 石首市| 安龙县| 余庆县| 高邑县| 灌云县| 沂水县| 获嘉县| 仁怀市| 宜宾县| 昭平县| 溆浦县| 界首市| 新巴尔虎右旗| 涪陵区| 施秉县| 台东市| 隆德县| 定日县| 普定县| 玉环县| 额尔古纳市|