Ownership
- 🌟🌟
2. fn main() {
3. // Use as many approaches as you can to make it work
4. let x = String::from("Hello world");
5. let y = x;
6. println!("{}, {}",x, y);
7. }
- 🌟🌟
1. // Don't modify code in main!
2. fn main() {
3. let s1 = String::from("Hello world");
4. let s2 = take_ownership(s1);
6. println!("{}", s2);
7. }
9. // Only modify the code below!
10. fn take_ownership(s: String) {
11. println!("{}", s);
12. }
- 🌟🌟
2. fn main() {
3. let s = give_ownership();
4. println!("{}", s);
5. }
7. // Only modify the code below!
8. fn give_ownership() -> String {
9. let s = String::from("Hello world");
10. // Convert String to Vec
11. let _s = s.into_bytes();
12. s
13. }
- 🌟🌟
1. // Fix the error without removing any code
2. fn main() {
3. let s = String::from("Hello World");
5. print_str(s);
7. println!("{}", s);
8. }
10. fn print_str(s: String) {
11. println!("{}",s)
12. }
- 🌟🌟
1. // Don't use clone ,use copy instead
2. fn main() {
3. let x = (1, 2, (), "hello".to_string());
4. let y = x.clone();
5. println!("{:?}, {:?}", x, y);
6. }
Mutability
Mutability can be changed when ownership is transferred.
- 🌟
2. // make the necessary variable mutable
3. fn main() {
4. let s = String::from("Hello ");
6. let s1 = s;
8. s1.push_str("World!");
10. println!("Success!");
11. }
- 🌟🌟🌟
2. fn main() {
3. let x = Box::new(5);
5. let ... // update this line, don't change other lines!
7. *y = 4;
9. assert_eq!(*x, 5);
11. println!("Success!");
12. }
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
2. fn main() {
3. #[derive(Debug)]
4. struct Person {
5. name: String,
6. age: Box<u8>,
7. }
9. let person = Person {
10. name: String::from("Alice"),
11. age: Box::new(20),
12. };
14. // `name` is moved out of person, but `age` is referenced
15. let Person { name, ref age } = person;
17. println!("The person's age is {}", age);
19. println!("The person's name is {}", name);
21. // Error! borrow of partially moved value: `person` partial move occurs
22. //println!("The person struct is {:?}", person);
24. // `person` cannot be used but `person.age` can be used as it is not moved
25. println!("The person's age from person struct is {}", person.age);
26. }
Exercises
- 🌟
2. fn main() {
3. let t = (String::from("hello"), String::from("world"));
5. let _s = t.0;
7. // Modify this line only, don't use `_s`
8. println!("{:?}", t);
9. }
- 🌟🌟
2. fn main() {
3. let t = (String::from("hello"), String::from("world"));
5. // Fill the blanks
6. let (__, __) = __;
8. println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world")
9. }
You can find the solutions here(under the solutions path), but only use it when you need it
