đĻ Rust Ownership āĻāĻŦāĻ Borrowing
A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.
Rust āĻĒā§āϰā§āĻā§āϰāĻžāĻŽāĻŋāĻ āϞā§āϝāĻžāĻā§āĻā§ā§ā§āĻāĻā§ āĻŦāϞāĻž āĻšā§ memory-safe, fast, āĻāĻŦāĻ modernāĨ¤ āĻāϰ āϏāĻŦāĻā§ā§ā§ āĻŦā§ā§ āĻŦā§āĻļāĻŋāώā§āĻā§āϝ āĻšāϞ⧠Ownership SystemāĨ¤ Rust-āĻ garbage collector āύā§āĻ, āĻāĻŦāĻžāϰ manual memory free āĻāϰāĻžāϰāĻ āĻĻāϰāĻāĻžāϰ āύā§āĻāĨ¤
đ āĻŦāϰāĻ Rust Ownership āĻāĻŦāĻ Borrowing Rules āĻĻāĻŋā§ā§ compile-time-āĻ āĻŽā§āĻŽā§āϰāĻŋ āϏā§āĻĢāĻāĻŋ āύāĻŋāĻļā§āĻāĻŋāϤ āĻāϰā§āĨ¤
āĻāĻāĻā§ āĻāĻŽāϰāĻž āĻļāĻŋāĻāĻŦā§ â
Ownership āĻāĻŋ?
Borrowing āĻāĻŋ?
āĻāĻ āϏāĻŋāϏā§āĻā§āĻŽ āĻā§āύ āĻĻāϰāĻāĻžāϰ?
Memory Level-āĻ āĻāĻŋāĻāĻžāĻŦā§ āĻāĻžāĻ āĻāϰā§?
Real-world problem āĻāĻŋāĻāĻžāĻŦā§ solve āĻšā§?
ā§§. Ownership āĻā§?
Rust-āĻ āĻĒā§āϰāϤāĻŋāĻāĻŋ value-āĻāϰ āĻāĻāĻāĻŋ Owner āĻĨāĻžāĻā§āĨ¤ Owner āĻŦāϞāϤ⧠āĻŦā§āĻā§, āϝ⧠variable āϏā§āĻ value-āϰ āĻŽāĻžāϞāĻŋāĻāĨ¤
āĻāϰ Ownership rules āĻšāϞā§:
āĻĒā§āϰāϤāĻŋāĻāĻŋ value-āĻāϰ āĻāĻāĻāύ owner āĻĨāĻžāĻāĻŦā§
āĻāĻ āϏāĻŽā§ā§ āĻāĻāĻāύ owner-āĻ āĻĨāĻžāĻāϤ⧠āĻĒāĻžāϰāĻŦā§
Owner scope-āĻāϰ āĻŦāĻžāĻāϰ⧠āĻā§āϞ⧠value āĻŽā§āĻŽā§āϰāĻŋ āĻĨā§āĻā§ āĻĢā§āϰāĻŋ āĻšā§ā§ āϝāĻžāĻŦā§ (Drop āĻšā§ā§ āϝāĻžāĻŦā§)
fn main() {
let s = String::from("Hello, Rust!");
println!("{}", s);
}
āĻāĻāĻžāύā§
sāĻšāϞ⧠ownerāĨ¤āϝāĻāύ
main()āĻļā§āώ āĻšāĻŦā§,sscope-āĻāϰ āĻŦāĻžāĻāϰ⧠āĻāϞ⧠āϝāĻžāĻŦā§ â memory free āĻšāĻŦā§āĨ¤
đ āĻ
āϰā§āĻĨāĻžā§, Rust āύāĻŋāĻā§ āĻĨā§āĻā§ garbage collect āĻāϰ⧠āύāĻž, āĻāĻŋāύā§āϤ⧠s scope āĻĨā§āĻā§ āĻŦā§āϰ āĻšāϞ⧠RAII (Resource Acquisition Is Initialization) āĻŽāĻĄā§āϞā§āϰ āĻŽāϤ⧠value free āĻāϰ⧠āĻĻā§ā§āĨ¤
⧍. Move (Ownership Transfer)
āϝāĻāύ āĻāĻāĻāĻž value āύāϤā§āύ variable-āĻ assign āĻāϰāĻž āĻšā§, āϤāĻāύ ownership transfer āĻšā§āĨ¤
fn main() {
let s1 = String::from("Munna");
let s2 = s1; // Ownership move āĻšā§ā§ āĻā§āϞ
println!("{}", s2);
// println!("{}", s1); â Error: s1 āĻāϰ owner āύā§
}
āĻāĻāĻžāύā§
s1āĻĨā§āĻā§s2ownership āĻāϞ⧠āĻā§āĻā§āĨ¤āĻāĻāύ āĻļā§āϧā§
s2ownerāĨ¤s1use āĻāϰāϞ⧠compiler error āĻĻā§āĻŦā§āĨ¤
đ āĻā§āύ? āĻāĻžāϰāĻŖ āϝāĻĻāĻŋ s1 āĻāϰ s2 āĻĻā§āĻāύ āĻāĻāϏāĻžāĻĨā§ owner āĻšā§ā§ āϝā§āϤā§, āϤāĻžāĻšāϞ⧠double free error āĻšāϤ⧠āĻĒāĻžāϰāϤā§āĨ¤ Rust āĻāĻāĻžāĻā§ āĻļā§āϰā§āϤā§āĻ āĻāĻāĻāĻžā§āĨ¤
ā§Š. Borrowing (āϧāĻžāϰ āĻĻā§āĻā§āĻž)
āϏāĻŦāϏāĻŽā§ ownership move āĻāϰāϞ⧠āϏāĻŽāϏā§āϝāĻž āĻšāϤā§āĨ¤ āĻāĻāύā§āϝ Rust Borrowing system āĻāύā§āĻā§āĨ¤
đ Borrowing āĻŽāĻžāύ⧠āĻšāϞ⧠owner āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āύāĻž āĻāϰ⧠value āĻā§ āϧāĻžāϰ āĻĻā§āĻā§āĻžāĨ¤
Immutable Borrowing
fn main() {
let s = String::from("Rust");
let len = calculate_length(&s); // &s āĻŽāĻžāύ⧠borrow āĻāϰāϞāĻžāĻŽ
println!("Length of {} is {}", s, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
&sāĻŽāĻžāύā§sāĻā§ borrow āĻāϰāĻž āĻšāϞā§āĨ¤Ownership
s-āĻāϰ āĻāĻžāĻā§āĻ āĻĨāĻžāĻāĻŦā§āĨ¤calculate_lengthāĻļā§āϧ⧠āĻĒā§āϤ⧠āĻĒāĻžāϰāĻŦā§, modify āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§ āύāĻžāĨ¤
đ Rule: āĻāĻāϏāĻžāĻĨā§ āϝāϤ āĻā§āĻļāĻŋ immutable borrow āĻāϰāĻž āϝāĻžāĻŦā§āĨ¤
Mutable Borrowing
fn main() {
let mut s = String::from("Hello");
change(&mut s); // mutable borrow
println!("{}", s);
}
fn change(s: &mut String) {
s.push_str(", World!");
}
āĻāĻāĻžāύā§
&mut sāĻĻāĻŋā§ā§ mutable borrow āĻāϰāĻž āĻšā§ā§āĻā§āĨ¤āĻāĻāύ function value modify āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āĨ¤
đ Rule: āĻāĻ āϏāĻŽā§ā§ āĻļā§āϧ⧠āĻāĻāĻāĻžāĻ mutable borrow āĻāϰāĻž āϝāĻžāĻŦā§āĨ¤
ā§Ē. āĻā§āύ āĻāĻ Ownership & Borrowing āĻĻāϰāĻāĻžāϰ?
Rust-āĻāϰ āĻāĻ āύāĻŋā§āĻŽāĻā§āϞ⧠main āϤāĻŋāύāĻāĻž āϏāĻŽāϏā§āϝāĻžāϰ āϏāĻŽāĻžāϧāĻžāύ āĻāϰā§:
Dangling Pointer â invalid memory reference
Double Free â āĻāĻāĻ memory āĻĻā§āĻāĻŦāĻžāϰ free āĻšā§ā§ crash
Data Race â āĻāĻāĻžāϧāĻŋāĻ thread āĻāĻāϏāĻžāĻĨā§ unsafe āĻāĻžāĻŦā§ memory access
ā§Ģ. Memory Level-āĻ āĻāĻŋ āĻāĻā§?
āϧāϰā§, āĻāĻŽāϰāĻž āĻāĻāĻāĻž String āĻŦāĻžāύāĻžāϞāĻžāĻŽ:
let s = String::from("Rust");
Memory Layout
svariable stack-āĻ pointer āϰāĻžāĻā§āĨ¤String-āĻāϰ āĻāϏāϞ data heap-āĻ āĻĨāĻžāĻā§āĨ¤
Pointer + length + capacity stack-āĻ store āĻšā§āĨ¤
Stack: [ ptr â heap, len = 4, capacity = 4 ]
Heap: [ 'R', 'u', 's', 't' ]
đ āĻāĻāύ āϝāĻĻāĻŋ ownership move āĻšā§:
let s1 = String::from("Hello");
let s2 = s1;
s2-āϤ⧠pointer copy āĻšā§, āĻāĻŋāύā§āϤ⧠ownership shift āĻšā§ā§ āϝāĻžā§āĨ¤s1āĻāϰ valid āύā§āĨ¤
đ Borrowing āĻšāϞā§:
Owner āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻšā§ āύāĻžāĨ¤
āĻļā§āϧ⧠reference (
&) stack pointer āĻšāĻŋāϏāĻžāĻŦā§ āϝāĻžā§āĨ¤Reference āĻĻāĻŋā§ā§ data access āĻāϰāĻž āϝāĻžā§, āĻāĻŋāύā§āϤ⧠āĻāϏāϞ owner control āϧāϰ⧠āϰāĻžāĻā§āĨ¤
ā§Ŧ. Real World Problem Solve
āϧāϰ⧠āϤā§āĻŽāĻŋ āĻāĻāĻāĻŋ multi-threaded program āĻŦāĻžāύāĻžāĻā§āĻā§ āϝā§āĻāĻžāύ⧠āĻāĻāĻžāϧāĻŋāĻ thread āĻāĻāĻ data āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻŦā§āĨ¤
C/C++-āĻ āĻšāϞ⧠manual lock āĻĻāĻŋāϤ⧠āĻšāϤā§āĨ¤ āĻā§āϞ āĻāϰāϞ⧠memory corruption āĻšāϤā§āĨ¤
Rust-āĻ:
Immutable borrow â āĻāĻāϏāĻžāĻĨā§ āĻ āύā§āĻ thread data read āĻāϰāϤ⧠āĻĒāĻžāϰā§
Mutable borrow â āĻāĻ āϏāĻŽā§ā§ āĻļā§āϧ⧠āĻāĻāĻāĻŋ thread data write āĻāϰāϤ⧠āĻĒāĻžāϰā§
āĻĢāϞāĻžāĻĢāϞ: Data Race impossible at compile time!
ā§. Beginner-Friendly Summary
Ownership â āĻĒā§āϰāϤāĻŋāĻāĻŋ value-āĻāϰ āĻāĻāĻāύ āĻŽāĻžāϞāĻŋāĻ (owner) āĻĨāĻžāĻā§
Move â owner āĻ āύā§āϝ variable-āĻ āĻāϞ⧠āϝāĻžā§
Borrowing â owner āĻŦāĻĻāϞāĻžā§ āύāĻž, āĻļā§āϧ⧠reference āϝāĻžā§
Immutable Borrow â āĻāĻāϏāĻžāĻĨā§ āĻ āύā§āĻāĻŦāĻžāϰ
Mutable Borrow â āĻāĻ āϏāĻŽā§ā§ āĻāĻāĻŦāĻžāϰ
Scope āĻļā§āώ āĻšāϞ⧠owner-āĻāϰ value āĻŽā§āĻŽā§āϰāĻŋ āĻĨā§āĻā§ free āĻšā§ā§ āϝāĻžā§
Rust compiler compile-time-āĻāĻ āĻāĻ āϏāĻŦ check āĻāϰ⧠â runtime-āĻ memory bug āύā§āĻ




