May 2019
Intermediate to advanced
698 pages
17h 21m
English
Rust's error diagnostics around the borrowing rules are really helpful when we go against the borrow checker. In the following few examples, we'll see them in various contexts.
Borrowing in functions: As you saw previously, moving ownership when making function calls does not make much sense if you are only reading the value, and is very limiting. You don't get to use the variable after you call the function. Instead of taking parameters by value, we can take them by references. We can fix the previous code example that was presented in the ownership section to pass the compiler without cloning, like so:
// borrowing_functions.rsfn take_the_n(n: &mut u8) { *n += 2;}fn take_the_s(s: &mut String) { s.push_str("ing");}fn ...Read now
Unlock full access