Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

Day 28 of #100DaysOfRust: Smart Pointers and Deref Coercion

Today’s learning was all about treating smart pointers like regular references by implementing the Deref trait, exploring deref coercion, and understanding how …

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Today’s learning was all about treating smart pointers like regular references by implementing the Deref trait, exploring deref coercion, and understanding how it interacts with mutability.









📌 Following References to Values



A regular reference (&T) points to a value stored elsewhere. To access the value, we use the dereference operator *:




fn main() {
let x = 5;
let y = &x;

assert_eq!(5, x);
assert_eq!(5, *y);
}






Here *y follows the reference to get the value 5. Without *, the comparison won’t compile because {integer} and &{integer} are different types.









📦 Using Box Like a Reference



Smart pointers like Box<T> behave similarly to references when dereferenced:




fn main() {
let x = 5;
let y = Box::new(x);

assert_eq!(5, x);
assert_eq!(5, *y);
}












🛠 Defining Our Own Smart Pointer



We can define our own smart pointer MyBox<T>:




struct MyBox<T>(T);

impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}






But trying to dereference *y will fail until we implement the Deref trait.









🔗 Implementing the Deref Trait



To enable dereferencing:




use std::ops::Deref;

impl<T> Deref for MyBox<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}






Now *y works just like with Box<T>.



Behind the scenes:




*(y.deref())






This happens automatically whenever we use *.









🔄 Implicit Deref Coercions



Deref coercion allows automatic conversion between references:




  • From &T to &U when T: Deref<Target=U>

  • From &mut T to &mut U when T: DerefMut<Target=U>

  • From &mut T to &U when T: Deref<Target=U>



Example with MyBox<String>:




fn hello(name: &str) {
println!("Hello, {name}!");
}

fn main() {
let m = MyBox::new(String::from("Rust"));
hello(&m); // Works because of deref coercion
}






Without coercion, we’d need:




hello(&(*m)[..]);






Much harder to read!









🔐 Deref and Mutability





  • &T → &U works with Deref.


  • &mut T → &mut U works with DerefMut.


  • &mut T → &U is also allowed.

  • ❌ But &T → &mut U is not allowed, because it could break Rust’s borrowing rules.



This ensures safety and prevents undefined behavior.









🧠 Summary




  • Regular references require * to access the value.


  • Box<T> and custom smart pointers can implement Deref to behave like references.

  • Deref coercion simplifies function calls by automatically converting types.

  • Mutability rules ensure safety: mutable → immutable works, but immutable → mutable does not.



Rust’s deref system is a zero-cost abstraction: all conversions happen at compile time with no runtime cost.






🚀 That’s all for Day 28! Tomorrow, I’ll continue exploring more about **smart pointers and their advanced use

🔍 CTI & Forensik

Cyber Threat Intelligence & Forensik

Bedrohungsgraph · ATT&CK-Mapping · Exploit-Belege
CTI Threat Relationship Graph
Akteure · Techniken · Beziehungen
2 Knoten · 1 Relationen
CVE / Incident Threat Actor Software MITRE ATT&CK CWE Weakness IoC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Day 28 of #100DaysOfRust: Smart Pointers and Deref Coercion

Thematisch verwandte Begriffe: 100DaysOfRust, Smart, Pointers, Deref · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag