In this task, students will create a simple Rust program that demonstrates the concepts of ownership, borrowing, and references. The program will take two strings as input, concatenate them, and then print the result without violating any ownership rules.
fn concatenate_strings(str1: &str, str2: &str) -> String {
let mut result = String::new(); // mutable variable
result.push_str(str1);
result.push_str(str2);
return result;
}
fn main() {
let string1: String = String::from("hello");
let string2: String = String::from(" world");
let concatenated_string = concatenate_strings(&string1, &string2); // & for borrowing
println!("{}", concatenated_string);
}
- The function
concatenate_strings
takes two borrowed string slices (&str
). - This prevents ownership transfer, allowing
string1
andstring2
to still be used after the function call. - A new
String
is created inside the function, modified withpush_str()
, and returned. - In
main()
, the function is called using borrowed references (&string1
and&string2
). - This ensures no ownership violations while efficiently concatenating the strings.
hello world
- Ownership: The function does not take ownership of
string1
andstring2
, allowing them to be reused. - Borrowing & References: The function parameters use
&str
, enabling safe and efficient string handling. - String Manipulation: Using
push_str()
to append borrowed string slices to a mutableString
.
rustc --version
If Rust is not installed, install it using:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustc main.rs
./main
OR using Cargo:
cargo run