use std::io;
// read_line会读入整行字符串,并保留行末的换行符
io::stdin()
.read_line(&mut str)
.expect("");
String::from("something") // 构造String
string.trim() // 去除字符串首尾空白字符,类似python的strip()
string.parse() // 字符串转数字
string.push_str("something") // string后接新的串
println!("{}", string) // 可以格式化打印
string.as_bytes() // 返回一个可迭代的字符数组
可以从字符串中取一段索引,它实际上是一种指向一个字符串的immutable reference.
str[0..2]
str[..2]
str[..]
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
fn main() {
let mut s = String::from("hello world");
let word = first_word(&s[..]);
s.clear(); // error!
println!("the first word is: {}", word);
}