-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborrowing_library.rs
More file actions
477 lines (399 loc) · 18.2 KB
/
borrowing_library.rs
File metadata and controls
477 lines (399 loc) · 18.2 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! borrowing_library.rs
//!
//! A realistic library catalog system demonstrating Rust borrowing.
//!
//! Concepts covered (from Ownership-Basic.md sections 5, 6, 7):
//! Section 5 — Borrowing: passing &T to read data without taking ownership
//! Section 6 — Mutable references: &mut T for modification, exclusivity rule
//! Section 7 — &str vs &String: using &str in function signatures for flexibility
//!
//! Each function has a single responsibility.
//! main() calls all functions and is placed at the bottom.
//!
//! Compile: rustc borrowing_library.rs && ./borrowing_library
// ── Data types ────────────────────────────────────────────────────────────────
#[derive(Debug)]
struct Book {
title: String,
author: String,
year: u32,
available: bool,
checkouts: u32,
}
#[derive(Debug)]
struct Member {
name: String,
checked_out: Vec<String>,
total_borrowed: u32,
}
struct Catalog {
books: Vec<Book>,
members: Vec<Member>,
}
// ── Construction helpers ──────────────────────────────────────────────────────
/// Create a new Book. Takes &str — flexible, no ownership required.
fn new_book(title: &str, author: &str, year: u32) -> Book {
Book {
title: title.to_string(),
author: author.to_string(),
year,
available: true,
checkouts: 0,
}
}
/// Create a new Member. Takes &str (Section 7).
fn new_member(name: &str) -> Member {
Member {
name: name.to_string(),
checked_out: Vec::new(),
total_borrowed: 0,
}
}
// ── Section 5: Immutable borrows — &T ────────────────────────────────────────
/// Print one book's details.
/// Takes &Book — borrows it, caller keeps ownership, book is valid after this call.
fn print_book(book: &Book) {
let status = if book.available { "Available" } else { "Checked out" };
println!(
" [{status}] \"{title}\" by {author} ({year}) — borrowed {checkouts}x",
title = book.title,
author = book.author,
year = book.year,
checkouts = book.checkouts,
);
}
/// Print the full catalog.
/// Takes &Catalog — read-only borrow of the whole structure.
fn print_catalog(catalog: &Catalog) {
println!("\n── Catalog ({} books) ──────────────────────", catalog.books.len());
for book in &catalog.books { // &catalog.books yields &[Book]; each element is &Book
print_book(book);
}
}
/// Print all members.
/// Takes &[Member] — a borrowed slice; works with &Vec<Member> via deref coercion.
fn print_members(members: &[Member]) {
println!("\n── Members ({} registered) ─────────────────", members.len());
for member in members {
if member.checked_out.is_empty() {
println!(" {} — nothing checked out", member.name);
} else {
println!(" {} — has: {}", member.name, member.checked_out.join(", "));
}
}
}
/// Count available books.
/// Takes &Catalog — just reads, no mutation needed.
fn count_available(catalog: &Catalog) -> usize {
catalog.books.iter().filter(|b| b.available).count()
}
/// Count a member's current loans.
/// Takes &Member — borrows, does not consume.
fn count_checked_out(member: &Member) -> usize {
member.checked_out.len()
}
/// Build a one-line summary string.
/// Borrows &Catalog — reads everything it needs.
fn catalog_summary(catalog: &Catalog) -> String {
let available = count_available(catalog);
let checked_out = catalog.books.len() - available;
format!(
"{} total | {} available | {} checked out | {} members",
catalog.books.len(),
available,
checked_out,
catalog.members.len(),
)
}
/// Find a book by exact title.
/// Takes &str — accepts both literals and &String (Section 7).
/// Returns Option<&Book> — a borrowed reference into the catalog.
/// The lifetime of the returned &Book cannot outlive the &Catalog borrow.
fn find_book<'a>(catalog: &'a Catalog, title: &str) -> Option<&'a Book> {
catalog.books.iter().find(|b| b.title == title)
}
/// Find a member by name.
/// Returns Option<&Member> — borrowed, not owned.
fn find_member<'a>(catalog: &'a Catalog, name: &str) -> Option<&'a Member> {
catalog.members.iter().find(|m| m.name == name)
}
/// Collect all books by a given author.
/// &str parameter (Section 7) — accepts literals and &String equally.
/// Returns Vec<&Book> — a collection of borrowed references; no book data is copied.
fn books_by_author<'a>(catalog: &'a Catalog, author: &str) -> Vec<&'a Book> {
catalog.books.iter().filter(|b| b.author == author).collect()
}
/// Case-insensitive title search.
/// Both parameters &str — no allocation, no ownership transfer.
fn title_matches(title: &str, query: &str) -> bool {
title.to_lowercase().contains(&query.to_lowercase())
}
/// Search catalog by partial title.
/// &str query works whether it comes from a literal or a String variable (Section 7).
fn search_by_title<'a>(catalog: &'a Catalog, query: &str) -> Vec<&'a Book> {
catalog.books.iter().filter(|b| title_matches(&b.title, query)).collect()
}
/// Find the most-borrowed book.
/// Returns a borrowed reference — no data copied, no ownership moved.
fn most_borrowed(catalog: &Catalog) -> Option<&Book> {
catalog
.books
.iter()
.max_by_key(|b| b.checkouts)
.filter(|b| b.checkouts > 0)
}
/// Generate a loan receipt string for a member.
/// Takes &Member — borrows to read, caller keeps the Member.
fn generate_loan_receipt(member: &Member) -> String {
if member.checked_out.is_empty() {
return format!("{} has no books checked out.", member.name);
}
let mut receipt = format!("Loan receipt — {}:\n", member.name);
for (i, title) in member.checked_out.iter().enumerate() {
receipt.push_str(&format!(" {}. {}\n", i + 1, title));
}
receipt.push_str(&format!(
" Checked out: {} | All-time borrowed: {}",
member.checked_out.len(),
member.total_borrowed,
));
receipt
}
// ── Section 7: &str utility functions ────────────────────────────────────────
/// Format a display label for a book.
/// Both parameters &str — works with literals and &String (Section 7).
fn format_book_label(title: &str, author: &str) -> String {
format!("\"{}\" — {}", title, author)
}
/// Print a section header.
/// &str — flexible, zero allocation at the call site.
fn print_header(label: &str) {
println!("\n╔══ {} ", label);
}
// ── Section 6: Mutable borrows — &mut T ──────────────────────────────────────
/// Check out a book to a member.
/// Takes &mut Catalog — exclusive mutable access.
/// While this call is running, no other borrow of catalog can exist.
fn checkout_book(catalog: &mut Catalog, member_name: &str, title: &str) -> Result<(), String> {
let book_idx = catalog.books.iter().position(|b| b.title == title);
let Some(book_idx) = book_idx else {
return Err(format!("Book not found: \"{}\"", title));
};
if !catalog.books[book_idx].available {
return Err(format!("\"{}\" is already checked out", title));
}
let member_idx = catalog.members.iter().position(|m| m.name == member_name);
let Some(member_idx) = member_idx else {
return Err(format!("Member not found: \"{}\"", member_name));
};
// Mutate book — exclusive mutable borrow in effect here
catalog.books[book_idx].available = false;
catalog.books[book_idx].checkouts += 1;
// Clone the title string so we can store it in the member record
let title_copy = catalog.books[book_idx].title.clone();
// Mutate member record
catalog.members[member_idx].checked_out.push(title_copy);
catalog.members[member_idx].total_borrowed += 1;
println!(
" ✓ \"{}\" → {}",
catalog.books[book_idx].title,
catalog.members[member_idx].name,
);
Ok(())
}
/// Return a book.
/// Takes &mut Catalog — needs to update both book and member record.
fn return_book(catalog: &mut Catalog, member_name: &str, title: &str) -> Result<(), String> {
let book_idx = catalog.books.iter().position(|b| b.title == title);
let Some(book_idx) = book_idx else {
return Err(format!("Book not found: \"{}\"", title));
};
if catalog.books[book_idx].available {
return Err(format!("\"{}\" is not checked out", title));
}
let member_idx = catalog.members.iter().position(|m| m.name == member_name);
let Some(member_idx) = member_idx else {
return Err(format!("Member not found: \"{}\"", member_name));
};
let has_it = catalog.members[member_idx].checked_out.iter().any(|t| t == title);
if !has_it {
return Err(format!("{} does not have \"{}\"", member_name, title));
}
// Mutate — exclusive &mut borrow
catalog.books[book_idx].available = true;
catalog.members[member_idx].checked_out.retain(|t| t != title);
println!(
" ✓ \"{}\" returned by {}",
title,
catalog.members[member_idx].name,
);
Ok(())
}
/// Add a new book to the catalog.
/// Takes &mut Catalog and &str params (Section 7 — flexible, no owned String required).
fn add_book(catalog: &mut Catalog, title: &str, author: &str, year: u32) {
let book = new_book(title, author, year);
println!(" + Added: \"{}\" by {}", book.title, book.author);
catalog.books.push(book);
}
/// Register a new member.
/// Takes &mut Catalog and &str for name (Section 7).
fn register_member(catalog: &mut Catalog, name: &str) {
let member = new_member(name);
println!(" + Member: {}", member.name);
catalog.members.push(member);
}
/// Mark a single book unavailable (e.g. for repair).
/// Takes &mut Book — mutable borrow of one book, not the whole catalog.
/// While this borrow lives, nothing else can read or write that Book.
fn mark_unavailable(book: &mut Book, reason: &str) {
book.available = false;
println!(" ! \"{}\" marked unavailable: {}", book.title, reason);
}
// ── Catalog construction ──────────────────────────────────────────────────────
fn build_catalog() -> Catalog {
Catalog {
books: vec![
new_book("The Rust Programming Language", "Steve Klabnik", 2019),
new_book("Programming Rust", "Jim Blandy", 2021),
new_book("Rust in Action", "Tim McNamara", 2021),
new_book("The C Programming Language", "Kernighan & Ritchie", 1988),
new_book("Clean Code", "Robert Martin", 2008),
new_book("The Pragmatic Programmer", "Hunt & Thomas", 2019),
],
members: vec![
new_member("Alice"),
new_member("Bob"),
new_member("Carol"),
],
}
}
// ── main — all functions called here ─────────────────────────────────────────
fn main() {
// catalog is owned by main() for the entire program.
// It is never moved — only borrowed (&) or mutably borrowed (&mut).
let mut catalog = build_catalog();
// ── Section 5: Immutable borrows ──────────────────────────────────────────
print_header("Initial State");
print_catalog(&catalog); // &Catalog — read-only borrow
print_members(&catalog.members); // &[Member] slice borrow
println!("\n {}", catalog_summary(&catalog));
// ── Section 7: &str vs &String ────────────────────────────────────────────
print_header("&str accepts both literals and &String");
// String literal — &str directly
println!(" {}", format_book_label("The Rust Programming Language", "Steve Klabnik"));
// &String — auto-deref coercion to &str (Section 7)
let owned_title = String::from("Programming Rust");
let owned_author = String::from("Jim Blandy");
println!(" {}", format_book_label(&owned_title, &owned_author));
// ── find_book — returns &Book borrowed from catalog ───────────────────────
print_header("find_book — borrowed reference");
if let Some(book) = find_book(&catalog, "Clean Code") {
print!(" Found: ");
print_book(book); // pass the &Book directly
}
if find_book(&catalog, "Missing Title").is_none() {
println!(" \"Missing Title\" — not in catalog");
}
// ── books_by_author — Vec of borrowed &Book references ───────────────────
print_header("books_by_author — &str param, Vec<&Book> result");
let rust_books = books_by_author(&catalog, "Tim McNamara");
println!(" Books by Tim McNamara:");
for book in &rust_books {
print_book(book);
}
// &String passed where &str expected — Section 7
let author = String::from("Jim Blandy");
let blandy_books = books_by_author(&catalog, &author);
println!(" Books by {}:", author);
for book in &blandy_books {
print_book(book);
}
// ── search_by_title — &str query ──────────────────────────────────────────
print_header("search_by_title — &str from literal and String");
let results = search_by_title(&catalog, "rust");
println!(" \"rust\" matches ({}):", results.len());
for book in &results {
print_book(book);
}
let owned_query = String::from("programming");
let results2 = search_by_title(&catalog, &owned_query); // &String → &str
println!(" \"{}\" matches ({}):", owned_query, results2.len());
for book in &results2 {
print_book(book);
}
// ── Section 6: Mutable borrows for checkout ───────────────────────────────
print_header("checkout_book — &mut Catalog (exclusive access)");
// Each call takes &mut catalog — exclusive while running, released after
let ops = [
("Alice", "Clean Code"),
("Alice", "Rust in Action"),
("Bob", "The Rust Programming Language"),
("Bob", "Clean Code"), // should fail — already out
("Carol", "Programming Rust"),
];
for (member, title) in &ops {
match checkout_book(&mut catalog, member, title) {
Ok(()) => {}
Err(msg) => println!(" ✗ {}", msg),
}
}
// &mut borrow ended — catalog is readable again via & borrows
print_catalog(&catalog);
// ── mark_unavailable — &mut Book (single book, not whole catalog) ─────────
print_header("mark_unavailable — &mut Book");
mark_unavailable(&mut catalog.books[3], "water damage");
println!(" Verified:");
print_book(&catalog.books[3]); // immutable borrow — fine now &mut is done
// ── Loan receipts — &Member ───────────────────────────────────────────────
print_header("generate_loan_receipt — &Member");
// find_member returns &Member — borrowed from catalog
for name in &["Alice", "Bob", "Carol"] {
if let Some(member) = find_member(&catalog, name) {
println!("\n{}", generate_loan_receipt(member));
}
}
// ── Return books — &mut Catalog ───────────────────────────────────────────
print_header("return_book — &mut Catalog");
let returns = [
("Alice", "Clean Code"),
("Bob", "The Rust Programming Language"),
("Alice", "Clean Code"), // should fail — already returned
("Carol", "Clean Code"), // should fail — Carol doesn't have it
];
for (member, title) in &returns {
match return_book(&mut catalog, member, title) {
Ok(()) => {}
Err(msg) => println!(" ✗ {}", msg),
}
}
// ── Add books and members — &mut Catalog, &str params ─────────────────────
print_header("add_book / register_member — &str params (Section 7)");
add_book(&mut catalog, "Zero To Production In Rust", "Luca Palmieri", 2022);
add_book(&mut catalog, "Rust for Rustaceans", "Jon Gjengset", 2021);
register_member(&mut catalog, "Dave");
// ── most_borrowed — immutable borrow after all mutations ──────────────────
print_header("most_borrowed — &Catalog after mutations");
match most_borrowed(&catalog) {
Some(book) => { print!(" Most borrowed: "); print_book(book); }
None => println!(" No books borrowed yet"),
}
// ── Final statistics — catalog still fully owned by main() ────────────────
print_header("Final Statistics");
println!(" Available: {}", count_available(&catalog));
println!(" Total books: {}", catalog.books.len());
for member in &catalog.members {
println!(
" {}: {} checked out | {} all-time",
member.name,
count_checked_out(member),
member.total_borrowed,
);
}
print_header("Final State");
print_catalog(&catalog);
print_members(&catalog.members);
println!("\n Summary: {}", catalog_summary(&catalog));
println!("\n catalog was owned by main() the entire time.");
println!(" It was never moved — only lent via & and &mut.");
}