• String + String -> String:
1
2
3
let s1 = "0";
let s2 = "1";
let s = format!("{}{}", s1, s2);
  • String + &str -> String:
1
2
3
let s1: &str;
let s: String;
s.push_str(s1);
  • &str + &str -> String:
1
2
3
4
let s1: &str;
let s2: &str;
let s = String::from_str(s1);
s.push_str(s2);
  • String -> &str:
1
2
3
let s: String;
let s1: &str = s.as_str();
let s2: &mut str = s.as_mut_str();
  • String -> &[u8]:
1
2
let s: String;
let s1: &[u8] = s.as_bytes();
  • String -> &mut Vec< u8 >
1
2
let s: String
let s1: &mut Vec<u8> = s.as_mut_vec();
  • &str -> String:
1
2
let s: &str;
let s1 = String::from_str(s);
  • &[u8] -> String:
1
2
3
let s: &[u8];
let s1 = String::from_utf8_lossy(s);
let s2 = String::from_utf8(s.to_vec()).unwrap();
  • Vec< u8 > -> String:
1
2
let s: Vec<u8>;
let s1 = String::from_utf8(s).unwrap();
  • 通过索引获取 String 中的 字符
1
2
let s: String;
let c = s.chars().nth(index).unwrap();
  • 遍历 String
1
2
3
4
let s: String;
for c in s.chars() {
//
}