char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Restructure repo to use `tools/ui` folder and `ui` / `UI` / `llama-ui` / `LLAMA_UI` naming (#23064)59778f019

char/aliceai-t5-moe
3.1 KiB125 linesraw
1// Blog Post Content
2export const BLOG_POST_MD = String.raw`
3# Understanding Rust's Ownership System
4
5*Published on March 15, 2024 • 8 min read*
6
7Rust's ownership system is one of its most distinctive features, enabling memory safety without garbage collection. In this post, we'll explore how ownership works and why it's revolutionary for systems programming.
8
9## What is Ownership?
10
11Ownership is a set of rules that governs how Rust manages memory. These rules are checked at compile time, ensuring memory safety without runtime overhead.
12
13### The Three Rules of Ownership
14
151. **Each value has a single owner**
162. **There can only be one owner at a time**
173. **When the owner goes out of scope, the value is dropped**
18
19## Memory Management Without GC
20
21Traditional approaches to memory management:
22
23- **Manual management** (C/C++): Error-prone, leads to bugs
24- **Garbage collection** (Java, Python): Runtime overhead
25- **Ownership** (Rust): Compile-time safety, zero runtime cost
26
27## Basic Examples
28
29### Variable Scope
30
31${'```'}rust
32fn main() {
33    let s = String::from("hello");  // s comes into scope
34    
35    // s is valid here
36    println!("{}", s);
37    
38}  // s goes out of scope and is dropped
39${'```'}
40
41### Move Semantics
42
43${'```'}rust
44fn main() {
45    let s1 = String::from("hello");
46    let s2 = s1;  // s1 is moved to s2
47    
48    // println!("{}", s1);  // ❌ ERROR: s1 is no longer valid
49    println!("{}", s2);     // ✅ OK: s2 owns the string
50}
51${'```'}
52
53## Borrowing and References
54
55Instead of transferring ownership, you can **borrow** values:
56
57### Immutable References
58
59${'```'}rust
60fn calculate_length(s: &String) -> usize {
61    s.len()  // s is a reference, doesn't own the String
62}
63
64fn main() {
65    let s1 = String::from("hello");
66    let len = calculate_length(&s1);  // Borrow s1
67    println!("Length of '{}' is {}", s1, len);  // s1 still valid
68}
69${'```'}
70
71### Mutable References
72
73${'```'}rust
74fn main() {
75    let mut s = String::from("hello");
76    
77    let r1 = &mut s;
78    r1.push_str(", world");
79    println!("{}", r1);
80    
81    // let r2 = &mut s;  // ❌ ERROR: cannot borrow twice
82}
83${'```'}
84
85## Common Pitfalls
86
87### Dangling References
88
89${'```'}rust
90fn dangle() -> &String {  // ❌ ERROR: missing lifetime specifier
91    let s = String::from("hello");
92    &s  // s will be dropped, leaving a dangling reference
93}
94${'```'}
95
96### ✅ Solution
97
98${'```'}rust
99fn no_dangle() -> String {
100    let s = String::from("hello");
101    s  // Ownership is moved out
102}
103${'```'}
104
105## Benefits
106
107- ✅ **No null pointer dereferences**
108- ✅ **No data races**
109- ✅ **No use-after-free**
110- ✅ **No memory leaks**
111
112## Conclusion
113
114Rust's ownership system eliminates entire classes of bugs at compile time. While it has a learning curve, the benefits in safety and performance are worth it.
115
116## Further Reading
117
118- [The Rust Book - Ownership](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html)
119- [Rust by Example - Ownership](https://doc.rust-lang.org/rust-by-example/scope/move.html)
120- [Rustlings Exercises](https://github.com/rust-lang/rustlings)
121
122---
123
124*Questions? Reach out on [Twitter](https://twitter.com/rustlang) or join the [Rust Discord](https://discord.gg/rust-lang)*
125`;