Skip to content

Commit

Permalink
Merge pull request #55 from toshmukhamedov/docs/ch15-02-deref
Browse files Browse the repository at this point in the history
docs/ch15-02-deref
  • Loading branch information
ismoilovdevml committed Feb 11, 2024
2 parents a93403e + 3ea7467 commit 1a0a810
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions rustbook-uz/src/ch15-02-deref.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
## Treating Smart Pointers Like Regular References with the `Deref` Trait

Implementing the `Deref` trait allows you to customize the behavior of the
*dereference operator* `*` (not to be confused with the multiplication or glob
operator). By implementing `Deref` in such a way that a smart pointer can be
treated like a regular reference, you can write code that operates on
references and use that code with smart pointers too.

Let’s first look at how the dereference operator works with regular references.
Then we’ll try to define a custom type that behaves like `Box<T>`, and see why
the dereference operator doesn’t work like a reference on our newly defined
type. We’ll explore how implementing the `Deref` trait makes it possible for
smart pointers to work in ways similar to references. Then we’ll look at
Rust’s *deref coercion* feature and how it lets us work with either references
or smart pointers.

> Note: there’s one big difference between the `MyBox<T>` type we’re about to
> build and the real `Box<T>`: our version will not store its data on the heap.
> We are focusing this example on `Deref`, so where the data is actually stored
> is less important than the pointer-like behavior.
## Smart Pointerlarni `Deref` Xususiyati Bilan Oddiy Havolalar Kabi Ishlatish

`Deref` xususiyatini qo'llash, *dereference operatori*ning `*` (ko'paytirish
yoki glob operatori bilan adashtirmaslik kerak) xulq-atvorini sozlashga imkon
beradi. Smart pointerlarni `Deref` xususiyati bilan oddiy havolalar kabi
qo'llasangiz, siz havolalar ustida ishlaydigan kod yozishingiz, shuningdek,
ushbu kodni smart pointerlar bilan ishlatishingiz mumkin bo'ladi.

Keling, avvalo, dereference operatori oddiy havolalar bilan qanday ishlashini
ko'rib chiqaylik. Keyin biz `Box<T>` kabi maxsus turni e'lon qilishga harakat
qilamiz va dereference operatori nega bizning yangi e'lon qilgan turimizdagi
havola kabi ishlamayotganini ko'ramiz. Biz `Deref` xususiyatini amalga oshirish
smart pointerlarning havolalarga o'xshash tarzda ishlashiga qanday imkon
berishini ko'rib chiqamiz. Keyin biz Rustning *deref coercion* xususiyatini va
u bizga havolalar yoki smart pointerlar bilan ishlashga qanday imkon berishini
ko'rib chiqamiz.

> Eslatma: biz qurmoqchi bo'lgan `MyBox<T>` turi va haqiqiy `Box<T>` o‘rtasida
> bitta katta farq bor: bizning versiyamiz o‘z ma’lumotlarini heapda saqlamaydi.
> Biz ushbu misolda e'tiborimizni `Deref`ga qaratmoqdamiz, shuning uchun
> ma'lumotlarning qayerda saqlanishi pointerga o'xshash xatti-harakatlardan
> kamroq ahamiyatga ega.
<!-- Old link, do not remove -->
<a id="following-the-pointer-to-the-value-with-the-dereference-operator"></a>

### Following the Pointer to the Value
### Pointerni Qiymatga bog'lash

A regular reference is a type of pointer, and one way to think of a pointer is
as an arrow to a value stored somewhere else. In Listing 15-6, we create a
reference to an `i32` value and then use the dereference operator to follow the
reference to the value:
Muntazam havola pointerning bir turi bo'lib, pointerni boshqa joyda saqlangan
qiymatga o'q kabi tasavvur qilishning bir usuli. 15-6 ro'yxatda biz `i32`
qiymatiga havola yaratamiz va keyin qiymatga havolani bog'lash uchun dereference
operatoridan foydalanamiz:

<span class="filename">Filename: src/main.rs</span>

Expand Down

0 comments on commit 1a0a810

Please sign in to comment.