rust-by-practice/solutions/collections/String.md

114 lines
2.4 KiB
Markdown

1.
```rust
fn main() {
let mut s: String = String::from("hello, ");
s.push_str("world");
s.push('!');
move_ownership(s.clone());
assert_eq!(s, "hello, world!");
println!("Success!")
}
fn move_ownership(s: String) {
println!("ownership of \"{}\" is moved here!", s)
}
```
```rust
fn main() {
let mut s: String = String::from("hello, ");
s.push_str("world");
s.push('!');
borrow_string(&s);
assert_eq!(s, "hello, world!");
println!("Success!")
}
fn borrow_string(s: &str) {
println!("ownership of \"{}\" is moved here!", s)
}
```
2.
```rust
// FILL in the blanks
fn main() {
// get a slice of String with reference: String -> &str
let mut s = String::from("hello, world");
let slice1: &str = &s; // in two ways
assert_eq!(slice1, "hello, world");
let slice2 = &s[0..5];
assert_eq!(slice2, "hello");
//Note! The type here cant be `&mut str` due to `push` is ONLY defined on String type and its mut reference: `&mut String` !
// So you can't use `s.as_mut_str()`
let slice3: &mut String = &mut s;
slice3.push('!');
assert_eq!(slice3, "hello, world!");
println!("Success!")
}
```
```rust
fn main() {
let mut s = String::from("hello, world");
let slice1: &str = s.as_str();
assert_eq!(slice1, "hello, world");
let slice2 = &s[0..5];
assert_eq!(slice2, "hello");
//Note! The type here cant be `&mut str` due to `push` is ONLY defined on String type and its mut reference: `&mut String` !
// So you can't use `s.as_mut_str()`
let slice3: &mut String = &mut s;
slice3.push('!');
assert_eq!(slice3, "hello, world!");
println!("Success!")
}
```
2.
```rust
fn main() {
let s = String::from("hello, 世界");
let slice1 = &s[0..1]; //modify this line to fix the error, tips: `h` only takes 1 byte in UTF8 format
assert_eq!(slice1, "h");
let slice2 = &s[7..10];//modify this line to fix the error, tips: `中` takes 3 bytes in UTF8 format
assert_eq!(slice2, "世");
println!("Success!")
}
```
3.
```rust
// FILL in the blanks
fn main() {
let mut s = String::new();
s.push_str("hello");
// some bytes, in a vector
let v = vec![104, 101, 108, 108, 111];
// Turn a bytes vector into a String
// We know these bytes are valid, so we'll use `unwrap()`.
let s1 = String::from_utf8(v).unwrap();
assert_eq!(s, s1);
println!("Success!")
}
```