Rust 從基礎(chǔ)到實踐(5)字符串

rust_logo.jpg

看一看國內(nèi)都誰在用 Rust,首先是就是字節(jié)跳動


batedance.png

字符串在Rust是一個對象特殊類型,所以單拿出來進行分享??梢詫⒆址私鉃閏har 的集合。

我們將給出stringRust的含義。Rust在核心語言中只有一種String類型,通過borrow的方式 &str 查看 str。。
string類型由 Rust的標準庫提供,而不是編碼為核心語言,是一種可增長、可變、擁有的、UTF-8 編碼的string 類型。當 Rustaceans 指的是Rust中的“string”時,通常指的是string和string slice&str類型,而不僅僅是這些類型中的一種。盡管這一部分主要是關(guān)于string 的,但這兩種類型在Rust的標準庫中都被大量使用,并且stringstring slice都是UTF-8編碼的。
Rust的標準庫還包括許多其他string類型,例如osstring、osstr、cstring 和 cstr??梢詾榇鎯?strong>string提供更多的選項??纯催@些類型基本都是以stringstr結(jié)尾的。這些類型指的是擁有借用的變體,和stringstr類型一樣。例如,這些字符串類型可以以不同的編碼存儲文本,或者以不同的方式在內(nèi)存中表示。

字符串類型由 Rust 的標準庫提供,而不是編碼為核心語言,是一種可增長、可變、擁有的、UTF-8 編碼的字符串類型.

字符串創(chuàng)建

let mut s = String::new();
let data = "initial contents";
let s = data.to_string();
  • 創(chuàng)建一個空的字符串(String)
  • 然后將數(shù)據(jù)加載到字符串中
let hello = String::from("Hello");

    println!("{}",hello);

也可以使用 String::from(數(shù)據(jù)) 來直接創(chuàng)建字符串。

字符串的追加內(nèi)容

let mut hello = String::from("Hello");

    println!("{}",hello);
    hello.push('W');
    println!("Length: {}", hello.len() )

字符串可以理解為字符集合,我們通過 push 為字符串添加字符,但是不可以使用 push 添加字符串。不然就會拋出下面的異常

 let mut hello = String::from("Hello");

    println!("{}",hello);
    hello.push('Wo');
    println!("Length: {}", hello.len() )
    //get length
error: character literal may only contain one codepoint: 'Wo'

我們可以使yong push_str 來為字符串添加字符串

let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s2 is {}", s2);

字符串重新賦值

hello.push_str("orld!");

如果要字符串重新賦值時候,如果將字符串 hello 修改可變類型,需要添加 mut

let mut hello = "Hello";

字符串的合并

let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; //

fn add(self, s: &str) -> String {

在 add 函數(shù)中 s2 有一個&,這意味著我們要將第二個string的引用添加到第一個string中,因為 add 函數(shù)中的 s 參數(shù):我們只能將a&str添加到字符串中;我們不能將兩個string進行合并的。 &s2的類型是&string,而不是&str。

能夠在合并字符串調(diào)用中使用 &s2 的原因是編譯器可以將&string參數(shù)強制為 &str。當我們調(diào)用 add 方法時,Rust使用一個 deref 強制,這里將&s2轉(zhuǎn)換為&s2[..]。因為 add不擁有s參數(shù)的所有權(quán)(ownership),所以s2在此操作之后仍然是有效的string。

第二,在合并字符串方法中拿 self 的所有權(quán),因為 self 沒有&。的 s1 將被移動到 add 調(diào)用中,s1 并且在此之后不再有效。因此,盡管let s3=s1+&s2;看起來add將復(fù)制兩個字符串并創(chuàng)建一個新的字符串,但此語句實際上擁有s1的所有權(quán),附加s2內(nèi)容的副本,然后返回結(jié)果的所有權(quán)。換句話說,看起來復(fù)制了很多副本,但是實際上并沒有;這樣做實現(xiàn)比復(fù)制效率更高。

fn main() {
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; 
println!("{} {}",s3,s1)
}
td::string::String
let s1 = String::from("Hello, ");
borrow of moved value: `s1`

value borrowed here after move

字符串的長度

assert_eq!(3, s.len());
屏幕快照 2019-03-09 下午3.53.06.png
let s1 = String::from("hello");
let h = s1[0];

在許多其他編程語言中,通過index可以訪問string中的單個字符,但是在Rust中的index 訪問字符串的某些部分,將會得到一個錯誤。

 let h = s1[0];
  |             ^^^^^ the type `std::string::String` cannot be indexed by `{integer}`

我們可以通過 slice 獲取想要的字符

let s1 = String::from("hello");
let h = &s1[0..1];
println!("first char {} ", h)

字符串的遍歷

for c in "您好".chars() {
        println!("{}", c);
    }

bytes)

for b in "您好".bytes() {
        println!("{}", b);
    }
230
130
168
229
165
189

為了夢想努力,邊學(xué)邊看一看這張圖片


th.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容