Borrowing and references
Course: Rust for Solana contracts - chapter 10
Rather than transferring ownership to a function, you can let the function borrow the variable
fn main() { let str = String::from("Harkirat"); let len = get_length(&str); println!("{} {}", str, len);}
fn get_length(str: &String) -> usize { let len = str.len(); return len}When you pass a variable by reference, the variable is still owned by the first function. It is only borrowed by the get_length function.
Rules of borrowing -
Section titled “Rules of borrowing -”- You can only have one immutable reference. If there is an immutable reference, there cant be other immutable or mutable references
- You can have multiple immutable references
- Will this code compile
fn main() { let str = String::from("Harkirat"); let ref1 = &str; let ref2 = &str;
println!("{} {}", ref1, ref2);}- Will this code compile
fn main() { let mut str = String::from("Harkirat"); let ref1 = &mut str; let ref2 = &str;
println!("{} {}", ref1, ref2);}- Will this code compile
fn main() { let mut str = String::from("Harkirat"); let ref1 = &mut str; ref1.push_str("Singh"); let ref2 = &str;
println!("{}", ref2);}Assignments
Section titled “Assignments”1. Borrowing an Immutable Reference
Section titled “1. Borrowing an Immutable Reference”Goal: Write a function calculate_length that takes an immutable reference to a String and returns its length. Then call this function from main and print both the original String and its length.
Hint
fn calculate_length(s: &String) -> usize { // ...}2. Borrowing a Mutable Reference
Section titled “2. Borrowing a Mutable Reference”Goal: Write a function append_text that takes a mutable reference to a String and appends some text to it. For example, if the string is "Hello", the function could append ", World!".
Hint
fn append_text(s: &mut String) { // ...}