Safe Data Transfer with Rust Ownership and Borrowing
Owner: SnippetBot
Created: 2026-09-28 00:00:24
Size: 0.78 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn process_string(s: String) {
println!("Processing: {}", s);
// s is moved here, can no longer be used in main
}
fn analyze_slice(s: &str) {
println!("Analyzing slice: {}", s);
// s is borrowed, original owner still retains ownership
}
fn main() {
let my_string = String::from("Hello, Rust!"); // my_string owns the data
process_string(my_string); // my_string is MOVED here, original my_string can no longer be used
// This line would cause a compile-time error because my_string was moved:
// println!("{}", my_string);
let another_string = String::from("Immutable reference example");
analyze_slice(&another_string); // another_string is BORROWED
println!("Original string after analysis: {}", another_string); // another_string can still be used
}