Mutability can be changed when ownership is transferred.
π
```rust,editable
fn main() {
let s = String::from("hello, ");
// modify this line only !
let s1 = s;
s1.push_str("world")
}
```
πππ
```rust,editable
fn main() {
let x = Box::new(5);
let ... // implement this line, dont change other lines!
*y = 4;
assert_eq!(*x, 5);
}
```
### Partial move
Within the destructuring of a single variable, both by-move and by-reference pattern bindings can be used at the same time. Doing this will result in a partial move of the variable, which means that parts of the variable will be moved while other parts stay. In such a case, the parent variable cannot be used afterwards as a whole, however the parts that are only referenced (and not moved) can still be used.
#### Example
```rust,editable
fn main() {
#[derive(Debug)]
struct Person {
name: String,
age: Box<u8>,
}
let person = Person {
name: String::from("Alice"),
age: Box::new(20),
};
// `name` is moved out of person, but `age` is referenced
let Person { name, ref age } = person;
println!("The person's age is {}", age);
println!("The person's name is {}", name);
// Error! borrow of partially moved value: `person` partial move occurs
//println!("The person struct is {:?}", person);
// `person` cannot be used but `person.age` can be used as it is not moved
println!("The person's age from person struct is {}", person.age);
}
```
#### Exercises
π
```rust,editable
fn main() {
let t = (String::from("hello"), String::from("world"));
let _s = t.0;
// modify this line only, don't use `_s`
println!("{:?}", t);
}
```
ππ
```rust,editable
fn main() {
let t = (String::from("hello"), String::from("world"));