-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborrowing_examples.rs
More file actions
70 lines (53 loc) · 2.96 KB
/
borrowing_examples.rs
File metadata and controls
70 lines (53 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! borrowing_examples.rs
//!
//! Three clear examples of borrowing in Rust based on section 5 of
//! Rust-Ownership-Basic.md — "Borrowing — Lending Without Giving Up Ownership".
//! Demonstrates immutable borrowing, multiple borrows, and practical usage.
// Example 1 helper function
fn calculate_length(s: &String) -> usize {
s.len()
}
// Example 2 helper functions
fn count_words(text: &str) -> usize {
text.split_whitespace().count()
}
fn count_chars(text: &str) -> usize {
text.chars().count()
}
// Example 3: A simple struct to demonstrate borrowing
struct Person {
name: String,
age: i32,
}
fn get_person_summary(person: &Person) -> String {
format!("{} is {} years old.", person.name, person.age)
}
fn main() {
// ──────────────────────────────────────────────────────────────
// Example 1: Basic immutable borrowing (from Rust-Ownership-Basic.md)
// ──────────────────────────────────────────────────────────────
let s1: String = String::from("hello");
let len: usize = calculate_length(&s1); // lend s1 to the function
println!("Example 1 - The length of '{}' is {}.", s1, len);
// s1 is still valid here — we only borrowed it
// ──────────────────────────────────────────────────────────────
// Example 2: Multiple immutable borrows (allowed simultaneously)
// ──────────────────────────────────────────────────────────────
let message: String = String::from("Rust borrowing is safe!");
let word_count: usize = count_words(&message);
let char_count: usize = count_chars(&message);
println!("\nExample 2 - Message: {}", message);
println!(" Words: {}", word_count);
println!(" Characters: {}", char_count);
// message is still fully owned by main()
// ──────────────────────────────────────────────────────────────
// Example 3: Borrowing a struct (practical real-world pattern)
// ──────────────────────────────────────────────────────────────
let person: Person = Person {
name: String::from("Alice"),
age: 32,
};
let summary: String = get_person_summary(&person);
println!("\nExample 3 - {}", summary);
println!("Person still exists: {} is {} years old.", person.name, person.age);
}