+```
+
+The book also uses two mdbook plugins which are part of this repository. If you
+do not install them, you will see warnings when building and the output will not
+look right, but you _will_ still be able to build the book. To use the plugins,
+you should run:
+
+```bash
+$ cargo install --locked --path packages/mdbook-trpl
+```
+
+## Building
+
+To build the book, type:
+
+```bash
+cd persian
+$ mdbook build
+```
+
+The output will be in the `book` subdirectory. To check it out, open it in
+your web browser.
+
+_Firefox:_
+
+```bash
+$ firefox book/index.html # Linux
+$ open -a "Firefox" book/index.html # OS X
+$ Start-Process "firefox.exe" .\book\index.html # Windows (PowerShell)
+$ start firefox.exe .\book\index.html # Windows (Cmd)
+```
+
+_Chrome:_
+
+```bash
+$ google-chrome book/index.html # Linux
+$ open -a "Google Chrome" book/index.html # OS X
+$ Start-Process "chrome.exe" .\book\index.html # Windows (PowerShell)
+$ start chrome.exe .\book\index.html # Windows (Cmd)
+```
+
+To run the tests:
+
+```bash
+$ mdbook test --library-path packages/trpl/target/debug/deps
+```
+
+## Contributing
+
+We'd love your help! Please see [CONTRIBUTING-EN.md][contrib] to learn about the
+kinds of contributions we're looking for.
+
+[contrib]: https://github.com/rust-lang/book/blob/main/CONTRIBUTING.md
+
+Because the book is [printed][nostarch], and because we want
+to keep the online version of the book close to the print version when
+possible, it may take longer than you're used to for us to address your issue
+or pull request.
+
+So far, we've been doing a larger revision to coincide with [Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger
+revisions, we will only be correcting errors. If your issue or pull request
+isn't strictly fixing an error, it might sit until the next time that we're
+working on a large revision: expect on the order of months or years. Thank you
+for your patience!
+
+### Translations
+
+We'd love help translating the book! See the [Translations] label to join in
+efforts that are currently in progress. Open a new issue to start working on
+a new language! We're waiting on [mdbook support] for multiple languages
+before we merge any in, but feel free to start!
+
+[Translations]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations
+[mdbook support]: https://github.com/rust-lang/mdBook/issues/5
+
+## Spellchecking
+
+To scan source files for spelling errors, you can use the `spellcheck.sh`
+script available in the `ci` directory. It needs a dictionary of valid words,
+which is provided in `ci/dictionary.txt`. If the script produces a false
+positive (say, you used the word `BTreeMap` which the script considers invalid),
+you need to add this word to `ci/dictionary.txt` (keep the sorted order for
+consistency).
diff --git a/README.md b/README.md
index 29c9083adb..9f5e09c77e 100644
--- a/README.md
+++ b/README.md
@@ -1,57 +1,72 @@
-# The Rust Programming Language
-
+# زبان برنامهنویسی راست
-This repository contains the source of "The Rust Programming Language" book.
+
+
+
+
-[The book is available in dead-tree form from No Starch Press][nostarch].
+
+
+[English](README-EN.md) | [فارسی](README.md)
+
+
+
+
+
+**لطفاً توجه داشته باشید که این ترجمه هنوز کامل نشده است و نیاز به بازبینی و ویرایش دقیق دارد که به زودی تکمیل خواهد شد. شما میتوانید ترجمههای در حال انجام را در شاخه [`persian-draft`](https://github.com/drunkleen/rust-book-persian/tree/persian-draft) مخزن پیدا کنید.**
+
+
+
+
+این مخزن شامل منبع کتاب "زبان برنامهنویسی راست" به زبان فارسی است.
+
+[کتاب انگلیسی به صورت نسخه چاپی از No Starch Press در دسترس است][nostarch].
[nostarch]: https://nostarch.com/rust-programming-language-2nd-edition
-You can also read the book for free online. Please see the book as shipped with
-the latest [stable], [beta], or [nightly] Rust releases. Be aware that issues
-in those versions may have been fixed in this repository already, as those
-releases are updated less frequently.
+همچنین میتوانید کتاب را به صورت رایگان آنلاین بخوانید. لطفاً کتاب را به صورت ارسال شده با آخرین نسخههای [stable]، [beta]، یا [nightly] راست مشاهده کنید. توجه داشته باشید که مشکلات در آن نسخهها ممکن است در این مخزن قبلاً رفع شده باشد، زیرا آن نسخهها به صورت مکرر بهروز نمیشوند.
[stable]: https://doc.rust-lang.org/stable/book/
[beta]: https://doc.rust-lang.org/beta/book/
[nightly]: https://doc.rust-lang.org/nightly/book/
-See the [releases] to download just the code of all the code listings that appear in the book.
+فقط برای دانلود تمام لیستهای کد که در کتاب ظاهر میشوند، به [releases] مراجعه کنید.
-[releases]: https://github.com/rust-lang/book/releases
+[releases]: https://github.com/drunkleen/rust-book-persian/releases
-## Requirements
+## نیازمندیها
-Building the book requires [mdBook], ideally the same version that
-rust-lang/rust uses in [this file][rust-mdbook]. To get it:
+برای ساخت کتاب نیاز به [mdBook] دارید، ترجیحاً همان نسخهای که rust-lang/rust در [این فایل][rust-mdbook] استفاده میکند. برای دریافت آن:
[mdBook]: https://github.com/rust-lang/mdBook
[rust-mdbook]: https://github.com/rust-lang/rust/blob/master/src/tools/rustbook/Cargo.toml
```bash
-$ cargo install mdbook --locked --version
+cargo install mdbook --locked --version
```
-
-The book also uses two mdbook plugins which are part of this repository. If you
-do not install them, you will see warnings when building and the output will not
-look right, but you _will_ still be able to build the book. To use the plugins,
-you should run:
+Example:
```bash
$ cargo install --locked --path packages/mdbook-trpl --force
```
-## Building
+این کتاب همچنین از دو افزونه mdbook استفاده میکند که بخشی از این مخزن هستند. اگر آنها را نصب نکنید، هنگام ساخت هشدارهایی مشاهده خواهید کرد و خروجی به درستی نمایش داده نمیشود، اما هنوز هم میتوانید کتاب را بسازید. برای استفاده از افزونهها، باید دستور زیر را اجرا کنید:
-To build the book, type:
+```bash
+cargo install --locked --path packages/mdbook-trpl
+```
+
+## ساخت
+
+برای ساختن کتاب عبارات زیر را تایپ کنید:
```bash
$ mdbook build
```
-The output will be in the `book` subdirectory. To check it out, open it in
-your web browser.
+
+خروجی در زیرمجموعه `book` خواهد بود. برای مشاهده، آن را در مرورگر وب خود باز کنید.
_Firefox:_
@@ -71,46 +86,21 @@ $ Start-Process "chrome.exe" .\book\index.html # Windows (PowerShell)
$ start chrome.exe .\book\index.html # Windows (Cmd)
```
-To run the tests:
+برای اجرای تستها:
```bash
-$ cd packages/trpl
$ mdbook test --library-path packages/trpl/target/debug/deps
```
+## مشارکت
-## Contributing
-
-We'd love your help! Please see [CONTRIBUTING.md][contrib] to learn about the
-kinds of contributions we're looking for.
-
-[contrib]: https://github.com/rust-lang/book/blob/main/CONTRIBUTING.md
-
-Because the book is [printed][nostarch], and because we want
-to keep the online version of the book close to the print version when
-possible, it may take longer than you're used to for us to address your issue
-or pull request.
-
-So far, we've been doing a larger revision to coincide with [Rust Editions](https://doc.rust-lang.org/edition-guide/). Between those larger
-revisions, we will only be correcting errors. If your issue or pull request
-isn't strictly fixing an error, it might sit until the next time that we're
-working on a large revision: expect on the order of months or years. Thank you
-for your patience!
+ما از کمک شما خوشحال میشویم! لطفاً [CONTRIBUTING.md][contrib] را ببینید تا با انواع مشارکتهایی که به دنبال آنها هستیم آشنا شوید.
-### Translations
+[contrib]: https://github.com/drunkleen/rust-book-persian/blob/main/CONTRIBUTING.md
-We'd love help translating the book! See the [Translations] label to join in
-efforts that are currently in progress. Open a new issue to start working on
-a new language! We're waiting on [mdbook support] for multiple languages
-before we merge any in, but feel free to start!
+به دلیل اینکه کتاب [چاپ شده است][nostarch] و همچنین میخواهیم نسخه آنلاین کتاب را تا حد ممکن نزدیک به نسخه چاپی نگه داریم، ممکن است برای ما بیشتر از حد معمول طول بکشد تا به مشکل یا درخواست کشش شما رسیدگی کنیم.
-[Translations]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations
-[mdbook support]: https://github.com/rust-lang/mdBook/issues/5
+## بررسی املاء
-## Spellchecking
+برای اسکن فایلهای منبع برای خطاهای املایی، میتوانید از اسکریپت `spellcheck.sh` که در پوشه `ci` موجود است استفاده کنید. این اسکریپت به یک فرهنگ لغت از کلمات معتبر نیاز دارد که در `ci/dictionary.txt` ارائه شده است. اگر اسکریپت یک مثبت کاذب تولید کرد (مثلاً شما از کلمه `BTreeMap` استفاده کردهاید که اسکریپت آن را نامعتبر میداند)، باید این کلمه را به `ci/dictionary.txt` اضافه کنید (ترتیب مرتبشده را برای سازگاری حفظ کنید).
-To scan source files for spelling errors, you can use the `spellcheck.sh`
-script available in the `ci` directory. It needs a dictionary of valid words,
-which is provided in `ci/dictionary.txt`. If the script produces a false
-positive (say, you used the word `BTreeMap` which the script considers invalid),
-you need to add this word to `ci/dictionary.txt` (keep the sorted order for
-consistency).
+
\ No newline at end of file
diff --git a/TODO-en.md b/TODO-en.md
new file mode 100644
index 0000000000..1e7f01387a
--- /dev/null
+++ b/TODO-en.md
@@ -0,0 +1,23 @@
+# In each chapter
+
+
+
+[English](TODO-EN.md) | [فارسی](TODO.md)
+
+
+
+- [ ] Manual regeneration
+- [ ] Check for upstream changes from last snapshot
+- [ ] Propagate updated output to docx
+- [ ] Extract docx and check diff
+- [ ] Answer all comments
+- [ ] Check cross references
+- [ ] Check indentation of --snip--
+- [ ] Numbered lines, Gray out unchanged lines
+- [ ] Check line wrapping
+- [ ] Check for unneeded command/compiling/running output
+- [ ] Check println style and error messages
+- [ ] Add alt text to images
+- [ ] Index tags
+ - [ ] search for "convention" conventions:naming:of blah
+ - [ ] check for double spaces, spaces at the end of paragraphs
diff --git a/TODO.md b/TODO.md
index b61f853145..2268eabc71 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,17 +1,33 @@
-# In each chapter
+
-- [ ] Manual regeneration
-- [ ] Check for upstream changes from last snapshot
-- [ ] Propagate updated output to docx
-- [ ] Extract docx and check diff
-- [ ] Answer all comments
-- [ ] Check cross references
-- [ ] Check indentation of --snip--
-- [ ] Numbered lines, Gray out unchanged lines
-- [ ] Check line wrapping
-- [ ] Check for unneeded command/compiling/running output
-- [ ] Check println style and error messages
-- [ ] Add alt text to images
-- [ ] Index tags
- - [ ] search for "convention" conventions:naming:of blah
- - [ ] check for double spaces, spaces at the end of paragraphs
+
+# در هر فصل
+
+
+
+[English](TODO-EN.md) | [فارسی](TODO.md)
+
+
+
+
+ - [ ] بازتولید دستی
+ - [ ] بررسی تغییرات بالادستی از آخرین اسنپشات
+ - [ ] انتقال خروجی بهروزشده به docx
+ - [ ] استخراج docx و بررسی تفاوتها
+ - [ ] پاسخ به تمام نظرات
+ - [ ] بررسی ارجاعات متقابل
+ - [ ] بررسی تورفتگی --snip--
+ - [ ] خطوط شمارهگذاریشده، خاکستریکردن خطوط بدون تغییر
+ - [ ] بررسی پیچیدن خطوط
+ - [ ] بررسی خروجیهای دستوری/کامپایل/اجرا که نیاز نیستند
+ - [ ] بررسی سبک println و پیامهای خطا
+ - [ ] افزودن متن جایگزین به تصاویر
+ - [ ] برچسبهای شاخص
+
+ - [ ] جستجوی "convention" conventions:naming:of blah
+ - [ ] بررسی وجود دو فاصله یا فاصله در انتهای پاراگرافها
+
+
+
+
+
\ No newline at end of file
diff --git a/book.toml b/book.toml
index 928a44453f..fae9703e5c 100644
--- a/book.toml
+++ b/book.toml
@@ -4,11 +4,12 @@
[book]
title = "The Rust Programming Language"
authors = ["Steve Klabnik", "Carol Nichols", "Chris Krycho", "Contributions from the Rust Community"]
+language = "fa"
[output.html]
-additional-css = ["ferris.css", "theme/2018-edition.css", "theme/semantic-notes.css", "theme/listing.css"]
+additional-css = ["ferris.css", "theme/2018-edition.css", "theme/semantic-notes.css", "theme/listing.css", "theme/custom.css"]
additional-js = ["ferris.js"]
-git-repository-url = "https://github.com/rust-lang/book"
+git-repository-url = "https://github.com/persian-rust/book"
[output.html.redirect]
"ch17-00-oop.html" = "ch18-00-oop.html"
diff --git a/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml b/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml
index eba27a883c..6c2b31e767 100644
--- a/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml
+++ b/listings/ch02-guessing-game-tutorial/listing-02-03/Cargo.toml
@@ -6,4 +6,4 @@ edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-rand = "0.8.5"
+rand = "0.9.2"
diff --git a/src/SUMMARY.md b/src/SUMMARY.md
index 286cb46db8..7a27c5aac0 100644
--- a/src/SUMMARY.md
+++ b/src/SUMMARY.md
@@ -1,143 +1,144 @@
-# The Rust Programming Language
-
-[The Rust Programming Language](title-page.md)
-[Foreword](foreword.md)
-[Introduction](ch00-00-introduction.md)
-
-## Getting started
-
-- [Getting Started](ch01-00-getting-started.md)
- - [Installation](ch01-01-installation.md)
- - [Hello, World!](ch01-02-hello-world.md)
- - [Hello, Cargo!](ch01-03-hello-cargo.md)
-
-- [Programming a Guessing Game](ch02-00-guessing-game-tutorial.md)
-
-- [Common Programming Concepts](ch03-00-common-programming-concepts.md)
- - [Variables and Mutability](ch03-01-variables-and-mutability.md)
- - [Data Types](ch03-02-data-types.md)
- - [Functions](ch03-03-how-functions-work.md)
- - [Comments](ch03-04-comments.md)
- - [Control Flow](ch03-05-control-flow.md)
-
-- [Understanding Ownership](ch04-00-understanding-ownership.md)
- - [What is Ownership?](ch04-01-what-is-ownership.md)
- - [References and Borrowing](ch04-02-references-and-borrowing.md)
- - [The Slice Type](ch04-03-slices.md)
-
-- [Using Structs to Structure Related Data](ch05-00-structs.md)
- - [Defining and Instantiating Structs](ch05-01-defining-structs.md)
- - [An Example Program Using Structs](ch05-02-example-structs.md)
- - [Method Syntax](ch05-03-method-syntax.md)
-
-- [Enums and Pattern Matching](ch06-00-enums.md)
- - [Defining an Enum](ch06-01-defining-an-enum.md)
- - [The `match` Control Flow Construct](ch06-02-match.md)
- - [Concise Control Flow with `if let` and `let else`](ch06-03-if-let.md)
-
-## Basic Rust Literacy
-
-- [Managing Growing Projects with Packages, Crates, and Modules](ch07-00-managing-growing-projects-with-packages-crates-and-modules.md)
- - [Packages and Crates](ch07-01-packages-and-crates.md)
- - [Defining Modules to Control Scope and Privacy](ch07-02-defining-modules-to-control-scope-and-privacy.md)
- - [Paths for Referring to an Item in the Module Tree](ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md)
- - [Bringing Paths Into Scope with the `use` Keyword](ch07-04-bringing-paths-into-scope-with-the-use-keyword.md)
- - [Separating Modules into Different Files](ch07-05-separating-modules-into-different-files.md)
-
-- [Common Collections](ch08-00-common-collections.md)
- - [Storing Lists of Values with Vectors](ch08-01-vectors.md)
- - [Storing UTF-8 Encoded Text with Strings](ch08-02-strings.md)
- - [Storing Keys with Associated Values in Hash Maps](ch08-03-hash-maps.md)
-
-- [Error Handling](ch09-00-error-handling.md)
- - [Unrecoverable Errors with `panic!`](ch09-01-unrecoverable-errors-with-panic.md)
- - [Recoverable Errors with `Result`](ch09-02-recoverable-errors-with-result.md)
- - [To `panic!` or Not to `panic!`](ch09-03-to-panic-or-not-to-panic.md)
-
-- [Generic Types, Traits, and Lifetimes](ch10-00-generics.md)
- - [Generic Data Types](ch10-01-syntax.md)
- - [Traits: Defining Shared Behavior](ch10-02-traits.md)
- - [Validating References with Lifetimes](ch10-03-lifetime-syntax.md)
-
-- [Writing Automated Tests](ch11-00-testing.md)
- - [How to Write Tests](ch11-01-writing-tests.md)
- - [Controlling How Tests Are Run](ch11-02-running-tests.md)
- - [Test Organization](ch11-03-test-organization.md)
-
-- [An I/O Project: Building a Command Line Program](ch12-00-an-io-project.md)
- - [Accepting Command Line Arguments](ch12-01-accepting-command-line-arguments.md)
- - [Reading a File](ch12-02-reading-a-file.md)
- - [Refactoring to Improve Modularity and Error Handling](ch12-03-improving-error-handling-and-modularity.md)
- - [Developing the Library’s Functionality with Test Driven Development](ch12-04-testing-the-librarys-functionality.md)
- - [Working with Environment Variables](ch12-05-working-with-environment-variables.md)
- - [Writing Error Messages to Standard Error Instead of Standard Output](ch12-06-writing-to-stderr-instead-of-stdout.md)
-
-## Thinking in Rust
-
-- [Functional Language Features: Iterators and Closures](ch13-00-functional-features.md)
- - [Closures: Anonymous Functions that Capture Their Environment](ch13-01-closures.md)
- - [Processing a Series of Items with Iterators](ch13-02-iterators.md)
- - [Improving Our I/O Project](ch13-03-improving-our-io-project.md)
- - [Comparing Performance: Loops vs. Iterators](ch13-04-performance.md)
-
-- [More about Cargo and Crates.io](ch14-00-more-about-cargo.md)
- - [Customizing Builds with Release Profiles](ch14-01-release-profiles.md)
- - [Publishing a Crate to Crates.io](ch14-02-publishing-to-crates-io.md)
- - [Cargo Workspaces](ch14-03-cargo-workspaces.md)
- - [Installing Binaries from Crates.io with `cargo install`](ch14-04-installing-binaries.md)
- - [Extending Cargo with Custom Commands](ch14-05-extending-cargo.md)
-
-- [Smart Pointers](ch15-00-smart-pointers.md)
- - [Using `Box` to Point to Data on the Heap](ch15-01-box.md)
- - [Treating Smart Pointers Like Regular References with `Deref`](ch15-02-deref.md)
- - [Running Code on Cleanup with the `Drop` Trait](ch15-03-drop.md)
- - [`Rc`, the Reference Counted Smart Pointer](ch15-04-rc.md)
- - [`RefCell` and the Interior Mutability Pattern](ch15-05-interior-mutability.md)
- - [Reference Cycles Can Leak Memory](ch15-06-reference-cycles.md)
-
-- [Fearless Concurrency](ch16-00-concurrency.md)
- - [Using Threads to Run Code Simultaneously](ch16-01-threads.md)
- - [Using Message Passing to Transfer Data Between Threads](ch16-02-message-passing.md)
- - [Shared-State Concurrency](ch16-03-shared-state.md)
- - [Extensible Concurrency with the `Send` and `Sync` Traits](ch16-04-extensible-concurrency-sync-and-send.md)
-
-- [Fundamentals of Asynchronous Programming: Async, Await, Futures, and Streams](ch17-00-async-await.md)
- - [Futures and the Async Syntax](ch17-01-futures-and-syntax.md)
- - [Applying Concurrency with Async](ch17-02-concurrency-with-async.md)
- - [Working With Any Number of Futures](ch17-03-more-futures.md)
- - [Streams: Futures in Sequence](ch17-04-streams.md)
- - [A Closer Look at the Traits for Async](ch17-05-traits-for-async.md)
- - [Futures, Tasks, and Threads](ch17-06-futures-tasks-threads.md)
-
-- [Object Oriented Programming Features of Rust](ch18-00-oop.md)
- - [Characteristics of Object-Oriented Languages](ch18-01-what-is-oo.md)
- - [Using Trait Objects That Allow for Values of Different Types](ch18-02-trait-objects.md)
- - [Implementing an Object-Oriented Design Pattern](ch18-03-oo-design-patterns.md)
-
-## Advanced Topics
-
-- [Patterns and Matching](ch19-00-patterns.md)
- - [All the Places Patterns Can Be Used](ch19-01-all-the-places-for-patterns.md)
- - [Refutability: Whether a Pattern Might Fail to Match](ch19-02-refutability.md)
- - [Pattern Syntax](ch19-03-pattern-syntax.md)
-
-- [Advanced Features](ch20-00-advanced-features.md)
- - [Unsafe Rust](ch20-01-unsafe-rust.md)
- - [Advanced Traits](ch20-02-advanced-traits.md)
- - [Advanced Types](ch20-03-advanced-types.md)
- - [Advanced Functions and Closures](ch20-04-advanced-functions-and-closures.md)
- - [Macros](ch20-05-macros.md)
-
-- [Final Project: Building a Multithreaded Web Server](ch21-00-final-project-a-web-server.md)
- - [Building a Single-Threaded Web Server](ch21-01-single-threaded.md)
- - [Turning Our Single-Threaded Server into a Multithreaded Server](ch21-02-multithreaded.md)
- - [Graceful Shutdown and Cleanup](ch21-03-graceful-shutdown-and-cleanup.md)
-
-- [Appendix](appendix-00.md)
- - [A - Keywords](appendix-01-keywords.md)
- - [B - Operators and Symbols](appendix-02-operators.md)
- - [C - Derivable Traits](appendix-03-derivable-traits.md)
- - [D - Useful Development Tools](appendix-04-useful-development-tools.md)
- - [E - Editions](appendix-05-editions.md)
- - [F - Translations of the Book](appendix-06-translation.md)
- - [G - How Rust is Made and “Nightly Rust”](appendix-07-nightly-rust.md)
+# زبان برنامهنویسی Rust
+
+[زبان برنامهنویسی راست](title-page.md)
+[پیشگفتار](foreword.md)
+[مقدمه](ch00-00-introduction.md)
+
+## شروع کار
+
+- [شروع کار](ch01-00-getting-started.md)
+ - [نصب](ch01-01-installation.md)
+ - [سلام، دنیا!](ch01-02-hello-world.md)
+ - [سلام، Cargo!](ch01-03-hello-cargo.md)
+
+- [برنامهنویسی یک بازی حدس زدن](ch02-00-guessing-game-tutorial.md)
+
+- [مفاهیم رایج برنامهنویسی](ch03-00-common-programming-concepts.md)
+ - [متغیرها و تغییرپذیری](ch03-01-variables-and-mutability.md)
+ - [انواع داده](ch03-02-data-types.md)
+ - [توابع](ch03-03-how-functions-work.md)
+ - [کامنتها](ch03-04-comments.md)
+ - [کنترل جریان](ch03-05-control-flow.md)
+
+- [درک مالکیت](ch04-00-understanding-ownership.md)
+ - [مالکیت چیست؟](ch04-01-what-is-ownership.md)
+ - [ارجاعات و قرض گرفتن](ch04-02-references-and-borrowing.md)
+ - [نوع Slice](ch04-03-slices.md)
+
+- [استفاده از Structها برای سازماندهی دادههای مرتبط](ch05-00-structs.md)
+ - [تعریف و نمونهسازی Structها](ch05-01-defining-structs.md)
+ - [یک برنامه نمونه با استفاده از Structها](ch05-02-example-structs.md)
+ - [متد](ch05-03-method-syntax.md)
+
+- [شمارندهها و تطابق الگو](ch06-00-enums.md)
+ - [تعریف یک Enum](ch06-01-defining-an-enum.md)
+ - [ساختار جریان کنترل `match`](ch06-02-match.md)
+ - [جریان کنترل مختصر با `if let` و `let else`](ch06-03-if-let.md)
+
+## سواد پایه Rust
+
+- [مدیریت پروژههای در حال رشد با پکیجها، جعبهها (crates)، و ماژولها](ch07-00-managing-growing-projects-with-packages-crates-and-modules.md)
+ - [پکیجها و جعبهها (crates)](ch07-01-packages-and-crates.md)
+ - [تعریف ماژولها برای کنترل دامنه و حریم خصوصی](ch07-02-defining-modules-to-control-scope-and-privacy.md)
+ - [مسیرها برای ارجاع به یک آیتم در درخت ماژول](ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md)
+ - [وارد کردن مسیرها با کلمه کلیدی `use`](ch07-04-bringing-paths-into-scope-with-the-use-keyword.md)
+ - [جداسازی ماژولها به فایلهای مختلف](ch07-05-separating-modules-into-different-files.md)
+
+- [مجموعههای رایج](ch08-00-common-collections.md)
+ - [ذخیره لیست مقادیر با بردارها](ch08-01-vectors.md)
+ - [ذخیره متن رمزگذاریشده UTF-8 با رشتهها](ch08-02-strings.md)
+ - [ذخیره کلیدها با مقادیر مرتبط در نقشههای هش](ch08-03-hash-maps.md)
+
+- [مدیریت خطاها](ch09-00-error-handling.md)
+ - [خطاهای غیرقابل بازیابی با `panic!`](ch09-01-unrecoverable-errors-with-panic.md)
+ - [خطاهای قابل بازیابی با `Result`](ch09-02-recoverable-errors-with-result.md)
+ - [آیا از `panic!` استفاده کنیم یا نه؟](ch09-03-to-panic-or-not-to-panic.md)
+
+- [انواع جنریک، صفتها، و طول عمرها](ch10-00-generics.md)
+ - [انواع داده جنریک](ch10-01-syntax.md)
+ - [صفتها: تعریف رفتارهای مشترک](ch10-02-traits.md)
+ - [اعتبارسنجی ارجاعات با طول عمرها](ch10-03-lifetime-syntax.md)
+
+- [نوشتن تستهای خودکار](ch11-00-testing.md)
+ - [چگونه تست بنویسیم](ch11-01-writing-tests.md)
+ - [کنترل نحوه اجرای تستها](ch11-02-running-tests.md)
+ - [سازماندهی تستها](ch11-03-test-organization.md)
+
+- [یک پروژه I/O: ساخت یک برنامه خط فرمان](ch12-00-an-io-project.md)
+ - [پذیرش آرگومانهای خط فرمان](ch12-01-accepting-command-line-arguments.md)
+ - [خواندن یک فایل](ch12-02-reading-a-file.md)
+ - [بازسازی برای بهبود ماژولاریت و مدیریت خطا](ch12-03-improving-error-handling-and-modularity.md)
+ - [توسعه قابلیتهای کتابخانه با توسعه مبتنی بر تست](ch12-04-testing-the-librarys-functionality.md)
+ - [کار با متغیرهای محیطی](ch12-05-working-with-environment-variables.md)
+ - [نوشتن پیامهای خطا به خروجی خطا به جای خروجی استاندارد](ch12-06-writing-to-stderr-instead-of-stdout.md)
+
+## تفکر در Rust
+
+- [ویژگیهای زبانهای تابعی: Iteratorها و Closureها](ch13-00-functional-features.md)
+ - [Closureها: توابع ناشناخته که محیط خود را میگیرند](ch13-01-closures.md)
+ - [پردازش یک سری آیتم با Iteratorها](ch13-02-iterators.md)
+ - [بهبود پروژه I/O ما](ch13-03-improving-our-io-project.md)
+ - [مقایسه عملکرد: حلقهها در مقابل Iteratorها](ch13-04-performance.md)
+
+- [بیشتر درباره Cargo و Crates.io](ch14-00-more-about-cargo.md)
+ - [سفارشیسازی بیلدها با پروفایلهای انتشار](ch14-01-release-profiles.md)
+ - [منتشر کردن یک crate در Crates.io](ch14-02-publishing-to-crates-io.md)
+ - [محیطهای کاری Cargo](ch14-03-cargo-workspaces.md)
+ - [نصب باینریها از Crates.io با `cargo install`](ch14-04-installing-binaries.md)
+ - [گسترش Cargo با دستورات سفارشی](ch14-05-extending-cargo.md)
+
+- [اشارهگرهای هوشمند (Smart Pointers)](ch15-00-smart-pointers.md)
+ - [استفاده از `Box` برای اشاره به دادهها در Heap](ch15-01-box.md)
+ - [رفتار اشارهگر (Pointer)های هوشمند مانند ارجاعات معمولی با صفت `Deref`](ch15-02-deref.md)
+ - [اجرای کد هنگام پاکسازی با صفت `Drop`](ch15-03-drop.md)
+ - [`Rc`، اشارهگر (Pointer) هوشمند با شمارش ارجاعات](ch15-04-rc.md)
+ - [`RefCell` و الگوی تغییرپذیری داخلی](ch15-05-interior-mutability.md)
+ - [چرخههای ارجاع میتوانند باعث نشت حافظه شوند](ch15-06-reference-cycles.md)
+
+- [همزمانی بدون ترس](ch16-00-concurrency.md)
+ - [استفاده از نخها برای اجرای همزمان کد](ch16-01-threads.md)
+ - [استفاده از پیامرسانی برای انتقال داده بین نخها](ch16-02-message-passing.md)
+ - [همزمانی با وضعیت مشترک](ch16-03-shared-state.md)
+ - [همزمانی قابل گسترش با صفتهای `Sync` و `Send`](ch16-04-extensible-concurrency-sync-and-send.md)
+
+- [Async و Await](ch17-00-async-await.md)
+ - [Futures و نحو Async](ch17-01-futures-and-syntax.md)
+ - [همزمانی با Async](ch17-02-concurrency-with-async.md)
+ - [کار با تعداد دلخواهی از Futures](ch17-03-more-futures.md)
+ - [Streams](ch17-04-streams.md)
+ - [بررسی عمیق صفتها برای Async](ch17-05-traits-for-async.md)
+ - [Futures، Tasks، و Threads](ch17-06-futures-tasks-threads.md)
+
+- [ویژگیهای برنامهنویسی شیگرا در Rust](ch18-00-oop.md)
+ - [ویژگیهای زبانهای شیگرا](ch18-01-what-is-oo.md)
+ - [استفاده از صفتهای شیگرا برای مقادیر از تایپهای مختلف](ch18-02-trait-objects.md)
+ - [پیادهسازی یک الگوی طراحی شیگرا](ch18-03-oo-design-patterns.md)
+
+## مباحث پیشرفته
+
+- [الگوها و تطبیق](ch19-00-patterns.md)
+ - [تمام مکانهایی که میتوان از الگوها استفاده کرد](ch19-01-all-the-places-for-patterns.md)
+ - [قابلیت رد: آیا ممکن است یک الگو با مقدار مطابقت نداشته باشد؟](ch19-02-refutability.md)
+ - [نحو الگوها](ch19-03-pattern-syntax.md)
+
+
+- [ویژگیهای پیشرفته](ch20-00-advanced-features.md)
+ - [Rust ناایمن](ch20-01-unsafe-rust.md)
+ - [Traits پیشرفته](ch20-02-advanced-traits.md)
+ - [تایپهای پیشرفته](ch20-03-advanced-types.md)
+ - [توابع و Closures پیشرفته](ch20-04-advanced-functions-and-closures.md)
+ - [ماکروها](ch20-05-macros.md)
+
+- [پروژه نهایی: ساخت یک وبسرور چندنخی](ch21-00-final-project-a-web-server.md)
+ - [ساخت یک وبسرور single-threaded](ch21-01-single-threaded.md)
+ - [تبدیل وبسرور تکنخی به وبسرور چندنخی](ch21-02-multithreaded.md)
+ - [خاموشی ملایم و پاکسازی](ch21-03-graceful-shutdown-and-cleanup.md)
+
+- [ضمیمه](appendix-00.md)
+ - [الف - کلمات کلیدی](appendix-01-keywords.md)
+ - [ب - عملگرها و نمادها](appendix-02-operators.md)
+ - [ج - صفتهای قابل اشتقاق](appendix-03-derivable-traits.md)
+ - [د - ابزارهای توسعه مفید](appendix-04-useful-development-tools.md)
+ - [ه - نسخهها](appendix-05-editions.md)
+ - [و - ترجمههای کتاب](appendix-06-translation.md)
+ - [ی - چگونه Rust ساخته میشود و "Nightly Rust"](appendix-07-nightly-rust.md)
diff --git a/src/appendix-00.md b/src/appendix-00.md
index 83a7e91329..9bf891af51 100644
--- a/src/appendix-00.md
+++ b/src/appendix-00.md
@@ -1,4 +1,5 @@
-# Appendix
+# ضمیمه
-The following sections contain reference material you may find useful in your
-Rust journey.
+بخشهای زیر حاوی مطالب مرجعی هستند که ممکن است در مسیر یادگیری Rust برای شما مفید باشند.
+
+
\ No newline at end of file
diff --git a/src/appendix-01-keywords.md b/src/appendix-01-keywords.md
index 15f6f5b9ae..d913a20836 100644
--- a/src/appendix-01-keywords.md
+++ b/src/appendix-01-keywords.md
@@ -1,69 +1,58 @@
-## Appendix A: Keywords
+## ضمیمه الف: کلمات کلیدی
-The following list contains keywords that are reserved for current or future
-use by the Rust language. As such, they cannot be used as identifiers (except
-as raw identifiers as we’ll discuss in the “[Raw
-Identifiers][raw-identifiers]” section). Identifiers are names
-of functions, variables, parameters, struct fields, modules, crates, constants,
-macros, static values, attributes, types, traits, or lifetimes.
+لیست زیر شامل کلمات کلیدی است که برای استفاده فعلی یا آینده توسط زبان Rust رزرو شدهاند. به همین دلیل، نمیتوان از آنها به عنوان شناسه استفاده کرد (مگر به عنوان شناسه خام، همانطور که در بخش “[شناسههای خام][raw-identifiers]” توضیح داده خواهد شد). شناسهها نامهایی هستند که برای توابع، متغیرها، پارامترها، فیلدهای ساختار، ماژولها، جعبهها (crates)، ثابتها، ماکروها، مقادیر استاتیک، ویژگیها، انواع، ویژگیها، یا طول عمرها استفاده میشوند.
[raw-identifiers]: #raw-identifiers
-### Keywords Currently in Use
-
-The following is a list of keywords currently in use, with their functionality
-described.
-
-- `as` - perform primitive casting, disambiguate the specific trait containing
- an item, or rename items in `use` statements
-- `async` - return a `Future` instead of blocking the current thread
-- `await` - suspend execution until the result of a `Future` is ready
-- `break` - exit a loop immediately
-- `const` - define constant items or constant raw pointers
-- `continue` - continue to the next loop iteration
-- `crate` - in a module path, refers to the crate root
-- `dyn` - dynamic dispatch to a trait object
-- `else` - fallback for `if` and `if let` control flow constructs
-- `enum` - define an enumeration
-- `extern` - link an external function or variable
-- `false` - Boolean false literal
-- `fn` - define a function or the function pointer type
-- `for` - loop over items from an iterator, implement a trait, or specify a
- higher-ranked lifetime
-- `if` - branch based on the result of a conditional expression
-- `impl` - implement inherent or trait functionality
-- `in` - part of `for` loop syntax
-- `let` - bind a variable
-- `loop` - loop unconditionally
-- `match` - match a value to patterns
-- `mod` - define a module
-- `move` - make a closure take ownership of all its captures
-- `mut` - denote mutability in references, raw pointers, or pattern bindings
-- `pub` - denote public visibility in struct fields, `impl` blocks, or modules
-- `ref` - bind by reference
-- `return` - return from function
-- `Self` - a type alias for the type we are defining or implementing
-- `self` - method subject or current module
-- `static` - global variable or lifetime lasting the entire program execution
-- `struct` - define a structure
-- `super` - parent module of the current module
-- `trait` - define a trait
-- `true` - Boolean true literal
-- `type` - define a type alias or associated type
-- `union` - define a [union][union]; is only a keyword when used
- in a union declaration
-- `unsafe` - denote unsafe code, functions, traits, or implementations
-- `use` - bring symbols into scope; specify precise captures for generic and
- lifetime bounds
-- `where` - denote clauses that constrain a type
-- `while` - loop conditionally based on the result of an expression
+### کلمات کلیدی در حال استفاده
+
+فهرست زیر شامل کلمات کلیدی است که در حال حاضر استفاده میشوند، همراه با توضیح عملکرد آنها:
+
+- `as` - انجام تبدیل اولیه، مشخص کردن ویژگی خاص حاوی یک آیتم، یا تغییر نام آیتمها در دستورات `use`
+- `async` - بازگرداندن یک `Future` به جای مسدود کردن Thread فعلی
+- `await` - متوقف کردن اجرا تا زمانی که نتیجه یک `Future` آماده شود
+- `break` - خروج فوری از یک حلقه
+- `const` - تعریف آیتمهای ثابت یا اشارهگرهای خام ثابت
+- `continue` - ادامه به تکرار بعدی حلقه
+- `crate` - در مسیر ماژول، به ریشه crate اشاره دارد
+- `dyn` - توزیع داینامیک به یک شیء ویژگی
+- `else` - حالت جایگزین برای سازههای جریان کنترلی `if` و `if let`
+- `enum` - تعریف یک شمارش
+- `extern` - لینک کردن یک تابع یا متغیر خارجی
+- `false` - مقدار بولی `false`
+- `fn` - تعریف یک تابع یا نوع اشارهگر تابع
+- `for` - حلقه بر روی آیتمهای یک iterator، پیادهسازی یک ویژگی، یا مشخص کردن طول عمر سطح بالاتر
+- `if` - انشعاب بر اساس نتیجه یک عبارت شرطی
+- `impl` - پیادهسازی قابلیتهای ذاتی یا ویژگی
+- `in` - بخشی از سینتکس حلقه `for`
+- `let` - متصل کردن یک متغیر
+- `loop` - حلقه بدون شرط
+- `match` - تطابق یک مقدار با الگوها
+- `mod` - تعریف یک ماژول
+- `move` - مالکیت متغیرهای گرفتهشده توسط Closure را منتقل میکند
+- `mut` - نشان دادن قابلیت تغییر در مراجع، اشارهگرهای خام، یا بایندینگ الگوها
+- `pub` - نشان دادن قابلیت مشاهده عمومی در فیلدهای struct، بلوکهای `impl`، یا ماژولها
+- `ref` - بایند کردن با مرجع
+- `return` - بازگرداندن از تابع
+- `Self` - یک نام مستعار برای نوعی که در حال تعریف یا پیادهسازی آن هستیم
+- `self` - موضوع متد یا ماژول فعلی
+- `static` - متغیر جهانی یا طول عمر طولانی تا زمان اجرای کامل برنامه
+- `struct` - تعریف یک ساختار
+- `super` - ماژول والد ماژول فعلی
+- `trait` - تعریف یک ویژگی
+- `true` - مقدار بولی `true`
+- `type` - تعریف یک نام مستعار نوع یا نوع وابسته
+- `union` - تعریف یک [union][union]؛ فقط هنگام استفاده در یک اعلان union یک کلمه کلیدی است
+- `unsafe` - نشان دادن کد، توابع، ویژگیها یا پیادهسازیهای ناامن
+- `use` - آوردن نمادها به دامنه؛ مشخص کردن برداشت دقیق برای محدودیتهای عمومی و طول عمر
+- `where` - مشخص کردن بندهایی که یک نوع را محدود میکنند
+- `while` - حلقه شرطی بر اساس نتیجه یک عبارت
[union]: ../reference/items/unions.html
-### Keywords Reserved for Future Use
+### کلمات کلیدی رزرو شده برای استفاده در آینده
-The following keywords do not yet have any functionality but are reserved by
-Rust for potential future use.
+کلمات کلیدی زیر هنوز هیچ کاربردی ندارند اما توسط Rust برای استفاده احتمالی در آینده رزرو شدهاند:
- `abstract`
- `become`
@@ -80,13 +69,11 @@ Rust for potential future use.
- `virtual`
- `yield`
-### Raw Identifiers
+### شناسههای خام
-_Raw identifiers_ are the syntax that lets you use keywords where they wouldn’t
-normally be allowed. You use a raw identifier by prefixing a keyword with `r#`.
+_شناسههای خام_ سینتکسی هستند که به شما اجازه میدهند از کلمات کلیدی در جایی که معمولاً مجاز نیستند استفاده کنید. برای استفاده از یک شناسه خام، یک `r#` قبل از کلمه کلیدی اضافه کنید.
-For example, `match` is a keyword. If you try to compile the following function
-that uses `match` as its name:
+برای مثال، `match` یک کلمه کلیدی است. اگر بخواهید تابع زیر را که از `match` به عنوان نام خود استفاده میکند کامپایل کنید:
Filename: src/main.rs
@@ -96,7 +83,7 @@ fn match(needle: &str, haystack: &str) -> bool {
}
```
-you’ll get this error:
+شما این خطا را دریافت خواهید کرد:
```text
error: expected identifier, found keyword `match`
@@ -106,9 +93,13 @@ error: expected identifier, found keyword `match`
| ^^^^^ expected identifier, found keyword
```
-The error shows that you can’t use the keyword `match` as the function
-identifier. To use `match` as a function name, you need to use the raw
-identifier syntax, like this:
+این خطا نشان میدهد که نمیتوانید از کلمه کلیدی `match` به عنوان شناسه تابع استفاده کنید. برای استفاده از `match` به عنوان نام تابع، باید از سینتکس شناسه خام به این شکل استفاده کنید:
+
+```rust
+fn r#match(needle: &str, haystack: &str) -> bool {
+ haystack.contains(needle)
+}
+```
Filename: src/main.rs
@@ -122,18 +113,32 @@ fn main() {
}
```
-This code will compile without any errors. Note the `r#` prefix on the function
-name in its definition as well as where the function is called in `main`.
+این کد بدون هیچ خطایی کامپایل خواهد شد. به پیشوند `r#` روی نام تابع در تعریف آن و همچنین جایی که تابع در `main` فراخوانی میشود، توجه کنید.
+
+شناسههای خام (_Raw identifiers_) به شما این امکان را میدهند که از هر کلمهای که انتخاب میکنید به عنوان شناسه استفاده کنید، حتی اگر آن کلمه بهطور معمول یک کلمه کلیدی رزروشده باشد. این ویژگی آزادی بیشتری برای انتخاب نام شناسهها فراهم میکند و همچنین امکان ادغام با برنامههایی که به زبانی نوشته شدهاند که این کلمات در آن زبان کلمات کلیدی نیستند، را میدهد. علاوه بر این، شناسههای خام به شما اجازه میدهند از کتابخانههایی استفاده کنید که با نسخهای از Rust نوشته شدهاند که با نسخه مورد استفاده شما متفاوت است.
+
+برای مثال، `try` در نسخه ۲۰۱۵ کلمه کلیدی نیست، اما در نسخههای ۲۰۱۸، ۲۰۲۱ و ۲۰۲۴ کلمه کلیدی است. اگر به کتابخانهای وابسته باشید که با نسخه ۲۰۱۵ نوشته شده و یک تابع به نام `try` دارد، باید از سینتکس شناسه خام، یعنی `r#try`، برای فراخوانی آن تابع از کد نسخه ۲۰۱۸ خود استفاده کنید. برای اطلاعات بیشتر در مورد نسخهها به [ضمیمه ه][appendix-e] مراجعه کنید.
+
+شناسههای خام (Raw identifiers) به شما این امکان
+را میدهند که از هر واژهای بهعنوان یک شناسه
+استفاده کنید، حتی اگر آن واژه یک کلمهی
+رزروشده باشد. این قابلیت، آزادی عمل بیشتری
+برای انتخاب نام شناسهها به ما میدهد و همچنین
+امکان یکپارچهسازی با برنامههایی که به زبانی نوشته
+شدهاند که این کلمات در آنها رزروشده نیستند
+را فراهم میکند. علاوه بر این، شناسههای خام
+به شما اجازه میدهند تا از کتابخانههایی استفاده
+کنید که با نگارشی متفاوت از crate شما نوشته
+شدهاند. برای مثال، `try` در نگارش ۲۰۱۵
+کلمهی کلیدی محسوب نمیشود، اما در نگارشهای
+۲۰۱۸، ۲۰۲۱ و ۲۰۲۴ یک کلمهی کلیدی است.
+اگر به کتابخانهای وابسته باشید که با نگارش
+۲۰۱۵ نوشته شده و تابعی با نام `try` دارد، برای
+فراخوانی این تابع در کد خود (با نگارشهای جدید)،
+باید از نحو شناسهی خام استفاده کنید، یعنی
+`r#try`. برای اطلاعات بیشتر دربارهی نگارشها
+به [ضمیمهی ه][appendix-e] مراجعه کنید.
-Raw identifiers allow you to use any word you choose as an identifier, even if
-that word happens to be a reserved keyword. This gives us more freedom to choose
-identifier names, as well as lets us integrate with programs written in a
-language where these words aren’t keywords. In addition, raw identifiers allow
-you to use libraries written in a different Rust edition than your crate uses.
-For example, `try` isn’t a keyword in the 2015 edition but is in the 2018, 2021,
-and 2024 editions. If you depend on a library that is written using the 2015
-edition and has a `try` function, you’ll need to use the raw identifier syntax,
-`r#try` in this case, to call that function from your code on later editions.
-See [Appendix E][appendix-e] for more information on editions.
[appendix-e]: appendix-05-editions.html
+
diff --git a/src/appendix-02-operators.md b/src/appendix-02-operators.md
index 36482f8486..23ee3c753b 100644
--- a/src/appendix-02-operators.md
+++ b/src/appendix-02-operators.md
@@ -1,206 +1,201 @@
-## Appendix B: Operators and Symbols
-
-This appendix contains a glossary of Rust’s syntax, including operators and
-other symbols that appear by themselves or in the context of paths, generics,
-trait bounds, macros, attributes, comments, tuples, and brackets.
-
-### Operators
-
-Table B-1 contains the operators in Rust, an example of how the operator would
-appear in context, a short explanation, and whether that operator is
-overloadable. If an operator is overloadable, the relevant trait to use to
-overload that operator is listed.
-
-Table B-1: Operators
-
-| Operator | Example | Explanation | Overloadable? |
-| ------------------------- | ------------------------------------------------------- | --------------------------------------------------------------------- | -------------- |
-| `!` | `ident!(...)`, `ident!{...}`, `ident![...]` | Macro expansion | |
-| `!` | `!expr` | Bitwise or logical complement | `Not` |
-| `!=` | `expr != expr` | Nonequality comparison | `PartialEq` |
-| `%` | `expr % expr` | Arithmetic remainder | `Rem` |
-| `%=` | `var %= expr` | Arithmetic remainder and assignment | `RemAssign` |
-| `&` | `&expr`, `&mut expr` | Borrow | |
-| `&` | `&type`, `&mut type`, `&'a type`, `&'a mut type` | Borrowed pointer type | |
-| `&` | `expr & expr` | Bitwise AND | `BitAnd` |
-| `&=` | `var &= expr` | Bitwise AND and assignment | `BitAndAssign` |
-| `&&` | `expr && expr` | Short-circuiting logical AND | |
-| `*` | `expr * expr` | Arithmetic multiplication | `Mul` |
-| `*=` | `var *= expr` | Arithmetic multiplication and assignment | `MulAssign` |
-| `*` | `*expr` | Dereference | `Deref` |
-| `*` | `*const type`, `*mut type` | Raw pointer | |
-| `+` | `trait + trait`, `'a + trait` | Compound type constraint | |
-| `+` | `expr + expr` | Arithmetic addition | `Add` |
-| `+=` | `var += expr` | Arithmetic addition and assignment | `AddAssign` |
-| `,` | `expr, expr` | Argument and element separator | |
-| `-` | `- expr` | Arithmetic negation | `Neg` |
-| `-` | `expr - expr` | Arithmetic subtraction | `Sub` |
-| `-=` | `var -= expr` | Arithmetic subtraction and assignment | `SubAssign` |
-| `->` | `fn(...) -> type`, |...| -> type | Function and closure return type | |
-| `.` | `expr.ident` | Field access | |
-| `.` | `expr.ident(expr, ...)` | Method call | |
-| `.` | `expr.0`, `expr.1`, etc. | Tuple indexing | |
-| `..` | `..`, `expr..`, `..expr`, `expr..expr` | Right-exclusive range literal | `PartialOrd` |
-| `..=` | `..=expr`, `expr..=expr` | Right-inclusive range literal | `PartialOrd` |
-| `..` | `..expr` | Struct literal update syntax | |
-| `..` | `variant(x, ..)`, `struct_type { x, .. }` | “And the rest” pattern binding | |
-| `...` | `expr...expr` | (Deprecated, use `..=` instead) In a pattern: inclusive range pattern | |
-| `/` | `expr / expr` | Arithmetic division | `Div` |
-| `/=` | `var /= expr` | Arithmetic division and assignment | `DivAssign` |
-| `:` | `pat: type`, `ident: type` | Constraints | |
-| `:` | `ident: expr` | Struct field initializer | |
-| `:` | `'a: loop {...}` | Loop label | |
-| `;` | `expr;` | Statement and item terminator | |
-| `;` | `[...; len]` | Part of fixed-size array syntax | |
-| `<<` | `expr << expr` | Left-shift | `Shl` |
-| `<<=` | `var <<= expr` | Left-shift and assignment | `ShlAssign` |
-| `<` | `expr < expr` | Less than comparison | `PartialOrd` |
-| `<=` | `expr <= expr` | Less than or equal to comparison | `PartialOrd` |
-| `=` | `var = expr`, `ident = type` | Assignment/equivalence | |
-| `==` | `expr == expr` | Equality comparison | `PartialEq` |
-| `=>` | `pat => expr` | Part of match arm syntax | |
-| `>` | `expr > expr` | Greater than comparison | `PartialOrd` |
-| `>=` | `expr >= expr` | Greater than or equal to comparison | `PartialOrd` |
-| `>>` | `expr >> expr` | Right-shift | `Shr` |
-| `>>=` | `var >>= expr` | Right-shift and assignment | `ShrAssign` |
-| `@` | `ident @ pat` | Pattern binding | |
-| `^` | `expr ^ expr` | Bitwise exclusive OR | `BitXor` |
-| `^=` | `var ^= expr` | Bitwise exclusive OR and assignment | `BitXorAssign` |
-| | | pat | pat | Pattern alternatives | |
-| | | expr | expr | Bitwise OR | `BitOr` |
-| |= | var |= expr | Bitwise OR and assignment | `BitOrAssign` |
-| || | expr || expr | Short-circuiting logical OR | |
-| `?` | `expr?` | Error propagation | |
-
-### Non-operator Symbols
-
-The following list contains all symbols that don’t function as operators; that
-is, they don’t behave like a function or method call.
-
-Table B-2 shows symbols that appear on their own and are valid in a variety of
-locations.
-
-Table B-2: Stand-Alone Syntax
-
-| Symbol | Explanation |
-| ---------------------------------------------------------------------- | ---------------------------------------------------------------------- |
-| `'ident` | Named lifetime or loop label |
-| Digits immediately followed by `u8`, `i32`, `f64`, `usize`, and so on | Numeric literal of specific type |
-| `"..."` | String literal |
-| `r"..."`, `r#"..."#`, `r##"..."##`, etc. | Raw string literal, escape characters not processed |
-| `b"..."` | Byte string literal; constructs an array of bytes instead of a string |
-| `br"..."`, `br#"..."#`, `br##"..."##`, etc. | Raw byte string literal, combination of raw and byte string literal |
-| `'...'` | Character literal |
-| `b'...'` | ASCII byte literal |
-| |...| expr | Closure |
-| `!` | Always empty bottom type for diverging functions |
-| `_` | “Ignored” pattern binding; also used to make integer literals readable |
-
-Table B-3 shows symbols that appear in the context of a path through the module
-hierarchy to an item.
-
-Table B-3: Path-Related Syntax
-
-| Symbol | Explanation |
-| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
-| `ident::ident` | Namespace path |
-| `::path` | Path relative to the extern prelude, where all other crates are rooted (i.e., an explicitly absolute path including crate name) |
-| `self::path` | Path relative to the current module (i.e., an explicitly relative path). |
-| `super::path` | Path relative to the parent of the current module |
-| `type::ident`, `::ident` | Associated constants, functions, and types |
-| `::...` | Associated item for a type that cannot be directly named (e.g., `<&T>::...`, `<[T]>::...`, etc.) |
-| `trait::method(...)` | Disambiguating a method call by naming the trait that defines it |
-| `type::method(...)` | Disambiguating a method call by naming the type for which it’s defined |
-| `::method(...)` | Disambiguating a method call by naming the trait and type |
-
-Table B-4 shows symbols that appear in the context of using generic type
-parameters.
-
-Table B-4: Generics
-
-| Symbol | Explanation |
-| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
-| `path<...>` | Specifies parameters to generic type in a type (e.g., `Vec`) |
-| `path::<...>`, `method::<...>` | Specifies parameters to generic type, function, or method in an expression; often referred to as turbofish (e.g., `"42".parse::()`) |
-| `fn ident<...> ...` | Define generic function |
-| `struct ident<...> ...` | Define generic structure |
-| `enum ident<...> ...` | Define generic enumeration |
-| `impl<...> ...` | Define generic implementation |
-| `for<...> type` | Higher-ranked lifetime bounds |
-| `type` | A generic type where one or more associated types have specific assignments (e.g., `Iterator- `) |
-
-Table B-5 shows symbols that appear in the context of constraining generic type
-parameters with trait bounds.
-
-Table B-5: Trait Bound Constraints
-
-| Symbol | Explanation |
-| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
-| `T: U` | Generic parameter `T` constrained to types that implement `U` |
-| `T: 'a` | Generic type `T` must outlive lifetime `'a` (meaning the type cannot transitively contain any references with lifetimes shorter than `'a`) |
-| `T: 'static` | Generic type `T` contains no borrowed references other than `'static` ones |
-| `'b: 'a` | Generic lifetime `'b` must outlive lifetime `'a` |
-| `T: ?Sized` | Allow generic type parameter to be a dynamically sized type |
-| `'a + trait`, `trait + trait` | Compound type constraint |
-
-Table B-6 shows symbols that appear in the context of calling or defining
-macros and specifying attributes on an item.
-
-Table B-6: Macros and Attributes
-
-| Symbol | Explanation |
+## ضمیمه ب: عملگرها و نمادها
+
+این ضمیمه شامل واژهنامهای از سینتکس زبان Rust است، از جمله عملگرها و سایر نمادهایی که به تنهایی یا در زمینه مسیرها، جنریکها، محدودیتهای ویژگی، ماکروها، ویژگیها، نظرات، تاپلها و براکتها ظاهر میشوند.
+
+### عملگرها
+
+جدول B-1 عملگرهای موجود در Rust، یک مثال از چگونگی ظاهر شدن عملگر در زمینه، توضیح کوتاه و اینکه آیا آن عملگر قابل اضافهبارگذاری است یا نه را نشان میدهد. اگر یک عملگر قابل اضافهبارگذاری باشد، ویژگی مرتبط برای اضافهبارگذاری آن عملگر ذکر شده است.
+
+جدول B-1: عملگرها
+
+| عملگر | مثال | توضیح | قابلیت بارگذاری مجدد (Overload)? | | | | |
+| ----- | ------------------------------------------------ | -------------------------------------------------------------- | -------------------------------- | --------------------------------------- | ------------- | --------------------- | - |
+| `!` | `ident!(...)`, `ident!{...}`, `ident![...]` | گسترش ماکرو | | | | | |
+| `!` | `!expr` | مکمل منطقی یا بیتی | `Not` | | | | |
+| `!=` | `expr != expr` | مقایسه نابرابری | `PartialEq` | | | | |
+| `%` | `expr % expr` | باقیمانده تقسیم | `Rem` | | | | |
+| `%=` | `var %= expr` | باقیمانده تقسیم و اختصاص | `RemAssign` | | | | |
+| `&` | `&expr`, `&mut expr` | قرضگیری | | | | | |
+| `&` | `&type`, `&mut type`, `&'a type`, `&'a mut type` | نوع اشارهگر قرضگرفتهشده | | | | | |
+| `&` | `expr & expr` | AND بیتی | `BitAnd` | | | | |
+| `&=` | `var &= expr` | AND بیتی و اختصاص | `BitAndAssign` | | | | |
+| `&&` | `expr && expr` | AND منطقی با قطع کوتاه | | | | | |
+| `*` | `expr * expr` | ضرب عددی | `Mul` | | | | |
+| `*=` | `var *= expr` | ضرب عددی و اختصاص | `MulAssign` | | | | |
+| `*` | `*expr` | dereference | `Deref` | | | | |
+| `*` | `*const type`, `*mut type` | اشارهگر خام | | | | | |
+| `+` | `trait + trait`, `'a + trait` | محدودیت ترکیبی برای نوع | | | | | |
+| `+` | `expr + expr` | جمع عددی | `Add` | | | | |
+| `+=` | `var += expr` | جمع عددی و اختصاص | `AddAssign` | | | | |
+| `,` | `expr, expr` | جداکننده آرگومان یا عنصر | | | | | |
+| `-` | `- expr` | منفی کردن عدد | `Neg` | | | | |
+| `-` | `expr - expr` | تفریق عددی | `Sub` | | | | |
+| `-=` | `var -= expr` | تفریق عددی و اختصاص | `SubAssign` | | | | |
+| `->` | `fn(...) -> type`, ` | ... | -> type` | نوع بازگشتی تابع یا closure | | | |
+| `.` | `expr.ident` | دسترسی به فیلد | | | | | |
+| `.` | `expr.ident(expr, ...)` | فراخوانی متد | | | | | |
+| `.` | `expr.0`, `expr.1`, ... | ایندکسگذاری tuple | | | | | |
+| `..` | `..`, `expr..`, `..expr`, `expr..expr` | بازهی راست-باز | `PartialOrd` | | | | |
+| `..=` | `..=expr`, `expr..=expr` | بازهی راست-بسته | `PartialOrd` | | | | |
+| `..` | `..expr` | بهروزرسانی literal ساختار | | | | | |
+| `..` | `variant(x, ..)`, `struct_type { x, .. }` | الگوی «و بقیه» در pattern binding | | | | | |
+| `...` | `expr...expr` | (منسوخشده، بهجای آن از `..=` استفاده کنید) الگوی بازهی بسته | | | | | |
+| `/` | `expr / expr` | تقسیم عددی | `Div` | | | | |
+| `/=` | `var /= expr` | تقسیم عددی و اختصاص | `DivAssign` | | | | |
+| `:` | `pat: type`, `ident: type` | محدودیت نوع | | | | | |
+| `:` | `ident: expr` | مقداردهی به فیلد ساختار | | | | | |
+| `:` | `'a: loop {...}` | برچسب حلقه | | | | | |
+| `;` | `expr;` | پایاندهنده عبارت یا آیتم | | | | | |
+| `;` | `[...; len]` | بخشی از نحوه تعریف آرایه با اندازه ثابت | | | | | |
+| `<<` | `expr << expr` | شیفت به چپ بیتی | `Shl` | | | | |
+| `<<=` | `var <<= expr` | شیفت به چپ بیتی و اختصاص | `ShlAssign` | | | | |
+| `<` | `expr < expr` | مقایسه کوچکتر از | `PartialOrd` | | | | |
+| `<=` | `expr <= expr` | مقایسه کوچکتر مساوی | `PartialOrd` | | | | |
+| `=` | `var = expr`, `ident = type` | تخصیص یا تساوی | | | | | |
+| `==` | `expr == expr` | مقایسه برابری | `PartialEq` | | | | |
+| `=>` | `pat => expr` | بخشی از نگارش match | | | | | |
+| `>` | `expr > expr` | مقایسه بزرگتر از | `PartialOrd` | | | | |
+| `>=` | `expr >= expr` | مقایسه بزرگتر مساوی | `PartialOrd` | | | | |
+| `>>` | `expr >> expr` | شیفت به راست بیتی | `Shr` | | | | |
+| `>>=` | `var >>= expr` | شیفت به راست بیتی و اختصاص | `ShrAssign` | | | | |
+| `@` | `ident @ pat` | pattern binding | | | | | |
+| `^` | `expr ^ expr` | XOR بیتی | `BitXor` | | | | |
+| `^=` | `var ^= expr` | XOR بیتی و اختصاص | `BitXorAssign` | | | | |
+| ` | ` | `pat | pat` | جایگزینهای الگو (pattern alternatives) | | | |
+| ` | ` | `expr | expr` | OR بیتی | `BitOr` | | |
+| ` | =` | `var | = expr` | OR بیتی و اختصاص | `BitOrAssign` | | |
+| ` | | ` | `expr | | expr` | OR منطقی با قطع کوتاه | |
+| `?` | `expr?` | انتشار خطا (error propagation) | | | | | |
+
+
+### نمادهای غیرعملگری
+
+لیست زیر شامل تمام نمادهایی است که به عنوان عملگر عمل نمیکنند؛ یعنی مانند یک تابع یا فراخوانی متد رفتار نمیکنند.
+
+جدول B-2 نمادهایی را نشان میدهد که به تنهایی ظاهر میشوند و در مکانهای مختلف معتبر هستند.
+
+جدول B-2: سینتکس مستقل
+
+| نماد | توضیح | | |
+| -------------------------------------------------------------- | ------------------------------------------------------------------- | ----------- | ------- |
+| `'ident` | lifetime نامگذاریشده یا برچسب حلقه | | |
+| اعداد بههمراه پسوندهایی مثل `u8`، `i32`، `f64`، `usize` و ... | عدد litteral با نوع مشخص | | |
+| `"..."` | رشته litteral | | |
+| `r"..."`، `r#"..."#`، `r##"..."##` و غیره | رشته خام؛ کاراکترهای escape تفسیر نمیشوند | | |
+| `b"..."` | رشته byte؛ آرایهای از بایت میسازد بهجای رشته | | |
+| `br"..."`، `br#"..."#`، `br##"..."##` و غیره | رشته byte خام؛ ترکیبی از رشته byte و رشته خام | | |
+| `'...'` | litteral کاراکتری | | |
+| `b'...'` | litteral بایت ASCII | | |
+|
| ... | expr | closure |
+| `!` | نوع تهی همواره خالی برای توابع واگرا (diverging) | | |
+| `_` | الگوی "نادیدهگرفتهشده"؛ همچنین برای خوانایی بهتر litteralهای عددی | | |
+
+جدول B-3 نمادهایی را نشان میدهد که در زمینه مسیریابی از سلسلهمراتب ماژول به یک آیتم ظاهر میشوند.
+
+جدول B-3: سینتکس مرتبط با مسیر
+
+| نماد | توضیح |
+| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
+| `ident::ident` | مسیر فضای نام |
+| `::path` | مسیر نسبی به پیشلود خارجی، جایی که تمام جعبهها (crates)ی دیگر ریشه دارند (یعنی یک مسیر مطلق که به وضوح شامل نام جعبه (crate) است) |
+| `self::path` | مسیر نسبی به ماژول جاری (یعنی یک مسیر نسبی به وضوح مشخصشده). |
+| `super::path` | مسیر نسبی به والد ماژول جاری |
+| `type::ident`, `::ident` | ثابتها، توابع، و انواع مرتبط |
+| `::...` | آیتم مرتبط برای نوعی که نمیتوان به طور مستقیم آن را نامگذاری کرد (مثلاً `<&T>::...`، `<[T]>::...`، و غیره) |
+| `trait::method(...)` | مشخص کردن فراخوانی متد با نامگذاری ویژگیای که آن را تعریف کرده است |
+| `type::method(...)` | مشخص کردن فراخوانی متد با نامگذاری نوعی که برای آن تعریف شده است |
+| `::method(...)` | مشخص کردن فراخوانی متد با نامگذاری ویژگی و نوع |
+
+جدول B-4 نمادهایی را نشان میدهد که در زمینه استفاده از پارامترهای نوع جنریک ظاهر میشوند.
+
+جدول B-4: جنریکها
+
+| نماد | توضیح |
+| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
+| `path<...>` | مشخص کردن پارامترها برای نوع جنریک در یک نوع (مثلاً `Vec`) |
+| `path::<...>`, `method::<...>`| مشخص کردن پارامترها برای نوع جنریک، تابع، یا متد در یک عبارت؛ که معمولاً به آن turbofish میگویند (مثلاً `"42".parse::()`) |
+| `fn ident<...> ...` | تعریف تابع جنریک |
+| `struct ident<...> ...` | تعریف ساختار جنریک |
+| `enum ident<...> ...` | تعریف شمارش جنریک |
+| `impl<...> ...` | تعریف پیادهسازی جنریک |
+| `for<...> type` | محدودیت طول عمر با رتبه بالاتر |
+| `type` | نوع جنریک که یک یا چند نوع مرتبط با آن دارای مقادیر مشخصی هستند (مثلاً `Iterator- `) |
+
+جدول B-5 نمادهایی را نشان میدهد که در زمینه محدود کردن پارامترهای نوع جنریک با محدودیتهای ویژگی ظاهر میشوند.
+
+جدول B-5: محدودیتهای ویژگی
+
+
+| نماد | توضیح |
+| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
+| `T: U` | پارامتر جنریک `T` محدود به انواع که `U` را پیادهسازی میکنند |
+| `T: 'a` | نوع جنریک `T` باید طول عمر بیشتری از طول عمر `'a` داشته باشد (یعنی نوع نمیتواند به صورت گذرا شامل ارجاعاتی با طول عمر کوتاهتر از `'a` باشد) |
+| `T: 'static` | نوع جنریک `T` شامل ارجاعات قرضگرفتهشدهای به جز ارجاعات `'static` نیست |
+| `'b: 'a` | طول عمر جنریک `'b` باید طول عمر بیشتری از طول عمر `'a` داشته باشد |
+| `T: ?Sized` | اجازه دادن به پارامتر نوع جنریک برای اینکه نوعی با اندازه پویا باشد |
+| `'a + trait`, `trait + trait` | محدودیت نوع ترکیبی |
+
+جدول B-6: ماکروها و ویژگیها
+
+| نماد | توضیح |
| ------------------------------------------- | ------------------ |
-| `#[meta]` | Outer attribute |
-| `#![meta]` | Inner attribute |
-| `$ident` | Macro substitution |
-| `$ident:kind` | Macro metavariable |
-| `$(...)...` | Macro repetition |
-| `ident!(...)`, `ident!{...}`, `ident![...]` | Macro invocation |
-
-Table B-7 shows symbols that create comments.
-
-Table B-7: Comments
-
-| Symbol | Explanation |
-| ---------- | ----------------------- |
-| `//` | Line comment |
-| `//!` | Inner line doc comment |
-| `///` | Outer line doc comment |
-| `/*...*/` | Block comment |
-| `/*!...*/` | Inner block doc comment |
-| `/**...*/` | Outer block doc comment |
-
-Table B-8 shows the contexts in which parentheses are used.
-
-Table B-8: Parentheses
-
-| Symbol | Explanation |
-| ------------------------ | ------------------------------------------------------------------------------------------- |
-| `()` | Empty tuple (aka unit), both literal and type |
-| `(expr)` | Parenthesized expression |
-| `(expr,)` | Single-element tuple expression |
-| `(type,)` | Single-element tuple type |
-| `(expr, ...)` | Tuple expression |
-| `(type, ...)` | Tuple type |
-| `expr(expr, ...)` | Function call expression; also used to initialize tuple `struct`s and tuple `enum` variants |
-
-Table B-9 shows the contexts in which curly braces are used.
-
-Table B-9: Curly Brackets
-
-| Context | Explanation |
-| ------------ | ---------------- |
-| `{...}` | Block expression |
-| `Type {...}` | Struct literal |
-
-Table B-10 shows the contexts in which square brackets are used.
-
-Table B-10: Square Brackets
-
-| Context | Explanation |
-| -------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
-| `[...]` | Array literal |
-| `[expr; len]` | Array literal containing `len` copies of `expr` |
-| `[type; len]` | Array type containing `len` instances of `type` |
-| `expr[expr]` | Collection indexing. Overloadable (`Index`, `IndexMut`) |
-| `expr[..]`, `expr[a..]`, `expr[..b]`, `expr[a..b]` | Collection indexing pretending to be collection slicing, using `Range`, `RangeFrom`, `RangeTo`, or `RangeFull` as the “index” |
+| `#[meta]` | attribute بیرونی |
+| `#![meta]` | attribute درونی |
+| `$ident` | جایگذاری در ماکرو |
+| `$ident:kind` | metavariable ماکرو |
+| `$(...)...` | تکرار در ماکرو |
+| `ident!(...)`، `ident!{...}`، `ident![...]` | فراخوانی ماکرو |
+
+جدول B-7: نظرات
+
+| نماد | توضیح |
+| ----------- | ----------------------- |
+| `//` | نظر تکخطی |
+| `//!` | نظر مستند داخلی تکخطی |
+| `///` | نظر مستند خارجی تکخطی |
+| `/*...*/` | نظر بلوکی |
+| `/*!...*/` | نظر مستند داخلی بلوکی |
+| `/**...*/` | نظر مستند خارجی بلوکی |
+
+جدول B-8: تاپلها
+
+جدول B-8 زمینههایی را نشان میدهد که در آنها از پرانتز استفاده میشود.
+
+جدول B-8: پرانتزها
+
+| نماد | توضیح |
+| ----------------- | ------------------------------------------------------------------------------------------- |
+| `()` | tuple تهی (یا همان unit)، هم بهصورت litteral و هم بهصورت نوع |
+| `(expr)` | عبارت داخل پرانتز |
+| `(expr,)` | عبارت tuple تکعضوی |
+| `(type,)` | نوع tuple تکعضوی |
+| `(expr, ...)` | عبارت tuple |
+| `(type, ...)` | نوع tuple |
+| `expr(expr, ...)` | فراخوانی تابع؛ همچنین برای مقداردهی به tuple structها و variantهای tuple enum بهکار میرود |
+
+جدول B-10: براکتها
+
+| زمینه | توضیح |
+| ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
+| `[...]` | لیترال آرایه |
+| `[expr; len]` | لیترال آرایه که شامل `len` نسخه از `expr` است |
+| `[type; len]` | نوع آرایه که شامل `len` نمونه از `type` است |
+| `expr[expr]` | اندیسگذاری مجموعه. قابل اضافهبارگذاری (`Index`, `IndexMut`) |
+| `expr[..]`, `expr[a..]`, `expr[..b]`, `expr[a..b]` | اندیسگذاری مجموعه که شبیه به برش مجموعه عمل میکند، با استفاده از `Range`، `RangeFrom`، `RangeTo`، یا `RangeFull` به عنوان "اندیس" |
+
+| زمینه | توضیح |
+| ------------ | -------------- |
+| `{...}` | عبارت block |
+| `Type {...}` | literal ساختار |
+
+جدول B-10 زمینههایی را نشان میدهد که در آنها از کروشه (براکت مربعی) استفاده میشود.
+
+جدول B-10: کروشهها (براکتهای مربعی)
+
+| زمینه | توضیح |
+| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
+| `[...]` | literal آرایه |
+| `[expr; len]` | literal آرایه شامل `len` نسخه از `expr` |
+| `[type; len]` | نوع آرایه شامل `len` نمونه از `type` |
+| `expr[expr]` | ایندکسگذاری روی مجموعهها. قابل بارگذاری مجدد (`Index`، `IndexMut`) |
+| `expr[..]`، `expr[a..]`، `expr[..b]`، `expr[a..b]` | ایندکسگذاری روی مجموعهها بهشکل شبیهسازیشدهی برش (slicing)، با استفاده از `Range`، `RangeFrom`، `RangeTo`، یا `RangeFull` |
diff --git a/src/appendix-03-derivable-traits.md b/src/appendix-03-derivable-traits.md
index 8e74b948c2..478630bbec 100644
--- a/src/appendix-03-derivable-traits.md
+++ b/src/appendix-03-derivable-traits.md
@@ -1,183 +1,131 @@
-## Appendix C: Derivable Traits
-
-In various places in the book, we’ve discussed the `derive` attribute, which
-you can apply to a struct or enum definition. The `derive` attribute generates
-code that will implement a trait with its own default implementation on the
-type you’ve annotated with the `derive` syntax.
-
-In this appendix, we provide a reference of all the traits in the standard
-library that you can use with `derive`. Each section covers:
-
-- What operators and methods deriving this trait will enable
-- What the implementation of the trait provided by `derive` does
-- What implementing the trait signifies about the type
-- The conditions in which you’re allowed or not allowed to implement the trait
-- Examples of operations that require the trait
-
-If you want different behavior from that provided by the `derive` attribute,
-consult the [standard library documentation](../std/index.html)
-for each trait for details on how to manually implement them.
-
-The traits listed here are the only ones defined by the standard library that
-can be implemented on your types using `derive`. Other traits defined in the
-standard library don’t have sensible default behavior, so it’s up to you to
-implement them in the way that makes sense for what you’re trying to accomplish.
-
-An example of a trait that can’t be derived is `Display`, which handles
-formatting for end users. You should always consider the appropriate way to
-display a type to an end user. What parts of the type should an end user be
-allowed to see? What parts would they find relevant? What format of the data
-would be most relevant to them? The Rust compiler doesn’t have this insight, so
-it can’t provide appropriate default behavior for you.
-
-The list of derivable traits provided in this appendix is not comprehensive:
-libraries can implement `derive` for their own traits, making the list of
-traits you can use `derive` with truly open-ended. Implementing `derive`
-involves using a procedural macro, which is covered in the
-[“Macros”][macros] section of Chapter 20.
-
-### `Debug` for Programmer Output
-
-The `Debug` trait enables debug formatting in format strings, which you
-indicate by adding `:?` within `{}` placeholders.
-
-The `Debug` trait allows you to print instances of a type for debugging
-purposes, so you and other programmers using your type can inspect an instance
-at a particular point in a program’s execution.
-
-The `Debug` trait is required, for example, in the use of the `assert_eq!`
-macro. This macro prints the values of instances given as arguments if the
-equality assertion fails so programmers can see why the two instances weren’t
-equal.
-
-### `PartialEq` and `Eq` for Equality Comparisons
-
-The `PartialEq` trait allows you to compare instances of a type to check for
-equality and enables use of the `==` and `!=` operators.
-
-Deriving `PartialEq` implements the `eq` method. When `PartialEq` is derived on
-structs, two instances are equal only if _all_ fields are equal, and the
-instances are not equal if any fields are not equal. When derived on enums,
-each variant is equal to itself and not equal to the other variants.
-
-The `PartialEq` trait is required, for example, with the use of the
-`assert_eq!` macro, which needs to be able to compare two instances of a type
-for equality.
-
-The `Eq` trait has no methods. Its purpose is to signal that for every value of
-the annotated type, the value is equal to itself. The `Eq` trait can only be
-applied to types that also implement `PartialEq`, although not all types that
-implement `PartialEq` can implement `Eq`. One example of this is floating point
-number types: the implementation of floating point numbers states that two
-instances of the not-a-number (`NaN`) value are not equal to each other.
-
-An example of when `Eq` is required is for keys in a `HashMap` so the
-`HashMap` can tell whether two keys are the same.
-
-### `PartialOrd` and `Ord` for Ordering Comparisons
-
-The `PartialOrd` trait allows you to compare instances of a type for sorting
-purposes. A type that implements `PartialOrd` can be used with the `<`, `>`,
-`<=`, and `>=` operators. You can only apply the `PartialOrd` trait to types
-that also implement `PartialEq`.
-
-Deriving `PartialOrd` implements the `partial_cmp` method, which returns an
-`Option` that will be `None` when the values given don’t produce an
-ordering. An example of a value that doesn’t produce an ordering, even though
-most values of that type can be compared, is the not-a-number (`NaN`) floating
-point value. Calling `partial_cmp` with any floating-point number and the `NaN`
-floating-point value will return `None`.
-
-When derived on structs, `PartialOrd` compares two instances by comparing the
-value in each field in the order in which the fields appear in the struct
-definition. When derived on enums, variants of the enum declared earlier in the
-enum definition are considered less than the variants listed later.
-
-The `PartialOrd` trait is required, for example, for the `gen_range` method
-from the `rand` crate that generates a random value in the range specified by a
-range expression.
-
-The `Ord` trait allows you to know that for any two values of the annotated
-type, a valid ordering will exist. The `Ord` trait implements the `cmp` method,
-which returns an `Ordering` rather than an `Option` because a valid
-ordering will always be possible. You can only apply the `Ord` trait to types
-that also implement `PartialOrd` and `Eq` (and `Eq` requires `PartialEq`). When
-derived on structs and enums, `cmp` behaves the same way as the derived
-implementation for `partial_cmp` does with `PartialOrd`.
-
-An example of when `Ord` is required is when storing values in a `BTreeSet`,
-a data structure that stores data based on the sort order of the values.
-
-### `Clone` and `Copy` for Duplicating Values
-
-The `Clone` trait allows you to explicitly create a deep copy of a value, and
-the duplication process might involve running arbitrary code and copying heap
-data. See [Variables and Data Interacting with
-Clone”][variables-and-data-interacting-with-clone] in Chapter 4
-for more information on `Clone`.
-
-Deriving `Clone` implements the `clone` method, which when implemented for the
-whole type, calls `clone` on each of the parts of the type. This means all the
-fields or values in the type must also implement `Clone` to derive `Clone`.
-
-An example of when `Clone` is required is when calling the `to_vec` method on a
-slice. The slice doesn’t own the type instances it contains, but the vector
-returned from `to_vec` will need to own its instances, so `to_vec` calls
-`clone` on each item. Thus the type stored in the slice must implement `Clone`.
-
-The `Copy` trait allows you to duplicate a value by only copying bits stored on
-the stack; no arbitrary code is necessary. See [“Stack-Only Data:
-Copy”][stack-only-data-copy] in Chapter 4 for more information on
-`Copy`.
-
-The `Copy` trait doesn’t define any methods to prevent programmers from
-overloading those methods and violating the assumption that no arbitrary code
-is being run. That way, all programmers can assume that copying a value will be
-very fast.
-
-You can derive `Copy` on any type whose parts all implement `Copy`. A type that
-implements `Copy` must also implement `Clone`, because a type that implements
-`Copy` has a trivial implementation of `Clone` that performs the same task as
-`Copy`.
-
-The `Copy` trait is rarely required; types that implement `Copy` have
-optimizations available, meaning you don’t have to call `clone`, which makes
-the code more concise.
-
-Everything possible with `Copy` you can also accomplish with `Clone`, but the
-code might be slower or have to use `clone` in places.
-
-### `Hash` for Mapping a Value to a Value of Fixed Size
-
-The `Hash` trait allows you to take an instance of a type of arbitrary size and
-map that instance to a value of fixed size using a hash function. Deriving
-`Hash` implements the `hash` method. The derived implementation of the `hash`
-method combines the result of calling `hash` on each of the parts of the type,
-meaning all fields or values must also implement `Hash` to derive `Hash`.
-
-An example of when `Hash` is required is in storing keys in a `HashMap`
-to store data efficiently.
-
-### `Default` for Default Values
-
-The `Default` trait allows you to create a default value for a type. Deriving
-`Default` implements the `default` function. The derived implementation of the
-`default` function calls the `default` function on each part of the type,
-meaning all fields or values in the type must also implement `Default` to
-derive `Default`.
-
-The `Default::default` function is commonly used in combination with the struct
-update syntax discussed in [“Creating Instances from Other Instances with Struct
-Update
-Syntax”][creating-instances-from-other-instances-with-struct-update-syntax] in Chapter 5. You can customize a few fields of a struct and then set
-and use a default value for the rest of the fields by using
-`..Default::default()`.
-
-The `Default` trait is required when you use the method `unwrap_or_default` on
-`Option` instances, for example. If the `Option` is `None`, the method
-`unwrap_or_default` will return the result of `Default::default` for the type
-`T` stored in the `Option`.
+## ضمیمه ج: ویژگیهای قابل اشتقاق
+
+در بخشهای مختلف کتاب، ما درباره ویژگی `derive` صحبت کردیم که میتوانید آن را به تعریف یک struct یا enum اعمال کنید. ویژگی `derive` کدی تولید میکند که یک ویژگی را با پیادهسازی پیشفرض خود روی نوعی که با سینتکس `derive` حاشیهنویسی کردهاید، پیادهسازی میکند.
+
+در این ضمیمه، مرجعی از تمام ویژگیهای موجود در کتابخانه استاندارد که میتوانید با `derive` استفاده کنید ارائه میشود. هر بخش شامل موارد زیر است:
+
+- چه عملگرها و متدهایی با مشتقسازی این ویژگی فعال میشوند
+- پیادهسازی ویژگی که توسط `derive` ارائه میشود چه میکند
+- پیادهسازی ویژگی چه مفهومی درباره نوع دارد
+- شرایطی که در آن اجازه یا عدم اجازه پیادهسازی ویژگی داده میشود
+- مثالهایی از عملیاتهایی که به این ویژگی نیاز دارند
+
+اگر رفتاری متفاوت از آنچه ویژگی `derive` ارائه میدهد
+میخواهید، به [مستندات کتابخانه استاندارد](https://doc.rust-lang.org/std/index.html)
+برای هر trait مراجعه کنید تا جزئیات نحوهی پیادهسازی
+دستی آنها را ببینید.
+
+traitهایی که در اینجا فهرست شدهاند، تنها traitهایی
+هستند که توسط کتابخانه استاندارد تعریف شدهاند
+و میتوان آنها را با استفاده از `derive` روی نوعهای
+خود پیادهسازی کرد. سایر traitهای موجود در کتابخانه
+استاندارد رفتاری پیشفرض و معنادار ندارند، بنابراین
+پیادهسازی آنها بهصورتی که با هدف شما سازگار باشد
+برعهدهی خودتان است.
+
+مثالی از یک ویژگی که نمیتواند مشتق شود، `Display` است که فرمتدهی برای کاربران نهایی را مدیریت میکند. شما باید همیشه راه مناسب برای نمایش یک نوع به کاربر نهایی را در نظر بگیرید. چه بخشهایی از نوع باید به کاربر نهایی نشان داده شود؟ چه بخشهایی برای او مرتبط است؟ چه فرمتی از داده برای او بیشترین اهمیت را دارد؟ کامپایلر Rust این بینش را ندارد، بنابراین نمیتواند رفتار پیشفرض مناسب را برای شما فراهم کند.
+
+لیست ویژگیهای قابل اشتقاق ارائهشده در این ضمیمه جامع نیست: کتابخانهها میتوانند `derive` را برای ویژگیهای خود پیادهسازی کنند و لیست ویژگیهایی که میتوانید با `derive` استفاده کنید را بهطور واقعی باز بگذارند. پیادهسازی `derive` شامل استفاده از یک ماکروی فرآیندی است که در بخش [“ماکروها”][macros] از فصل 20 پوشش داده شده است.
+
+### `Debug` برای خروجی برنامهنویسی
+
+ویژگی `Debug` فرمتدهی دیباگ را در رشتههای فرمت فعال میکند که با افزودن `:?` درون نگهدارندههای `{}` مشخص میکنید.
+
+ویژگی `Debug` به شما اجازه میدهد نمونههایی از یک نوع را برای مقاصد دیباگ چاپ کنید، بهطوریکه شما و سایر برنامهنویسانی که از نوع شما استفاده میکنند بتوانید نمونهای را در یک نقطه خاص از اجرای برنامه بررسی کنید.
+
+برای مثال، trait `Debug` در استفاده از ماکروی
+`assert_eq!` الزامی است. این ماکرو در صورتی
+که بررسی تساوی با شکست مواجه شود، مقادیر
+نمونههایی را که بهعنوان آرگومان دریافت کرده
+است چاپ میکند تا برنامهنویس بتواند دلیل نابرابری
+دو نمونه را مشاهده کند.
+
+### `PartialEq` و `Eq` برای مقایسه برابری
+
+ویژگی `PartialEq` به شما اجازه میدهد نمونههای یک نوع را برای بررسی برابری مقایسه کنید و استفاده از عملگرهای `==` و `!=` را ممکن میسازد.
+
+مشتقسازی `PartialEq` متد `eq` را پیادهسازی میکند. وقتی `PartialEq` روی structها مشتق میشود، دو نمونه فقط زمانی برابر هستند که _تمام_ فیلدها برابر باشند و نمونهها برابر نیستند اگر هر یک از فیلدها برابر نباشند. وقتی روی enumها مشتق میشود، هر واریانت با خودش برابر است و با سایر واریانتها برابر نیست.
+
+ویژگی `PartialEq`، برای مثال، با استفاده از ماکروی `assert_eq!` مورد نیاز است که باید بتواند دو نمونه از یک نوع را برای برابری مقایسه کند.
+
+ویژگی `Eq` هیچ متدی ندارد. هدف آن این است که نشان دهد برای هر مقدار از نوع حاشیهنویسیشده، مقدار با خودش برابر است. ویژگی `Eq` فقط میتواند به نوعهایی اعمال شود که همچنین `PartialEq` را پیادهسازی کرده باشند، اگرچه همه نوعهایی که `PartialEq` را پیادهسازی کردهاند نمیتوانند `Eq` را پیادهسازی کنند. مثالی از این مورد نوعهای عدد ممیز شناور هستند: پیادهسازی اعداد ممیز شناور بیان میکند که دو نمونه از مقدار غیرعدد (`NaN`) برابر نیستند.
+
+مثالی از زمانی که `Eq` مورد نیاز است، برای کلیدها در `HashMap` است تا `HashMap` بتواند تعیین کند که آیا دو کلید یکسان هستند یا نه.
+
+
+### `PartialOrd` و `Ord` برای مقایسه مرتبسازی
+
+مشتقگیری از `PartialOrd` باعث پیادهسازی
+متد `partial_cmp` میشود، که یک `Option`
+بازمیگرداند؛ این مقدار در صورتی `None` خواهد بود
+که مقادیر دادهشده نتوانند ترتیب مشخصی تولید کنند.
+مثالی از مقداری که ترتیبپذیر نیست، هرچند بیشتر
+مقادیر آن نوع قابل مقایسهاند، مقدار `NaN` در
+اعداد اعشاری (floating point) است. فراخوانی
+`partial_cmp` با هر عدد اعشاری و مقدار `NaN`
+منجر به بازگشت `None` میشود.
+
+مشتقسازی `PartialOrd` متد `partial_cmp` را پیادهسازی میکند، که یک `Option` را برمیگرداند که در صورتی که مقادیر دادهشده ترتیببندی تولید نکنند، `None` خواهد بود. مثالی از مقداری که ترتیببندی تولید نمیکند، حتی اگر بیشتر مقادیر آن نوع قابل مقایسه باشند، مقدار نقطه شناور غیرعدد (`NaN`) است. فراخوانی `partial_cmp` با هر عدد شناور و مقدار `NaN` نقطه شناور `None` را برمیگرداند.
+
+وقتی روی structها مشتق میشود، `PartialOrd` دو نمونه را با مقایسه مقدار هر فیلد به ترتیب ظاهر شدن فیلدها در تعریف struct مقایسه میکند. وقتی روی enumها مشتق میشود، واریانتهای enum که زودتر در تعریف enum اعلام شدهاند، کمتر از واریانتهایی در نظر گرفته میشوند که بعداً فهرست شدهاند.
+
+ویژگی `PartialOrd`، برای مثال، برای متد `gen_range` از crate `rand` مورد نیاز است که یک مقدار تصادفی در محدوده مشخصشده توسط یک عبارت محدوده تولید میکند.
+
+ویژگی `Ord` به شما امکان میدهد بدانید که برای هر دو مقدار از نوع حاشیهنویسیشده، یک ترتیببندی معتبر وجود خواهد داشت. ویژگی `Ord` متد `cmp` را پیادهسازی میکند، که به جای `Option`، یک `Ordering` را برمیگرداند زیرا یک ترتیببندی معتبر همیشه ممکن خواهد بود. شما فقط میتوانید ویژگی `Ord` را به نوعهایی اعمال کنید که همچنین `PartialOrd` و `Eq` را پیادهسازی کرده باشند (و `Eq` نیازمند `PartialEq` است). وقتی روی structها و enumها مشتق میشود، `cmp` به همان شکلی عمل میکند که پیادهسازی مشتقشده برای `partial_cmp` در `PartialOrd` عمل میکند.
+
+مثالی از زمانی که `Ord` مورد نیاز است، هنگام ذخیره مقادیر در `BTreeSet` است، یک ساختار داده که دادهها را بر اساس ترتیب مرتبسازی مقادیر ذخیره میکند.
+
+trait `Clone` به شما امکان میدهد که بهصورت
+صریح یک کپی عمیق از یک مقدار ایجاد کنید،
+و این فرایند تکثیر ممکن است شامل اجرای کد دلخواه
+و کپیکردن دادهها از حافظه heap باشد. برای اطلاعات
+بیشتر دربارهی `Clone` به بخش [«متغیرها و دادهها در تعامل با Clone»][variables-and-data-interacting-with-clone]
+در فصل ۴ مراجعه کنید.
+
+مثالی از جایی که `Clone` مورد نیاز است، هنگام
+فراخوانی متد `to_vec` روی یک slice میباشد.
+slice مالک نمونههای نوعی که در خود دارد نیست،
+اما برداری که از `to_vec` بازمیگردد باید مالک
+این نمونهها باشد، بنابراین `to_vec` روی هر آیتم
+تابع `clone` را فراخوانی میکند. از این رو، نوعی
+که درون slice ذخیره شده باید trait `Clone` را
+پیادهسازی کرده باشد.
+
+trait `Copy` به شما اجازه میدهد که یک مقدار را
+تنها با کپیکردن بیتهای ذخیرهشده در stack
+تکثیر کنید؛ هیچ کد دلخواهی اجرا نمیشود.
+برای اطلاعات بیشتر دربارهی `Copy` به بخش
+[«دادههای فقط-روی-استک: Copy»][stack-only-data-copy]
+در فصل ۴ مراجعه کنید.
+
+ویژگی `Copy` به شما امکان میدهد یک مقدار را با کپی کردن بیتهای ذخیرهشده روی stack تکثیر کنید؛ هیچ کد دلخواهی لازم نیست. برای اطلاعات بیشتر درباره `Copy`، به بخش [“دادههای فقط stack: Copy”][stack-only-data-copy] در فصل 4 مراجعه کنید.
+
+ویژگی `Copy` هیچ متدی را تعریف نمیکند تا از اضافهبارگذاری آن متدها توسط برنامهنویسان و نقض فرضی که هیچ کد دلخواهی اجرا نمیشود جلوگیری کند. به این ترتیب، تمام برنامهنویسان میتوانند فرض کنند که کپی کردن یک مقدار بسیار سریع خواهد بود.
+
+شما میتوانید `Copy` را روی هر نوعی مشتق کنید که تمام اجزای آن `Copy` را پیادهسازی میکنند. نوعی که `Copy` را پیادهسازی میکند باید همچنین `Clone` را پیادهسازی کند، زیرا نوعی که `Copy` را پیادهسازی میکند دارای پیادهسازی سادهای از `Clone` است که همان وظیفه را به عنوان `Copy` انجام میدهد.
+
+ویژگی `Copy` به ندرت مورد نیاز است؛ نوعهایی که `Copy` را پیادهسازی میکنند بهینهسازیهایی در دسترس دارند، به این معنا که شما نیازی به فراخوانی `clone` ندارید، که کد را مختصرتر میکند.
+
+هر چیزی که با `Copy` ممکن است را میتوانید با `Clone` نیز انجام دهید، اما کد ممکن است کندتر باشد یا نیاز به استفاده از `clone` در مکانهای مختلف داشته باشد.
+
+
+### `Hash` برای نگاشت مقدار به مقدار با اندازه ثابت
+
+ویژگی `Hash` به شما امکان میدهد یک نمونه از نوعی با اندازه دلخواه بگیرید و آن نمونه را با استفاده از یک تابع هش به مقدار با اندازه ثابت نگاشت کنید. مشتقسازی `Hash` متد `hash` را پیادهسازی میکند. پیادهسازی مشتقشده متد `hash` نتیجه فراخوانی `hash` روی هر یک از بخشهای نوع را ترکیب میکند، به این معنی که تمام فیلدها یا مقادیر نیز باید `Hash` را پیادهسازی کنند تا `Hash` مشتق شود.
+
+مثالی از زمانی که `Hash` مورد نیاز است، هنگام ذخیره کلیدها در `HashMap` برای ذخیره دادهها به صورت کارآمد است.
+
+تابع `Default::default` معمولاً همراه با نگارش
+بهروزرسانی ساختار (struct update syntax) که در
+بخش [«ایجاد نمونههایی از نمونههای دیگر با استفاده از نگارش بهروزرسانی ساختار»][creating-instances-from-other-instances-with-struct-update-syntax]
+در فصل ۵ توضیح داده شده، استفاده میشود.
+میتوانید تنها چند فیلد از یک ساختار را شخصیسازی کنید
+و سپس برای فیلدهای باقیمانده از مقدار پیشفرض
+با استفاده از `..Default::default()` بهره ببرید.
+
+تابع `Default::default` معمولاً به همراه سینتکس بهروزرسانی ساختار که در بخش [“ایجاد نمونهها از نمونههای دیگر با سینتکس بهروزرسانی ساختار”][creating-instances-from-other-instances-with-struct-update-syntax] در فصل 5 مورد بحث قرار گرفته است، استفاده میشود. میتوانید چند فیلد از یک ساختار را سفارشی کنید و سپس یک مقدار پیشفرض برای بقیه فیلدها با استفاده از `..Default::default()` تنظیم و استفاده کنید.
+
+ویژگی `Default`، برای مثال، زمانی مورد نیاز است که از متد `unwrap_or_default` روی نمونههای `Option` استفاده میکنید. اگر `Option` برابر با `None` باشد، متد `unwrap_or_default` نتیجه `Default::default` را برای نوع `T` ذخیرهشده در `Option` برمیگرداند.
[creating-instances-from-other-instances-with-struct-update-syntax]: ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
[stack-only-data-copy]: ch04-01-what-is-ownership.html#stack-only-data-copy
diff --git a/src/appendix-04-useful-development-tools.md b/src/appendix-04-useful-development-tools.md
index 02dd6490cb..b2ef577b09 100644
--- a/src/appendix-04-useful-development-tools.md
+++ b/src/appendix-04-useful-development-tools.md
@@ -1,35 +1,35 @@
-## Appendix D - Useful Development Tools
+## ضمیمه د - ابزارهای مفید توسعه
-In this appendix, we talk about some useful development tools that the Rust
-project provides. We’ll look at automatic formatting, quick ways to apply
-warning fixes, a linter, and integrating with IDEs.
+در این ضمیمه، ما درباره برخی ابزارهای مفید توسعه که پروژه Rust ارائه میدهد صحبت میکنیم. به فرمتدهی خودکار، روشهای سریع برای اعمال اصلاحات هشدارها، یک تحلیلگر کد (linter) و یکپارچهسازی با محیطهای توسعه یکپارچه (IDE) خواهیم پرداخت.
-### Automatic Formatting with `rustfmt`
+### فرمتدهی خودکار با `rustfmt`
-The `rustfmt` tool reformats your code according to the community code style.
-Many collaborative projects use `rustfmt` to prevent arguments about which
-style to use when writing Rust: everyone formats their code using the tool.
-
-Rust installations include `rustfmt` by default, so you should already have the
-programs `rustfmt` and `cargo-fmt` on your system. These two commands are
-analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
-and `cargo-fmt` understands conventions of a project that uses Cargo. To format
-any Cargo project, enter the following:
+نصبهای Rust بهصورت پیشفرض شامل `rustfmt`
+هستند، بنابراین احتمالاً هماکنون برنامههای
+`rustfmt` و `cargo-fmt` روی سیستم شما
+نصب شدهاند. این دو دستور همانند `rustc` و `cargo`
+هستند؛ به این صورت که `rustfmt` کنترل دقیقتری
+ارائه میدهد و `cargo-fmt` با ساختار و قراردادهای
+پروژههای مبتنی بر Cargo آشنایی دارد. برای قالببندی
+هر پروژهی Cargo، دستور زیر را وارد کنید:
```console
$ cargo fmt
```
-Running this command reformats all the Rust code in the current crate. This
-should only change the code style, not the code semantics. For more information
-on `rustfmt`, see [its documentation][rustfmt].
+اجرای این دستور تمام کدهای Rust در crate فعلی را مجدداً فرمت میکند. این کار باید فقط سبک کدنویسی را تغییر دهد، نه معنای کد را. برای اطلاعات بیشتر در مورد `rustfmt`، به [مستندات آن][rustfmt] مراجعه کنید.
+
+[rustfmt]: https://github.com/rust-lang/rustfmt
-### Fix Your Code with `rustfix`
+### اصلاح کد شما با `rustfix`
-The `rustfix` tool is included with Rust installations and can automatically fix
-compiler warnings that have a clear way to correct the problem that’s likely
-what you want. You’ve probably seen compiler warnings before. For example,
-consider this code:
+ابزار `rustfix` همراه با نصب Rust ارائه میشود و
+میتواند هشدارهای کامپایلر را بهصورت خودکار
+برطرف کند، آن هم در مواردی که راهحل مشخصی
+برای رفع مشکل وجود دارد و احتمالاً همان چیزی است
+که شما انتظار دارید. احتمالاً پیش از این با
+هشدارهای کامپایلر روبهرو شدهاید. برای مثال،
+به کد زیر توجه کنید:
Filename: src/main.rs
@@ -40,8 +40,9 @@ fn main() {
}
```
-Here, we’re defining the variable `x` as mutable, but we never actually mutate
-it. Rust warns us about that:
+در اینجا، متغیر `x` را بهصورت قابلتغییر (mutable)
+تعریف کردهایم، اما در عمل هیچگاه آن را تغییر نمیدهیم.
+Rust در این مورد به ما هشدار میدهد:
```console
$ cargo build
@@ -57,9 +58,10 @@ warning: variable does not need to be mutable
= note: `#[warn(unused_mut)]` on by default
```
-The warning suggests that we remove the `mut` keyword. We can automatically
-apply that suggestion using the `rustfix` tool by running the command `cargo
-fix`:
+هشدار پیشنهاد میدهد که کلمهی کلیدی `mut`
+را حذف کنیم. میتوانیم این پیشنهاد را بهصورت
+خودکار با استفاده از ابزار `rustfix` و اجرای دستور
+`cargo fix` اعمال کنیم:
```console
$ cargo fix
@@ -68,8 +70,7 @@ $ cargo fix
Finished dev [unoptimized + debuginfo] target(s) in 0.59s
```
-When we look at _src/main.rs_ again, we’ll see that `cargo fix` has changed the
-code:
+وقتی دوباره فایل _src/main.rs_ را بررسی کنیم، خواهیم دید که `cargo fix` کد را تغییر داده است:
Filename: src/main.rs
@@ -80,25 +81,25 @@ fn main() {
}
```
-The variable `x` is now immutable, and the warning no longer appears.
+متغیر `x` اکنون غیرقابلتغییر (immutable) شده است
+و هشدار نیز دیگر نمایش داده نمیشود.
-You can also use the `cargo fix` command to transition your code between
-different Rust editions. Editions are covered in [Appendix E][editions].
+همچنین میتوانید از دستور `cargo fix` برای انتقال کد خود بین نسخههای مختلف Rust استفاده کنید. نسخهها در [ضمیمه ه][editions] پوشش داده شدهاند.
-### More Lints with Clippy
+### لینتهای بیشتر با Clippy
-The Clippy tool is a collection of lints to analyze your code so you can catch
-common mistakes and improve your Rust code. Clippy is included with standard
-Rust installations.
+ابزار Clippy مجموعهای از lintها برای تحلیل کد شماست
+تا بتوانید خطاهای رایج را شناسایی کرده و کد Rust خود
+را بهبود دهید. Clippy همراه با نصب استاندارد Rust
+در دسترس است.
-To run Clippy’s lints on any Cargo project, enter the following:
+برای اجرای تحلیلگرهای Clippy روی هر پروژه Cargo، دستور زیر را وارد کنید:
```console
$ cargo clippy
```
-For example, say you write a program that uses an approximation of a
-mathematical constant, such as pi, as this program does:
+به عنوان مثال، فرض کنید برنامهای مینویسید که از یک مقدار تقریبی برای یک ثابت ریاضی، مانند pi، استفاده میکند، همانطور که این برنامه انجام میدهد:
@@ -112,7 +113,7 @@ fn main() {
-Running `cargo clippy` on this project results in this error:
+اجرای `cargo clippy` روی این پروژه منجر به این خطا میشود:
```text
error: approximate value of `f{32, 64}::consts::PI` found
@@ -126,11 +127,13 @@ error: approximate value of `f{32, 64}::consts::PI` found
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
```
-This error lets you know that Rust already has a more precise `PI` constant
-defined, and that your program would be more correct if you used the constant
-instead. You would then change your code to use the `PI` constant.
+این خطا به شما اطلاع میدهد که Rust از پیش
+ثابت `PI` دقیقتری را تعریف کرده است، و استفاده از
+این ثابت در برنامهتان باعث درستی بیشتر کد میشود.
+بنابراین باید کد خود را طوری تغییر دهید که از
+ثابت `PI` استفاده کند.
-The following code doesn’t result in any errors or warnings from Clippy:
+کد زیر هیچ خطا یا هشداری از Clippy ایجاد نمیکند:
@@ -144,21 +147,25 @@ fn main() {
-For more information on Clippy, see [its documentation][clippy].
-
-### IDE Integration Using `rust-analyzer`
-
-To help with IDE integration, the Rust community recommends using
-[`rust-analyzer`][rust-analyzer]. This tool is a set of
-compiler-centric utilities that speak [Language Server Protocol][lsp], which is a specification for IDEs and programming languages to
-communicate with each other. Different clients can use `rust-analyzer`, such as
-[the Rust analyzer plug-in for Visual Studio Code][vscode].
-
-Visit the `rust-analyzer` project’s [home page][rust-analyzer]
-for installation instructions, then install the language server support in your
-particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
-definition, and inline errors.
+برای اطلاعات بیشتر درباره Clippy، به [مستندات آن][clippy] مراجعه کنید.
+
+### یکپارچهسازی IDE با استفاده از `rust-analyzer`
+
+برای یکپارچهسازی بهتر با محیطهای توسعه (IDE)،
+جامعهی Rust استفاده از [`rust-analyzer`][rust-analyzer]
+را توصیه میکند. این ابزار مجموعهای از ابزارهای
+وابسته به کامپایلر است که با [پروتکل زبان سرور (LSP)][lsp]
+ارتباط برقرار میکند؛ این پروتکل مشخصاتی است برای
+ارتباط میان IDEها و زبانهای برنامهنویسی.
+کلاینتهای مختلفی میتوانند از `rust-analyzer`
+استفاده کنند، مانند [افزونهی Rust Analyzer برای
+Visual Studio Code][vscode].
+
+برای دریافت دستورالعمل نصب، به [صفحهی اصلی
+پروژهی `rust-analyzer`][rust-analyzer] مراجعه کنید،
+سپس پشتیبانی از language server را در IDE خود نصب نمایید.
+پس از آن، امکاناتی مانند تکمیل خودکار، پرش به تعریف،
+و نمایش خطاها بهصورت درونخطی به IDE شما اضافه خواهد شد.
[rustfmt]: https://github.com/rust-lang/rustfmt
[editions]: appendix-05-editions.md
diff --git a/src/appendix-05-editions.md b/src/appendix-05-editions.md
index 0b8c4a3a12..91e3cc09bf 100644
--- a/src/appendix-05-editions.md
+++ b/src/appendix-05-editions.md
@@ -1,57 +1,33 @@
-## Appendix E - Editions
-
-In Chapter 1, you saw that `cargo new` adds a bit of metadata to your
-_Cargo.toml_ file about an edition. This appendix talks about what that means!
-
-The Rust language and compiler have a six-week release cycle, meaning users get
-a constant stream of new features. Other programming languages release larger
-changes less often; Rust releases smaller updates more frequently. After a
-while, all of these tiny changes add up. But from release to release, it can be
-difficult to look back and say, “Wow, between Rust 1.10 and Rust 1.31, Rust has
-changed a lot!”
-
-Every three years or so, the Rust team produces a new Rust _edition_. Each
-edition brings together the features that have landed into a clear package with
-fully updated documentation and tooling. New editions ship as part of the usual
-six-week release process.
-
-Editions serve different purposes for different people:
-
-- For active Rust users, a new edition brings together incremental changes into
- an easy-to-understand package.
-- For non-users, a new edition signals that some major advancements have
- landed, which might make Rust worth another look.
-- For those developing Rust, a new edition provides a rallying point for the
- project as a whole.
-
-At the time of this writing, four Rust editions are available: Rust 2015, Rust
-2018, Rust 2021, and Rust 2024. This book is written using Rust 2024 edition
-idioms.
-
-The `edition` key in _Cargo.toml_ indicates which edition the compiler should
-use for your code. If the key doesn’t exist, Rust uses `2015` as the edition
-value for backward compatibility reasons.
-
-Each project can opt in to an edition other than the default 2015 edition.
-Editions can contain incompatible changes, such as including a new keyword that
-conflicts with identifiers in code. However, unless you opt in to those
-changes, your code will continue to compile even as you upgrade the Rust
-compiler version you use.
-
-All Rust compiler versions support any edition that existed prior to that
-compiler’s release, and they can link crates of any supported editions
-together. Edition changes only affect the way the compiler initially parses
-code. Therefore, if you’re using Rust 2015 and one of your dependencies uses
-Rust 2018, your project will compile and be able to use that dependency. The
-opposite situation, where your project uses Rust 2018 and a dependency uses
-Rust 2015, works as well.
-
-To be clear: most features will be available on all editions. Developers using
-any Rust edition will continue to see improvements as new stable releases are
-made. However, in some cases, mainly when new keywords are added, some new
-features might only be available in later editions. You will need to switch
-editions if you want to take advantage of such features.
-
-For more details, the [_Edition Guide_](https://doc.rust-lang.org/stable/edition-guide/) is a complete book
-about editions that enumerates the differences between editions and explains
-how to automatically upgrade your code to a new edition via `cargo fix`.
+## ضمیمه ه - نسخهها
+
+در فصل 1 دیدید که `cargo new` کمی متاداده به فایل _Cargo.toml_ شما اضافه میکند که درباره نسخه است. این ضمیمه توضیح میدهد که این به چه معناست!
+
+زبان Rust و کامپایلر آن یک چرخه انتشار ششهفتهای دارند، به این معنی که کاربران به طور مداوم به ویژگیهای جدید دسترسی پیدا میکنند. زبانهای برنامهنویسی دیگر تغییرات بزرگ را کمتر منتشر میکنند؛ در حالی که Rust به طور مرتب بهروزرسانیهای کوچک ارائه میدهد. پس از مدتی، همه این تغییرات کوچک جمع میشوند. اما از انتشار به انتشار، ممکن است سخت باشد که بگویید: «وای، بین Rust 1.10 و Rust 1.31، Rust خیلی تغییر کرده است!»
+
+تقریباً هر سه سال یکبار، تیم Rust یک *نگارش*
+جدید از Rust منتشر میکند. هر نگارش، ویژگیهایی
+را که در طول زمان اضافه شدهاند، در قالبی منسجم
+و همراه با مستندات و ابزارهای کاملاً بهروزشده
+ارائه میدهد. نگارشهای جدید بهعنوان بخشی
+از روند معمول انتشارهای ششهفتهای عرضه میشوند.
+
+نسخهها اهداف مختلفی برای افراد مختلف دارند:
+
+
+ - برای کاربران فعال Rust، یک نسخه جدید تغییرات تدریجی را در یک بسته آسان برای درک جمع میکند.
+ - برای غیرکاربران، یک نسخه جدید نشاندهنده این است که پیشرفتهای عمدهای انجام شده است که ممکن است ارزش داشته باشد Rust را دوباره بررسی کنند.
+ - برای کسانی که Rust را توسعه میدهند، یک نسخه جدید نقطهای برای گردهمایی پروژه به عنوان یک کل فراهم میکند.
+
+
+در زمان نگارش این متن، چهار نسخه از Rust در دسترس هستند: Rust 2015، Rust 2018، Rust 2021، و Rust 2024. این کتاب با استفاده از الگوها و قواعد نسخه Rust 2024 نوشته شده است.
+
+کلید `edition` در فایل _Cargo.toml_ نشان میدهد که کامپایلر باید از کدام نسخه برای کد شما استفاده کند. اگر این کلید وجود نداشته باشد، Rust به دلایل سازگاری با نسخههای قبلی از مقدار `2015` بهعنوان نسخه پیشفرض استفاده میکند.
+
+هر پروژه میتواند به یک نسخه غیر از نسخه پیشفرض 2015 مهاجرت کند. نسخهها میتوانند تغییرات ناسازگار داشته باشند، مانند اضافه کردن یک کلمه کلیدی جدید که با شناسهها در کد تداخل دارد. اما تا زمانی که به این تغییرات مهاجرت نکنید، کد شما همچنان کامپایل خواهد شد حتی اگر نسخه کامپایلر Rust خود را ارتقا دهید.
+
+تمام نسخههای کامپایلر Rust از هر نسخهای که پیش از انتشار آن کامپایلر وجود داشته باشد پشتیبانی میکنند و میتوانند کتابخانههایی از هر نسخه پشتیبانیشده را به یکدیگر لینک کنند. تغییرات نسخه فقط بر نحوه تجزیه اولیه کد توسط کامپایلر تأثیر میگذارد. بنابراین، اگر از Rust 2015 استفاده میکنید و یکی از وابستگیهای شما از Rust 2018 استفاده میکند، پروژه شما کامپایل خواهد شد و میتواند از آن وابستگی استفاده کند. وضعیت معکوس، جایی که پروژه شما از Rust 2018 و یک وابستگی از Rust 2015 استفاده میکند، نیز کار میکند.
+
+برای روشن شدن: بیشتر ویژگیها در همه نسخهها در دسترس خواهند بود. توسعهدهندگان با استفاده از هر نسخهای از Rust همچنان بهبودهایی را با انتشارهای پایدار جدید مشاهده خواهند کرد. اما در برخی موارد، عمدتاً زمانی که کلمات کلیدی جدید اضافه میشوند، برخی ویژگیهای جدید ممکن است فقط در نسخههای بعدی در دسترس باشند. اگر میخواهید از چنین ویژگیهایی استفاده کنید، باید به نسخههای جدیدتر مهاجرت کنید.
+
+برای جزئیات بیشتر، [_راهنمای نسخهها_](https://doc.rust-lang.org/stable/edition-guide/) کتاب کاملی درباره نسخهها است که تفاوتهای بین نسخهها را فهرست میکند و توضیح میدهد که چگونه میتوانید کد خود را با استفاده از `cargo fix` به نسخه جدید ارتقا دهید.
+
diff --git a/src/appendix-06-translation.md b/src/appendix-06-translation.md
index f06b1986cf..5f1d3b2e98 100644
--- a/src/appendix-06-translation.md
+++ b/src/appendix-06-translation.md
@@ -1,7 +1,6 @@
-## Appendix F: Translations of the Book
+## ضمیمه و: ترجمههای کتاب
-For resources in languages other than English. Most are still in progress; see
-[the Translations label][label] to help or let us know about a new translation!
+برای منابع به زبانهایی غیر از انگلیسی. بیشتر آنها هنوز در حال پیشرفت هستند؛ برای کمک یا اطلاع دادن از ترجمه جدید، به [برچسب ترجمهها][label] مراجعه کنید!
[label]: https://github.com/rust-lang/book/issues?q=is%3Aopen+is%3Aissue+label%3ATranslations
@@ -26,3 +25,4 @@ For resources in languages other than English. Most are still in progress; see
- [हिंदी](https://github.com/venkatarun95/rust-book-hindi)
- [ไทย](https://github.com/rust-lang-th/book-th)
- [Danske](https://github.com/DanKHansen/book-dk)
+
diff --git a/src/appendix-07-nightly-rust.md b/src/appendix-07-nightly-rust.md
index 5246d72825..6b4a963a2a 100644
--- a/src/appendix-07-nightly-rust.md
+++ b/src/appendix-07-nightly-rust.md
@@ -1,50 +1,33 @@
-## Appendix G - How Rust is Made and “Nightly Rust”
+## ضمیمه ی - چگونگی توسعه Rust و "Rust Nightly"
-This appendix is about how Rust is made and how that affects you as a Rust
-developer.
+این ضمیمه درباره چگونگی توسعه Rust و تأثیر آن بر شما به عنوان یک توسعهدهنده Rust است.
-### Stability Without Stagnation
+### ثبات بدون رکود
-As a language, Rust cares a _lot_ about the stability of your code. We want
-Rust to be a rock-solid foundation you can build on, and if things were
-constantly changing, that would be impossible. At the same time, if we can’t
-experiment with new features, we may not find out important flaws until after
-their release, when we can no longer change things.
+به عنوان یک زبان، Rust به _ثبات_ کد شما بسیار اهمیت میدهد. ما میخواهیم Rust یک پایه محکم و قابل اعتماد باشد که بتوانید بر روی آن بسازید، و اگر همه چیز به طور مداوم تغییر میکرد، این امکانپذیر نبود. در عین حال، اگر نتوانیم با ویژگیهای جدید آزمایش کنیم، ممکن است مشکلات مهمی را تا بعد از انتشار آنها کشف نکنیم، زمانی که دیگر نمیتوان تغییراتی ایجاد کرد.
-Our solution to this problem is what we call “stability without stagnation”,
-and our guiding principle is this: you should never have to fear upgrading to a
-new version of stable Rust. Each upgrade should be painless, but should also
-bring you new features, fewer bugs, and faster compile times.
+راهحل ما برای این مشکل چیزی است که ما آن را "ثبات بدون رکود" مینامیم، و اصل راهنمای ما این است: شما هرگز نباید از ارتقاء به یک نسخه جدید از Rust پایدار بترسید. هر ارتقاء باید بدون دردسر باشد، اما همچنین ویژگیهای جدید، باگهای کمتر، و زمانهای کامپایل سریعتر را برای شما به ارمغان بیاورد.
-### Choo, Choo! Release Channels and Riding the Trains
+### چو، چو! کانالهای انتشار و حرکت قطارها
-Rust development operates on a _train schedule_. That is, all development is
-done on the `master` branch of the Rust repository. Releases follow a software
-release train model, which has been used by Cisco IOS and other software
-projects. There are three _release channels_ for Rust:
+توسعه Rust بر اساس یک _برنامه زمانی قطار_ عمل میکند. یعنی تمام توسعهها در شاخه `master` مخزن Rust انجام میشود. انتشارها از مدل قطار انتشار نرمافزار پیروی میکنند، مدلی که توسط Cisco IOS و پروژههای نرمافزاری دیگر استفاده شده است. سه _کانال انتشار_ برای Rust وجود دارد:
- Nightly
- Beta
- Stable
-Most Rust developers primarily use the stable channel, but those who want to
-try out experimental new features may use nightly or beta.
+
-Here’s an example of how the development and release process works: let’s
-assume that the Rust team is working on the release of Rust 1.5. That release
-happened in December of 2015, but it will provide us with realistic version
-numbers. A new feature is added to Rust: a new commit lands on the `master`
-branch. Each night, a new nightly version of Rust is produced. Every day is a
-release day, and these releases are created by our release infrastructure
-automatically. So as time passes, our releases look like this, once a night:
+
+بیشتر توسعهدهندگان Rust عمدتاً از کانال پایدار استفاده میکنند، اما کسانی که میخواهند ویژگیهای آزمایشی جدید را امتحان کنند ممکن است از کانالهای nightly یا beta استفاده کنند.
+
+در اینجا مثالی از نحوه کار فرآیند توسعه و انتشار آورده شده است: فرض کنید تیم Rust روی انتشار نسخه Rust 1.5 کار میکند. آن انتشار در دسامبر 2015 اتفاق افتاد، اما اعداد نسخهای واقعی به ما ارائه میدهد. یک ویژگی جدید به Rust اضافه میشود: یک commit جدید به شاخه `master` اضافه میشود. هر شب، یک نسخه جدید nightly از Rust تولید میشود. هر روز یک روز انتشار است، و این نسخهها به طور خودکار توسط زیرساخت انتشار ما ایجاد میشوند. بنابراین با گذشت زمان، انتشارهای ما به این صورت خواهند بود، هر شب:
```text
nightly: * - - * - - *
```
-Every six weeks, it’s time to prepare a new release! The `beta` branch of the
-Rust repository branches off from the `master` branch used by nightly. Now,
-there are two releases:
+هر شش هفته، زمان آمادهسازی یک انتشار جدید است! شاخه `beta` مخزن Rust از شاخه `master` که برای nightly استفاده میشود منشعب میشود. اکنون دو نسخه وجود دارد:
```text
nightly: * - - * - - *
@@ -52,9 +35,7 @@ nightly: * - - * - - *
beta: *
```
-Most Rust users do not use beta releases actively, but test against beta in
-their CI system to help Rust discover possible regressions. In the meantime,
-there’s still a nightly release every night:
+بیشتر کاربران Rust به طور فعال از نسخههای beta استفاده نمیکنند، اما در سیستم CI خود علیه beta تست میگیرند تا به Rust کمک کنند که مشکلات احتمالی را شناسایی کند. در همین حال، هنوز هر شب یک نسخه nightly منتشر میشود:
```text
nightly: * - - * - - * - - * - - *
@@ -62,10 +43,7 @@ nightly: * - - * - - * - - * - - *
beta: *
```
-Let’s say a regression is found. Good thing we had some time to test the beta
-release before the regression snuck into a stable release! The fix is applied
-to `master`, so that nightly is fixed, and then the fix is backported to the
-`beta` branch, and a new release of beta is produced:
+فرض کنید یک مشکل (regression) پیدا شود. خوشبختانه ما زمانی برای تست نسخه beta داشتیم قبل از اینکه مشکل وارد نسخه پایدار شود! اصلاح به شاخه `master` اعمال میشود، بنابراین nightly اصلاح میشود، و سپس این اصلاح به شاخه `beta` بازگردانده میشود، و یک نسخه جدید از beta تولید میشود:
```text
nightly: * - - * - - * - - * - - * - - *
@@ -73,8 +51,7 @@ nightly: * - - * - - * - - * - - * - - *
beta: * - - - - - - - - *
```
-Six weeks after the first beta was created, it’s time for a stable release! The
-`stable` branch is produced from the `beta` branch:
+شش هفته پس از ایجاد اولین نسخه beta، زمان انتشار نسخه پایدار است! شاخه `stable` از شاخه `beta` تولید میشود:
```text
nightly: * - - * - - * - - * - - * - - * - * - *
@@ -84,10 +61,7 @@ beta: * - - - - - - - - *
stable: *
```
-Hooray! Rust 1.5 is done! However, we’ve forgotten one thing: because the six
-weeks have gone by, we also need a new beta of the _next_ version of Rust, 1.6.
-So after `stable` branches off of `beta`, the next version of `beta` branches
-off of `nightly` again:
+هورا! Rust 1.5 آماده است! اما یک چیز را فراموش کردهایم: چون شش هفته گذشته است، ما به نسخه beta جدیدی از _نسخه بعدی_ Rust، یعنی 1.6، نیاز داریم. بنابراین پس از اینکه شاخه `stable` از `beta` جدا شد، نسخه بعدی `beta` دوباره از `nightly` منشعب میشود:
```text
nightly: * - - * - - * - - * - - * - - * - * - *
@@ -97,63 +71,33 @@ beta: * - - - - - - - - * *
stable: *
```
-This is called the “train model” because every six weeks, a release “leaves the
-station”, but still has to take a journey through the beta channel before it
-arrives as a stable release.
+این مدل "قطار" نامیده میشود، زیرا هر شش هفته، یک انتشار "ایستگاه را ترک میکند"، اما همچنان باید از کانال beta عبور کند تا به یک انتشار پایدار تبدیل شود.
-Rust releases every six weeks, like clockwork. If you know the date of one Rust
-release, you can know the date of the next one: it’s six weeks later. A nice
-aspect of having releases scheduled every six weeks is that the next train is
-coming soon. If a feature happens to miss a particular release, there’s no need
-to worry: another one is happening in a short time! This helps reduce pressure
-to sneak possibly unpolished features in close to the release deadline.
+انتشارهای Rust هر شش هفته، مانند ساعت دقیق انجام میشوند. اگر تاریخ یک انتشار Rust را بدانید، میتوانید تاریخ انتشار بعدی را بدانید: شش هفته بعد. یکی از جنبههای خوب داشتن انتشارهای برنامهریزیشده هر شش هفته این است که قطار بعدی به زودی میآید. اگر یک ویژگی به طور اتفاقی یک انتشار خاص را از دست بدهد، نیازی به نگرانی نیست: انتشار بعدی در مدت کوتاهی اتفاق میافتد! این امر به کاهش فشار برای افزودن ویژگیهای احتمالاً ناقص نزدیک به مهلت انتشار کمک میکند.
-Thanks to this process, you can always check out the next build of Rust and
-verify for yourself that it’s easy to upgrade to: if a beta release doesn’t
-work as expected, you can report it to the team and get it fixed before the
-next stable release happens! Breakage in a beta release is relatively rare, but
-`rustc` is still a piece of software, and bugs do exist.
+با تشکر از این فرآیند، شما همیشه میتوانید نسخه بعدی Rust را بررسی کرده و برای خود تأیید کنید که ارتقاء به آن آسان است: اگر یک نسخه beta مطابق انتظار عمل نکند، میتوانید آن را به تیم گزارش دهید و قبل از اینکه انتشار پایدار بعدی انجام شود، آن را اصلاح کنید! شکستن در یک نسخه beta نسبتاً نادر است، اما `rustc` همچنان یک نرمافزار است و باگها وجود دارند.
-### Maintenance time
+### زمان نگهداری
-The Rust project supports the most recent stable version. When a new stable
-version is released, the old version reaches its end of life (EOL). This means
-each version is supported for six weeks.
+پروژه Rust از آخرین نسخه پایدار پشتیبانی میکند. وقتی یک نسخه پایدار جدید منتشر میشود، نسخه قدیمی به پایان عمر خود (EOL) میرسد. این به این معنی است که هر نسخه برای شش هفته پشتیبانی میشود.
-### Unstable Features
+### ویژگیهای ناپایدار
-There’s one more catch with this release model: unstable features. Rust uses a
-technique called “feature flags” to determine what features are enabled in a
-given release. If a new feature is under active development, it lands on
-`master`, and therefore, in nightly, but behind a _feature flag_. If you, as a
-user, wish to try out the work-in-progress feature, you can, but you must be
-using a nightly release of Rust and annotate your source code with the
-appropriate flag to opt in.
+یک نکته دیگر در این مدل انتشار وجود دارد: ویژگیهای ناپایدار. Rust از تکنیکی به نام "پرچمهای ویژگی" (feature flags) استفاده میکند تا تعیین کند چه ویژگیهایی در یک انتشار فعال هستند. اگر یک ویژگی جدید تحت توسعه فعال باشد، روی شاخه `master` قرار میگیرد و بنابراین، در nightly، اما پشت یک _پرچم ویژگی_ قرار میگیرد. اگر بهعنوان کاربر، مایلید ویژگی در حال توسعه را امتحان کنید، میتوانید این کار را انجام دهید، اما باید از نسخه nightly Rust استفاده کرده و کد منبع خود را با پرچم مناسب برای فعالسازی آن علامتگذاری کنید.
-If you’re using a beta or stable release of Rust, you can’t use any feature
-flags. This is the key that allows us to get practical use with new features
-before we declare them stable forever. Those who wish to opt into the bleeding
-edge can do so, and those who want a rock-solid experience can stick with
-stable and know that their code won’t break. Stability without stagnation.
+اگر از نسخه beta یا پایدار Rust استفاده میکنید، نمیتوانید از پرچمهای ویژگی استفاده کنید. این نکتهای است که به ما اجازه میدهد از ویژگیهای جدید به صورت عملی استفاده کنیم قبل از اینکه آنها را برای همیشه پایدار اعلام کنیم. کسانی که مایلند از ویژگیهای پیشرفته استفاده کنند، میتوانند این کار را انجام دهند، و کسانی که تجربهای پایدار و قابل اعتماد میخواهند میتوانند با نسخه پایدار بمانند و مطمئن باشند که کد آنها خراب نخواهد شد. ثبات بدون رکود.
-This book only contains information about stable features, as in-progress
-features are still changing, and surely they’ll be different between when this
-book was written and when they get enabled in stable builds. You can find
-documentation for nightly-only features online.
+این کتاب فقط شامل اطلاعات مربوط به ویژگیهای پایدار است، زیرا ویژگیهای در حال توسعه همچنان در حال تغییر هستند و مطمئناً بین زمانی که این کتاب نوشته شده و زمانی که در نسخههای پایدار فعال میشوند، متفاوت خواهند بود. میتوانید مستندات مربوط به ویژگیهایی که فقط در nightly موجود هستند را به صورت آنلاین پیدا کنید.
-### Rustup and the Role of Rust Nightly
+### Rustup و نقش Rust Nightly
-Rustup makes it easy to change between different release channels of Rust, on a
-global or per-project basis. By default, you’ll have stable Rust installed. To
-install nightly, for example:
+ابزار Rustup تغییر بین کانالهای مختلف انتشار Rust را، به صورت جهانی یا بر اساس هر پروژه، آسان میکند. به طور پیشفرض، Rust پایدار نصب خواهد بود. برای نصب نسخه nightly، به عنوان مثال:
```console
$ rustup toolchain install nightly
```
-You can see all of the _toolchains_ (releases of Rust and associated
-components) you have installed with `rustup` as well. Here’s an example on one
-of your authors’ Windows computer:
+همچنین میتوانید تمام _ابزارهای موجود_ (نسخههای Rust و اجزای مرتبط) که با `rustup` نصب کردهاید را ببینید. در اینجا مثالی از یک کامپیوتر ویندوزی یکی از نویسندگان آورده شده است:
```powershell
> rustup toolchain list
@@ -162,45 +106,21 @@ beta-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-msvc
```
-As you can see, the stable toolchain is the default. Most Rust users use stable
-most of the time. You might want to use stable most of the time, but use
-nightly on a specific project, because you care about a cutting-edge feature.
-To do so, you can use `rustup override` in that project’s directory to set the
-nightly toolchain as the one `rustup` should use when you’re in that directory:
+همانطور که میبینید، ابزار stable به طور پیشفرض تنظیم شده است. بیشتر کاربران Rust بیشتر وقت خود از stable استفاده میکنند. ممکن است بخواهید بیشتر وقت خود از stable استفاده کنید، اما در یک پروژه خاص از nightly استفاده کنید، زیرا به یک ویژگی پیشرفته علاقه دارید. برای انجام این کار، میتوانید از `rustup override` در دایرکتوری آن پروژه استفاده کنید تا ابزار nightly را بهعنوان ابزار مورد استفاده `rustup` در آن دایرکتوری تنظیم کنید:
```console
$ cd ~/projects/needs-nightly
$ rustup override set nightly
```
-Now, every time you call `rustc` or `cargo` inside of
-_~/projects/needs-nightly_, `rustup` will make sure that you are using nightly
-Rust, rather than your default of stable Rust. This comes in handy when you
-have a lot of Rust projects!
-
-### The RFC Process and Teams
-
-So how do you learn about these new features? Rust’s development model follows
-a _Request For Comments (RFC) process_. If you’d like an improvement in Rust,
-you can write up a proposal, called an RFC.
-
-Anyone can write RFCs to improve Rust, and the proposals are reviewed and
-discussed by the Rust team, which is comprised of many topic subteams. There’s
-a full list of the teams [on Rust’s website](https://www.rust-lang.org/governance), which includes teams for
-each area of the project: language design, compiler implementation,
-infrastructure, documentation, and more. The appropriate team reads the
-proposal and the comments, writes some comments of their own, and eventually,
-there’s consensus to accept or reject the feature.
-
-If the feature is accepted, an issue is opened on the Rust repository, and
-someone can implement it. The person who implements it very well may not be the
-person who proposed the feature in the first place! When the implementation is
-ready, it lands on the `master` branch behind a feature gate, as we discussed
-in the [“Unstable Features”](#unstable-features) section.
-
-After some time, once Rust developers who use nightly releases have been able
-to try out the new feature, team members will discuss the feature, how it’s
-worked out on nightly, and decide if it should make it into stable Rust or not.
-If the decision is to move forward, the feature gate is removed, and the
-feature is now considered stable! It rides the trains into a new stable release
-of Rust.
+اکنون، هر بار که در دایرکتوری _~/projects/needs-nightly_ دستور `rustc` یا `cargo` را فراخوانی کنید، `rustup` اطمینان حاصل میکند که شما از Rust nightly استفاده میکنید، نه نسخه پایدار پیشفرض. این ویژگی زمانی که پروژههای زیادی با Rust دارید، بسیار مفید است!
+
+### فرآیند RFC و تیمها
+
+چگونه میتوانید درباره این ویژگیهای جدید اطلاعات کسب کنید؟ مدل توسعه Rust از یک فرآیند _درخواست نظرات (RFC)_ پیروی میکند. اگر بهبود خاصی در Rust میخواهید، میتوانید یک پیشنهاد بنویسید که به آن RFC گفته میشود.
+
+هر کسی میتواند RFC بنویسد تا Rust را بهبود دهد، و این پیشنهادها توسط تیم Rust که از چندین زیرتیم موضوعی تشکیل شده است، بررسی و بحث میشوند. لیست کامل تیمها [در وبسایت Rust](https://www.rust-lang.org/governance) موجود است، که شامل تیمهایی برای هر بخش از پروژه میشود: طراحی زبان، پیادهسازی کامپایلر، زیرساخت، مستندات و موارد دیگر. تیم مربوطه پیشنهاد و نظرات را میخواند، نظرات خود را مینویسد، و در نهایت، توافقی برای پذیرش یا رد ویژگی حاصل میشود.
+
+اگر ویژگی پذیرفته شود، یک issue در مخزن Rust باز میشود و کسی میتواند آن را پیادهسازی کند. فردی که آن را پیادهسازی میکند، ممکن است همان فردی نباشد که ویژگی را ابتدا پیشنهاد داده است! وقتی پیادهسازی آماده شد، روی شاخه `master` پشت یک پرچم ویژگی قرار میگیرد، همانطور که در بخش [“ویژگیهای ناپایدار”](#unstable-features) بحث شد.
+
+پس از مدتی، زمانی که توسعهدهندگان Rust که از نسخههای nightly استفاده میکنند توانستهاند ویژگی جدید را امتحان کنند، اعضای تیم درباره این ویژگی، نحوه عملکرد آن در nightly و تصمیمگیری میکنند که آیا باید وارد Rust پایدار شود یا نه. اگر تصمیم بر ادامه باشد، پرچم ویژگی حذف میشود و ویژگی اکنون پایدار تلقی میشود! سپس این ویژگی وارد نسخه پایدار جدید Rust میشود.
diff --git a/src/ch00-00-introduction.md b/src/ch00-00-introduction.md
index f3916dceda..d5dce15aef 100644
--- a/src/ch00-00-introduction.md
+++ b/src/ch00-00-introduction.md
@@ -1,196 +1,129 @@
-# Introduction
+# مقدمه
-> Note: This edition of the book is the same as [The Rust Programming
-> Language][nsprust] available in print and ebook format from [No Starch
-> Press][nsp].
+> توجه: این نسخه از کتاب همان [The Rust Programming Language][nsprust] است که به صورت چاپی و الکترونیکی از [No Starch Press][nsp] در دسترس است.
[nsprust]: https://nostarch.com/rust-programming-language-2nd-edition
[nsp]: https://nostarch.com/
-Welcome to _The Rust Programming Language_, an introductory book about Rust.
-The Rust programming language helps you write faster, more reliable software.
-High-level ergonomics and low-level control are often at odds in programming
-language design; Rust challenges that conflict. Through balancing powerful
-technical capacity and a great developer experience, Rust gives you the option
-to control low-level details (such as memory usage) without all the hassle
-traditionally associated with such control.
-
-## Who Rust Is For
-
-Rust is ideal for many people for a variety of reasons. Let’s look at a few of
-the most important groups.
-
-### Teams of Developers
-
-Rust is proving to be a productive tool for collaborating among large teams of
-developers with varying levels of systems programming knowledge. Low-level code
-is prone to various subtle bugs, which in most other languages can be caught
-only through extensive testing and careful code review by experienced
-developers. In Rust, the compiler plays a gatekeeper role by refusing to
-compile code with these elusive bugs, including concurrency bugs. By working
-alongside the compiler, the team can spend their time focusing on the program’s
-logic rather than chasing down bugs.
-
-Rust also brings contemporary developer tools to the systems programming world:
-
-- Cargo, the included dependency manager and build tool, makes adding,
- compiling, and managing dependencies painless and consistent across the Rust
- ecosystem.
-- The Rustfmt formatting tool ensures a consistent coding style across
- developers.
-- The rust-analyzer powers Integrated Development Environment (IDE)
- integration for code completion and inline error messages.
-
-By using these and other tools in the Rust ecosystem, developers can be
-productive while writing systems-level code.
-
-### Students
-
-Rust is for students and those who are interested in learning about systems
-concepts. Using Rust, many people have learned about topics like operating
-systems development. The community is very welcoming and happy to answer
-student questions. Through efforts such as this book, the Rust teams want to
-make systems concepts more accessible to more people, especially those new to
-programming.
-
-### Companies
-
-Hundreds of companies, large and small, use Rust in production for a variety of
-tasks, including command line tools, web services, DevOps tooling, embedded
-devices, audio and video analysis and transcoding, cryptocurrencies,
-bioinformatics, search engines, Internet of Things applications, machine
-learning, and even major parts of the Firefox web browser.
-
-### Open Source Developers
-
-Rust is for people who want to build the Rust programming language, community,
-developer tools, and libraries. We’d love to have you contribute to the Rust
-language.
-
-### People Who Value Speed and Stability
-
-Rust is for people who crave speed and stability in a language. By speed, we
-mean both how quickly Rust code can run and the speed at which Rust lets you
-write programs. The Rust compiler’s checks ensure stability through feature
-additions and refactoring. This is in contrast to the brittle legacy code in
-languages without these checks, which developers are often afraid to modify. By
-striving for zero-cost abstractions—higher-level features that compile to
-lower-level code as fast as code written manually—Rust endeavors to make safe
-code be fast code as well.
-
-The Rust language hopes to support many other users as well; those mentioned
-here are merely some of the biggest stakeholders. Overall, Rust’s greatest
-ambition is to eliminate the trade-offs that programmers have accepted for
-decades by providing safety _and_ productivity, speed _and_ ergonomics. Give
-Rust a try and see if its choices work for you.
-
-## Who This Book Is For
-
-This book assumes that you’ve written code in another programming language but
-doesn’t make any assumptions about which one. We’ve tried to make the material
-broadly accessible to those from a wide variety of programming backgrounds. We
-don’t spend a lot of time talking about what programming _is_ or how to think
-about it. If you’re entirely new to programming, you would be better served by
-reading a book that specifically provides an introduction to programming.
-
-## How to Use This Book
-
-In general, this book assumes that you’re reading it in sequence from front to
-back. Later chapters build on concepts in earlier chapters, and earlier
-chapters might not delve into details on a particular topic but will revisit
-the topic in a later chapter.
-
-You’ll find two kinds of chapters in this book: concept chapters and project
-chapters. In concept chapters, you’ll learn about an aspect of Rust. In project
-chapters, we’ll build small programs together, applying what you’ve learned so
-far. Chapters 2, 12, and 21 are project chapters; the rest are concept chapters.
-
-Chapter 1 explains how to install Rust, how to write a “Hello, world!” program,
-and how to use Cargo, Rust’s package manager and build tool. Chapter 2 is a
-hands-on introduction to writing a program in Rust, having you build up a
-number guessing game. Here we cover concepts at a high level, and later
-chapters will provide additional detail. If you want to get your hands dirty
-right away, Chapter 2 is the place for that. Chapter 3 covers Rust features
-that are similar to those of other programming languages, and in Chapter 4
-you’ll learn about Rust’s ownership system. If you’re a particularly meticulous
-learner who prefers to learn every detail before moving on to the next, you
-might want to skip Chapter 2 and go straight to Chapter 3, returning to Chapter
-2 when you’d like to work on a project applying the details you’ve learned.
-
-Chapter 5 discusses structs and methods, and Chapter 6 covers enums, `match`
-expressions, and the `if let` control flow construct. You’ll use structs and
-enums to make custom types in Rust.
-
-In Chapter 7, you’ll learn about Rust’s module system and about privacy rules
-for organizing your code and its public Application Programming Interface
-(API). Chapter 8 discusses some common collection data structures that the
-standard library provides, such as vectors, strings, and hash maps. Chapter 9
-explores Rust’s error-handling philosophy and techniques.
-
-Chapter 10 digs into generics, traits, and lifetimes, which give you the power
-to define code that applies to multiple types. Chapter 11 is all about testing,
-which even with Rust’s safety guarantees is necessary to ensure your program’s
-logic is correct. In Chapter 12, we’ll build our own implementation of a subset
-of functionality from the `grep` command line tool that searches for text
-within files. For this, we’ll use many of the concepts we discussed in the
-previous chapters.
-
-Chapter 13 explores closures and iterators: features of Rust that come from
-functional programming languages. In Chapter 14, we’ll examine Cargo in more
-depth and talk about best practices for sharing your libraries with others.
-Chapter 15 discusses smart pointers that the standard library provides and the
-traits that enable their functionality.
-
-In Chapter 16, we’ll walk through different models of concurrent programming and
-talk about how Rust helps you to program in multiple threads fearlessly. In
-Chapter 17, we build on that by exploring Rust’s async and await syntax, along
-with tasks, futures, and streams, and the lightweight concurrency model they
-enable.
-
-Chapter 18 looks at how Rust idioms compare to object-oriented programming
-principles you might be familiar with. Chapter 19 is a reference on patterns and
-pattern matching, which are powerful ways of expressing ideas throughout Rust
-programs. Chapter 20 contains a smorgasbord of advanced topics of interest,
-including unsafe Rust, macros, and more about lifetimes, traits, types,
-functions, and closures.
-
-In Chapter 21, we’ll complete a project in which we’ll implement a low-level
-multithreaded web server!
-
-Finally, some appendixes contain useful information about the language in a more
-reference-like format. **Appendix A** covers Rust’s keywords, **Appendix B**
-covers Rust’s operators and symbols, **Appendix C** covers derivable traits
-provided by the standard library, **Appendix D** covers some useful development
-tools, and **Appendix E** explains Rust editions. In **Appendix F**, you can
-find translations of the book, and in **Appendix G** we’ll cover how Rust is
-made and what nightly Rust is.
-
-There is no wrong way to read this book: if you want to skip ahead, go for it!
-You might have to jump back to earlier chapters if you experience any
-confusion. But do whatever works for you.
+به _زبان برنامهنویسی راست_ خوش آمدید، یک کتاب مقدماتی درباره راست. زبان برنامهنویسی راست به شما کمک میکند نرمافزاری سریعتر و قابلاعتمادتر بنویسید. در طراحی زبانهای برنامهنویسی، راحتی در سطح بالا و کنترل در سطح پایین اغلب در تضاد هستند؛ راست این تناقض را به چالش میکشد. با ایجاد تعادل بین تواناییهای فنی قدرتمند و تجربه عالی برنامهنویسی، راست به شما این امکان را میدهد که جزئیات سطح پایین (مانند استفاده از حافظه) را بدون دردسرهای سنتی مرتبط با چنین کنترلی مدیریت کنید.
+
+## راست برای چه کسانی است
+
+راست برای افراد مختلف با دلایل متنوع ایدهآل است. بیایید به برخی از مهمترین گروهها نگاهی بیندازیم.
+
+### تیمهای برنامهنویسی
+
+راست ابزاری اثبات شده برای همکاری میان تیمهای بزرگ برنامهنویسان با سطوح مختلف دانش برنامهنویسی سیستم است. کد سطح پایین مستعد اشکالات ظریف متعددی است که در بیشتر زبانهای دیگر تنها از طریق تست گسترده و بازبینی دقیق کد توسط برنامهنویسان با تجربه قابل شناسایی هستند. در راست، کامپایلر نقش نگهبان را ایفا میکند و از کامپایل کردن کدهایی با این اشکالات گریزان، از جمله اشکالات همروندی، جلوگیری میکند. با کار کردن در کنار کامپایلر، تیم میتواند زمان خود را بر روی منطق برنامه به جای رفع اشکالات صرف کند.
+
+راست همچنین ابزارهای مدرن برنامهنویسی را به دنیای برنامهنویسی سیستمها میآورد:
+
+- Cargo، مدیر وابستگی و ابزار ساخت، اضافه کردن، کامپایل کردن، و مدیریت وابستگیها را در سراسر اکوسیستم راست ساده و یکپارچه میکند.
+- ابزار قالببندی Rustfmt، یک سبک کدنویسی ثابت را در بین برنامهنویسان تضمین میکند.
+- rust-analyzer یکپارچگی محیط توسعه یکپارچه (IDE) را برای تکمیل کد و پیامهای خطای درونخطی فراهم میکند.
+
+با استفاده از این ابزارها و دیگر ابزارهای اکوسیستم راست، برنامهنویسان میتوانند در هنگام نوشتن کد سطح سیستمها بهرهور باشند.
+
+### دانشجویان
+
+راست برای دانشجویان و کسانی است که به یادگیری مفاهیم سیستمها علاقهمند هستند. بسیاری از افراد با استفاده از راست موضوعاتی مانند توسعه سیستمعامل را آموختهاند. جامعه راست بسیار پذیرنده است و با خوشحالی به سوالات دانشجویان پاسخ میدهد. از طریق تلاشهایی مانند این کتاب، تیمهای راست میخواهند مفاهیم سیستمها را برای افراد بیشتری، به ویژه کسانی که تازه وارد برنامهنویسی هستند، قابل دسترستر کنند.
+
+### شرکتها
+
+صدها شرکت، بزرگ و کوچک، از راست در تولید برای وظایف متنوعی استفاده میکنند، از جمله ابزارهای خط فرمان، خدمات وب، ابزارهای DevOps، دستگاههای تعبیهشده، تحلیل و رمزگذاری صدا و تصویر، ارزهای دیجیتال، زیستاطلاعات، موتورهای جستجو، برنامههای اینترنت اشیاء، یادگیری ماشین و حتی بخشهای اصلی مرورگر وب فایرفاکس.
+
+### توسعهدهندگان متنباز
+
+راست برای کسانی است که میخواهند زبان برنامهنویسی راست، جامعه، ابزارهای توسعهدهنده و کتابخانهها را بسازند. ما دوست داریم شما در توسعه زبان راست مشارکت کنید.
+
+### افرادی که سرعت و پایداری را ارزشمند میدانند
+
+Rust برای کسانی است که بهدنبال سرعت و پایداری
+در یک زبان برنامهنویسی هستند. منظور از سرعت،
+هم سرعت اجرای کدهای Rust و هم سرعت توسعه
+برنامه با استفاده از Rust است. بررسیهای
+کامپایلر Rust پایداری را حتی هنگام افزودن ویژگیهای
+جدید یا بازسازی کد (refactoring) تضمین میکنند.
+این در تضاد با کدهای قدیمی و شکننده در زبانهایی
+است که چنین بررسیهایی ندارند و توسعهدهندگان
+اغلب از تغییر آنها واهمه دارند. Rust با تمرکز بر
+مفهوم انتزاعهای بدونهزینه (zero-cost abstractions)—
+یعنی ویژگیهای سطح بالا که پس از کامپایل به
+کدی در سطح پایین و سریع مانند کد دستی تبدیل
+میشوند—تلاش میکند تا کد امن، کدی سریع نیز باشد.
+
+زبان راست امیدوار است از بسیاری از کاربران دیگر نیز پشتیبانی کند؛ افرادی که در اینجا ذکر شدند تنها برخی از بزرگترین ذینفعان هستند. در کل، بزرگترین جاهطلبی راست این است که با ارائه ایمنی _و_ بهرهوری، سرعت _و_ راحتی، مصالحههایی که برنامهنویسان دههها پذیرفتهاند را حذف کند. راست را امتحان کنید و ببینید آیا انتخابهای آن برای شما مناسب است یا خیر.
+
+## این کتاب برای چه کسانی است
+
+این کتاب فرض میکند که شما قبلاً در یک زبان برنامهنویسی دیگر کدنویسی کردهاید اما هیچ فرضی در مورد اینکه کدام زبان است، ندارد. ما سعی کردهایم مطالب را به گونهای ارائه دهیم که برای افراد با زمینههای برنامهنویسی متنوع قابل دسترسی باشد. ما زمان زیادی را صرف صحبت درباره اینکه برنامهنویسی _چیست_ یا چگونه باید به آن فکر کنید، نمیکنیم. اگر کاملاً تازهوارد برنامهنویسی هستید، بهتر است کتابی را بخوانید که به طور خاص مقدمهای بر برنامهنویسی ارائه میدهد.
+
+## نحوه استفاده از این کتاب
+
+به طور کلی، این کتاب فرض میکند که شما آن را به ترتیب از ابتدا تا انتها میخوانید. فصلهای بعدی بر مفاهیم فصلهای قبلی بنا شدهاند و فصلهای اولیه ممکن است به جزئیات خاصی وارد نشوند اما در فصول بعدی به آن موضوعات بازمیگردند.
+
+در این کتاب، دو نوع فصل وجود دارد: فصلهای مفهومی و فصلهای پروژهای. در فصلهای مفهومی، درباره یک جنبه از راست یاد خواهید گرفت. در فصلهای پروژهای، برنامههای کوچکی را با هم میسازیم و آنچه را که تاکنون آموختهاید به کار میگیریم. فصلهای ۲، ۱۲ و ۲۱ فصلهای پروژهای هستند؛ بقیه فصلها مفهومی هستند.
+
+فصل ۱ نحوه نصب راست، نوشتن یک برنامه "سلام دنیا!" و استفاده از Cargo، مدیر بسته و ابزار ساخت راست را توضیح میدهد. فصل ۲ مقدمهای عملی برای نوشتن برنامهای در راست است و شما را به ساخت یک بازی حدس عدد میبرد. در اینجا مفاهیم را به طور کلی پوشش میدهیم و جزئیات بیشتری را در فصول بعدی ارائه خواهیم کرد. اگر میخواهید بلافاصله کار عملی انجام دهید، فصل ۲ مناسب شماست. فصل ۳ ویژگیهای راست را که مشابه ویژگیهای سایر زبانهای برنامهنویسی است پوشش میدهد و در فصل ۴ درباره سیستم مالکیت راست یاد خواهید گرفت. اگر شما یک یادگیرنده دقیق هستید که ترجیح میدهید قبل از ادامه، همه جزئیات را بیاموزید، ممکن است بخواهید فصل ۲ را رد کنید و مستقیماً به فصل ۳ بروید و پس از یادگیری جزئیات به فصل ۲ بازگردید تا روی پروژهای کار کنید.
+
+فصل ۵ به ساختارها (structs) و متدها میپردازد و فصل ۶ شامل enumerations (enums)، عبارات `match` و سازه کنترلی `if let` است. از ساختارها و enumها برای ایجاد انواع سفارشی در راست استفاده خواهید کرد.
+
+در فصل ۷، درباره سیستم ماژول راست و قوانین حریم خصوصی برای سازماندهی کد و رابط برنامهنویسی عمومی (API) آن یاد خواهید گرفت. فصل ۸ به بررسی برخی از ساختارهای داده مجموعه رایج که کتابخانه استاندارد ارائه میدهد، مانند vectors، strings و hash maps میپردازد. فصل ۹ فلسفه و تکنیکهای مدیریت خطا در راست را بررسی میکند.
+
+فصل ۱۰ به مفاهیم جنریکها، traits و lifetimes میپردازد که به شما این قدرت را میدهد تا کدی بنویسید که به انواع مختلف اعمال شود. فصل ۱۱ کاملاً درباره تست است که حتی با تضمینهای ایمنی راست، برای اطمینان از درستی منطق برنامه شما ضروری است. در فصل ۱۲، پیادهسازی بخشی از ابزار خط فرمان `grep` که متن را در فایلها جستجو میکند، خواهیم ساخت. برای این کار، از بسیاری از مفاهیمی که در فصلهای قبلی مورد بحث قرار گرفتند استفاده خواهیم کرد.
+
+فصل ۱۳ به بررسی closures و iterators میپردازد: ویژگیهایی از راست که از زبانهای برنامهنویسی تابعی آمدهاند. در فصل ۱۴، Cargo را به طور عمیقتری بررسی خواهیم کرد و درباره بهترین روشها برای اشتراکگذاری کتابخانههای خود با دیگران صحبت خواهیم کرد. فصل ۱۵ اشارهگر (Pointer)های هوشمند (smart pointers) ارائهشده توسط کتابخانه استاندارد و traitsی که قابلیتهای آنها را امکانپذیر میسازد بررسی میکند.
+
+در فصل ۱۶، با مدلهای مختلف برنامهنویسی همروند
+(concurrent) آشنا خواهیم شد و دربارهی اینکه چگونه
+Rust به شما کمک میکند تا بدون ترس در چند thread
+برنامهنویسی کنید صحبت میکنیم. در فصل ۱۷،
+بر پایهی آن مفاهیم، نگارش async و await را در
+Rust بررسی میکنیم و همچنین به سراغ taskها،
+futureها، و streamها میرویم که مدل همروندی سبکوزن
+را فراهم میکنند.
+
+فصل ۱۸ به مقایسهی شیوههای رایج در Rust با
+اصول برنامهنویسی شیگرا میپردازد که ممکن است
+پیشتر با آنها آشنا باشید. فصل ۱۹ مرجعی است
+برای الگوها (patterns) و pattern matching، که راهکارهایی
+قدرتمند برای بیان مفاهیم در سراسر برنامههای Rust هستند.
+فصل ۲۰ مجموعهای متنوع از موضوعات پیشرفته
+را در بر میگیرد، از جمله Rust ناایمن (unsafe)،
+ماکروها، و مباحث بیشتری دربارهی lifetimeها،
+traitها، نوعها، تابعها و closureها.
+
+در فصل ۲۱، پروژهای را تکمیل میکنیم که در آن یک سرور وب چندرشتهای سطح پایین پیادهسازی خواهیم کرد!
+
+در نهایت، برخی ضمیمهها شامل اطلاعات مفیدی
+دربارهی زبان Rust هستند که بهصورت مرجعگونه
+ارائه شدهاند. **ضمیمهی الف** به کلمات کلیدی
+Rust میپردازد، **ضمیمهی ب** عملگرها و نمادهای
+Rust را پوشش میدهد، **ضمیمهی ج** traitهای
+قابلمشتق موجود در کتابخانهی استاندارد را بررسی
+میکند، **ضمیمهی د** به برخی ابزارهای مفید توسعه
+میپردازد، و **ضمیمهی ه** نگارشهای مختلف Rust
+را توضیح میدهد. در **ضمیمهی و** میتوانید
+ترجمههای این کتاب را بیابید، و در **ضمیمهی ی**
+با روند توسعهی Rust و مفهوم Rust شبانه (nightly)
+آشنا خواهید شد.
+
+
+هیچ روش نادرستی برای خواندن این کتاب وجود ندارد: اگر میخواهید به جلو بروید، این کار را انجام دهید! ممکن است مجبور شوید به فصلهای قبلی بازگردید اگر با سردرگمی روبهرو شدید. اما هرچه برای شما مناسب است انجام دهید.
-An important part of the process of learning Rust is learning how to read the
-error messages the compiler displays: these will guide you toward working code.
-As such, we’ll provide many examples that don’t compile along with the error
-message the compiler will show you in each situation. Know that if you enter
-and run a random example, it may not compile! Make sure you read the
-surrounding text to see whether the example you’re trying to run is meant to
-error. Ferris will also help you distinguish code that isn’t meant to work:
+بخش مهمی از فرآیند یادگیری راست، یادگیری نحوه خواندن پیامهای خطای کامپایلر است: این پیامها شما را به سمت کدی که کار میکند هدایت میکنند. به همین دلیل، مثالهای زیادی را ارائه میدهیم که کامپایل نمیشوند، همراه با پیام خطایی که کامپایلر در هر وضعیت نمایش میدهد. بدانید که اگر یک مثال تصادفی را وارد کنید و اجرا کنید، ممکن است کامپایل نشود! مطمئن شوید که متن اطراف را بخوانید تا ببینید آیا مثالی که میخواهید اجرا کنید قرار است خطا بدهد یا خیر. Ferris همچنین به شما کمک میکند کدی که قرار نیست کار کند را تشخیص دهید:
-| Ferris | Meaning |
+| Ferris | معنی |
| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
-|
| This code does not compile! |
-|
| This code panics! |
-|
| This code does not produce the desired behavior. |
+|
| این کد کامپایل نمیشود! |
+|
| این کد وحشت میکند! |
+|
| این کد رفتار مورد انتظار را تولید نمیکند. |
-In most situations, we’ll lead you to the correct version of any code that
-doesn’t compile.
+در بیشتر موارد، شما را به نسخه صحیح هر کدی که کامپایل نمیشود هدایت خواهیم کرد.
-## Source Code
+## کد منبع
-The source files from which this book is generated can be found on
-[GitHub][book].
+فایلهای منبعی که این کتاب از آنها تولید میشود را میتوانید در [GitHub][book] پیدا کنید.
-[book]: https://github.com/rust-lang/book/tree/main/src
+[book]: https://github.com/persian-rust/book/tree/main/src
diff --git a/src/ch01-00-getting-started.md b/src/ch01-00-getting-started.md
index ccb10e884e..1f2edbe1ec 100644
--- a/src/ch01-00-getting-started.md
+++ b/src/ch01-00-getting-started.md
@@ -1,8 +1,8 @@
-# Getting Started
+# شروع به کار
-Let’s start your Rust journey! There’s a lot to learn, but every journey starts
-somewhere. In this chapter, we’ll discuss:
+بیایید سفر خود به دنیای راست را آغاز کنیم! چیزهای زیادی برای یادگیری وجود دارد، اما هر سفری از جایی شروع میشود. در این فصل، درباره موارد زیر صحبت خواهیم کرد:
+
+- نصب راست بر روی لینوکس، macOS، و ویندوز
+- نوشتن برنامهای که `سلام دنیا!` را چاپ میکند
+- استفاده از `cargo`، مدیر بسته و سیستم ساخت راست
-- Installing Rust on Linux, macOS, and Windows
-- Writing a program that prints `Hello, world!`
-- Using `cargo`, Rust’s package manager and build system
diff --git a/src/ch01-01-installation.md b/src/ch01-01-installation.md
index cf2ca86db0..93caf4d674 100644
--- a/src/ch01-01-installation.md
+++ b/src/ch01-01-installation.md
@@ -1,154 +1,114 @@
-## Installation
+## نصب
-The first step is to install Rust. We’ll download Rust through `rustup`, a
-command line tool for managing Rust versions and associated tools. You’ll need
-an internet connection for the download.
+اولین قدم نصب راست است. ما راست را از طریق `rustup` دانلود میکنیم، ابزاری خط فرمان برای مدیریت نسخههای راست و ابزارهای مربوطه. برای دانلود به اتصال اینترنتی نیاز دارید.
-> Note: If you prefer not to use `rustup` for some reason, please see the
-> [Other Rust Installation Methods page][otherinstall] for more options.
+> توجه: اگر به هر دلیلی ترجیح میدهید از `rustup` استفاده نکنید، لطفاً صفحه [روشهای نصب دیگر راست][otherinstall] را برای گزینههای بیشتر مشاهده کنید.
-The following steps install the latest stable version of the Rust compiler.
-Rust’s stability guarantees ensure that all the examples in the book that
-compile will continue to compile with newer Rust versions. The output might
-differ slightly between versions because Rust often improves error messages and
-warnings. In other words, any newer, stable version of Rust you install using
-these steps should work as expected with the content of this book.
+مراحل زیر نسخه پایدار جدیدترین کامپایلر راست را نصب میکنند. تضمینهای پایداری راست اطمینان میدهند که تمام مثالهای کتاب که کامپایل میشوند، با نسخههای جدیدتر راست نیز کامپایل خواهند شد. خروجی ممکن است کمی متفاوت باشد، زیرا راست به طور مرتب پیغامهای خطا و هشدارها را بهبود میبخشد. به عبارت دیگر، هر نسخه پایدار جدیدی که با این مراحل نصب کنید، باید با محتوای این کتاب به درستی کار کند.
-> ### Command Line Notation
+> ### یادداشت دستورات خط فرمان
>
-> In this chapter and throughout the book, we’ll show some commands used in the
-> terminal. Lines that you should enter in a terminal all start with `$`. You
-> don’t need to type the `$` character; it’s the command line prompt shown to
-> indicate the start of each command. Lines that don’t start with `$` typically
-> show the output of the previous command. Additionally, PowerShell-specific
-> examples will use `>` rather than `$`.
+> در این فصل و throughout the book، ما برخی از دستورات استفاده شده در ترمینال را نمایش خواهیم داد. خطوطی که باید در ترمینال وارد کنید، همگی با `$` شروع میشوند. شما نیازی به وارد کردن نماد `$` ندارید؛ این نماد نشاندهنده شروع هر دستور است. خطوطی که با `$` شروع نمیشوند معمولاً خروجی دستور قبلی را نشان میدهند. علاوه بر این، مثالهای خاص PowerShell از `>` به جای `$` استفاده میکنند.
-### Installing `rustup` on Linux or macOS
+### نصب `rustup` در لینوکس یا macOS
-If you’re using Linux or macOS, open a terminal and enter the following command:
+اگر از لینوکس یا macOS استفاده میکنید، یک ترمینال باز کرده و دستور زیر را وارد کنید:
```console
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
```
-The command downloads a script and starts the installation of the `rustup`
-tool, which installs the latest stable version of Rust. You might be prompted
-for your password. If the install is successful, the following line will appear:
+این دستور یک اسکریپت دانلود کرده و نصب ابزار `rustup` را آغاز میکند که نسخه پایدار جدید راست را نصب میکند. ممکن است از شما خواسته شود تا رمز عبور خود را وارد کنید. اگر نصب موفقیتآمیز بود، خط زیر ظاهر میشود:
```text
Rust is installed now. Great!
```
-You will also need a _linker_, which is a program that Rust uses to join its
-compiled outputs into one file. It is likely you already have one. If you get
-linker errors, you should install a C compiler, which will typically include a
-linker. A C compiler is also useful because some common Rust packages depend on
-C code and will need a C compiler.
+همچنین به یک _لینکر_ نیاز خواهید داشت که برنامهای است که راست از آن برای ترکیب خروجیهای کامپایل شده خود به یک فایل استفاده میکند. احتمالاً شما یک لینکر دارید. اگر با ارورهای لینکر روبهرو شدید، باید یک کامپایلر C نصب کنید که معمولاً لینکر را نیز شامل میشود. یک کامپایلر C همچنین مفید است زیرا برخی از پکیجهای رایج راست به کد C وابستهاند و به یک کامپایلر C نیاز دارند.
-On macOS, you can get a C compiler by running:
+برای نصب کامپایلر C در macOS، دستور زیر را اجرا کنید:
```console
$ xcode-select --install
```
-Linux users should generally install GCC or Clang, according to their
-distribution’s documentation. For example, if you use Ubuntu, you can install
-the `build-essential` package.
+کاربران لینوکس معمولاً باید GCC یا Clang را طبق مستندات توزیع خود نصب کنند. برای مثال، اگر از اوبونتو استفاده میکنید، میتوانید پکیج `build-essential` را نصب کنید.
-### Installing `rustup` on Windows
+### نصب `rustup` در ویندوز
-On Windows, go to [https://www.rust-lang.org/tools/install][install] and follow
-the instructions for installing Rust. At some point in the installation, you’ll
-be prompted to install Visual Studio. This provides a linker and the native
-libraries needed to compile programs. If you need more help with this step, see
-[https://rust-lang.github.io/rustup/installation/windows-msvc.html][msvc]
+در ویندوز، به [https://www.rust-lang.org/tools/install][install] بروید و دستورالعملهای نصب راست را دنبال کنید. در یک مرحله از نصب، از شما خواسته میشود تا Visual Studio را نصب کنید. این ابزار یک لینکر و کتابخانههای بومی لازم برای کامپایل برنامهها را فراهم میکند. اگر به کمک بیشتری نیاز دارید، این صفحه را مشاهده کنید [https://rust-lang.github.io/rustup/installation/windows-msvc.html][msvc]
-The rest of this book uses commands that work in both _cmd.exe_ and PowerShell.
-If there are specific differences, we’ll explain which to use.
+بقیه کتاب از دستورات استفاده شده در _cmd.exe_ و PowerShell استفاده میکند. اگر تفاوتهای خاصی وجود داشته باشد، توضیح خواهیم داد که کدام را باید استفاده کنید.
-### Troubleshooting
+### عیبیابی
-To check whether you have Rust installed correctly, open a shell and enter this
-line:
+برای بررسی اینکه راست به درستی نصب شده است یا خیر، یک شل باز کرده و این دستور را وارد کنید:
```console
$ rustc --version
```
-You should see the version number, commit hash, and commit date for the latest
-stable version that has been released, in the following format:
+باید شماره نسخه، هش کمیّت و تاریخ کمیّت برای جدیدترین نسخه پایدار منتشر شده را به صورت زیر ببینید:
```text
rustc x.y.z (abcabcabc yyyy-mm-dd)
```
-If you see this information, you have installed Rust successfully! If you don’t
-see this information, check that Rust is in your `%PATH%` system variable as
-follows.
+اگر این اطلاعات را مشاهده کردید، راست به درستی نصب شده است! اگر این اطلاعات را مشاهده نکردید، بررسی کنید که راست در متغیر سیستم `%PATH%` شما قرار دارد.
-In Windows CMD, use:
+در CMD ویندوز، از دستور زیر استفاده کنید:
```console
> echo %PATH%
```
-In PowerShell, use:
+در PowerShell، از دستور زیر استفاده کنید:
```powershell
> echo $env:Path
```
-In Linux and macOS, use:
+در لینوکس و macOS، از دستور زیر استفاده کنید:
```console
$ echo $PATH
```
-If that’s all correct and Rust still isn’t working, there are a number of
-places you can get help. Find out how to get in touch with other Rustaceans (a
-silly nickname we call ourselves) on [the community page][community].
+اگر همه چیز درست باشد و راست همچنان کار نکند، منابع زیادی برای کمک وجود دارد. برای تماس با سایر راستنویسان (لقب خندهداری که خودمان به کار میبریم)، به صفحه [اجتماع][community] مراجعه کنید.
-### Updating and Uninstalling
+### بروزرسانی و حذف نصب
-Once Rust is installed via `rustup`, updating to a newly released version is
-easy. From your shell, run the following update script:
+بعد از نصب راست از طریق `rustup`، بروزرسانی به نسخه جدید بسیار آسان است. از شل خود دستور زیر را اجرا کنید:
```console
$ rustup update
```
-To uninstall Rust and `rustup`, run the following uninstall script from your
-shell:
+برای حذف نصب راست و `rustup`، اسکریپت حذف زیر را از شل خود اجرا کنید:
```console
$ rustup self uninstall
```
-### Local Documentation
+### مستندات محلی
-The installation of Rust also includes a local copy of the documentation so
-that you can read it offline. Run `rustup doc` to open the local documentation
-in your browser.
+نصب راست همچنین شامل یک نسخه محلی از مستندات است تا بتوانید آن را به صورت آفلاین مطالعه کنید. برای باز کردن مستندات محلی در مرورگر خود، دستور `rustup doc` را اجرا کنید.
-Any time a type or function is provided by the standard library and you’re not
-sure what it does or how to use it, use the application programming interface
-(API) documentation to find out!
+هر زمان که از یک نوع یا تابع ارائهشده توسط کتابخانه استاندارد استفاده میکنید و مطمئن نیستید که چه کار میکند یا چگونه از آن استفاده کنید، از مستندات رابط برنامهنویسی (API) برای یافتن آن استفاده کنید!
-### Text Editors and Integrated Development Environments
+### ویرایشگرهای متن و محیطهای توسعه یکپارچه
-This book makes no assumptions about what tools you use to author Rust code.
-Just about any text editor will get the job done! However, many text editors and
-integrated development environments (IDEs) have built-in support for Rust. You
-can always find a fairly current list of many editors and IDEs on [the tools
-page][tools] on the Rust website.
+این کتاب هیچ فرضی درباره ابزارهایی که برای نوشتن کد راست استفاده میکنید، ندارد. تقریباً هر ویرایشگر متنی کار را انجام میدهد! با این حال، بسیاری از ویرایشگرها و محیطهای توسعه یکپارچه (IDE) پشتیبانی داخلی برای راست دارند. همیشه میتوانید فهرست نسبتاً جدیدی از بسیاری از ویرایشگرها و IDEها را در [صفحه ابزارها][tools] در وبسایت راست پیدا کنید.
-### Working Offline with This Book
+### کار با این کتاب بهصورت آفلاین
-In several examples, we will use Rust packages beyond the standard library. To
-work through those examples, you will either need to have an internet connection
-or to have downloaded those dependencies ahead of time. To download the
-dependencies ahead of time, you can run the following commands. (We’ll explain
-what `cargo` is and what each of these commands does in detail later.)
+در چندین مثال، از پکیجهایی در Rust استفاده خواهیم کرد
+که فراتر از کتابخانه استاندارد هستند. برای اجرای این
+مثالها، یا باید به اینترنت متصل باشید یا اینکه
+پیشاپیش این وابستگیها را دانلود کرده باشید.
+برای دانلود پیشاپیش این وابستگیها، میتوانید
+دستورات زیر را اجرا کنید. (در ادامه، `cargo` و
+عملکرد هرکدام از این دستورات را بهطور کامل توضیح خواهیم داد.)
```console
$ cargo new get-dependencies
@@ -156,11 +116,15 @@ $ cd get-dependencies
$ cargo add rand@0.8.5 trpl@0.2.0
```
-This will cache the downloads for these packages so you will not need to
-download them later. Once you have run this command, you do not need to keep the
-`get-dependencies` folder. If you have run this command, you can use the
-`--offline` flag with all `cargo` commands in the rest of the book to use these
-cached versions instead of attempting to use the network.
+این کار نسخههای دانلودشدهی این پکیجها را در کش
+ذخیره میکند تا در آینده نیازی به دانلود مجدد نباشد.
+پس از اجرای این دستورات، نیازی به نگهداشتن
+پوشهی `get-dependencies` ندارید. اگر این دستورات
+را اجرا کرده باشید، میتوانید در باقی قسمتهای
+این کتاب از فلگ `--offline` همراه با تمام دستورات
+`cargo` استفاده کنید تا بهجای اتصال به شبکه،
+از نسخههای کششده بهره ببرید.
+
[otherinstall]: https://forge.rust-lang.org/infra/other-installation-methods.html
[install]: https://www.rust-lang.org/tools/install
diff --git a/src/ch01-02-hello-world.md b/src/ch01-02-hello-world.md
index 6c74dc1f75..aecc77e830 100644
--- a/src/ch01-02-hello-world.md
+++ b/src/ch01-02-hello-world.md
@@ -1,28 +1,17 @@
-## Hello, World!
+## سلام، دنیا!
-Now that you’ve installed Rust, it’s time to write your first Rust program.
-It’s traditional when learning a new language to write a little program that
-prints the text `Hello, world!` to the screen, so we’ll do the same here!
+حالا که Rust را نصب کردهاید، وقت آن است که اولین برنامهی Rust خود را بنویسید.
+وقتی زبان جدیدی را یاد میگیرید، معمولاً یک برنامه کوچک مینویسید که متن `Hello, world!` را به صفحه نمایش چاپ کند، پس ما هم همین کار را خواهیم کرد!
-> Note: This book assumes basic familiarity with the command line. Rust makes
-> no specific demands about your editing or tooling or where your code lives, so
-> if you prefer to use an integrated development environment (IDE) instead of
-> the command line, feel free to use your favorite IDE. Many IDEs now have some
-> degree of Rust support; check the IDE’s documentation for details. The Rust
-> team has been focusing on enabling great IDE support via `rust-analyzer`. See
-> [Appendix D][devtools] for more details.
+> نکته: این کتاب فرض میکند که شما با خط فرمان آشنایی پایهای دارید. Rust هیچگونه الزامی در مورد ویرایش یا ابزارهای شما یا جایی که کد شما قرار دارد ندارد، بنابراین اگر ترجیح میدهید از یک محیط توسعه یکپارچه (IDE) به جای خط فرمان استفاده کنید، میتوانید از IDE مورد علاقه خود استفاده کنید. بسیاری از IDEها اکنون از Rust پشتیبانی میکنند؛ برای جزئیات، مستندات IDE خود را بررسی کنید. تیم Rust تمرکز خود را بر enabling پشتیبانی خوب از IDE از طریق `rust-analyzer` گذاشته است. برای جزئیات بیشتر، به [ضمیمه د][devtools] مراجعه کنید.
-### Creating a Project Directory
+### ایجاد یک دایرکتوری پروژه
-You’ll start by making a directory to store your Rust code. It doesn’t matter
-to Rust where your code lives, but for the exercises and projects in this book,
-we suggest making a _projects_ directory in your home directory and keeping all
-your projects there.
+شما با ایجاد یک دایرکتوری برای ذخیره کدهای Rust خود شروع خواهید کرد. برای Rust مهم نیست که کد شما کجا قرار دارد، اما برای تمرینها و پروژههای این کتاب، پیشنهاد میکنیم یک دایرکتوری _projects_ در دایرکتوری خانهتان بسازید و تمام پروژههایتان را در آن نگهدارید.
-Open a terminal and enter the following commands to make a _projects_ directory
-and a directory for the “Hello, world!” project within the _projects_ directory.
+یک ترمینال باز کنید و دستورات زیر را وارد کنید تا یک دایرکتوری _projects_ و یک دایرکتوری برای پروژهی "Hello, world!" در داخل دایرکتوری _projects_ ایجاد کنید.
-For Linux, macOS, and PowerShell on Windows, enter this:
+برای لینوکس، macOS، و PowerShell در ویندوز، این دستورات را وارد کنید:
```console
$ mkdir ~/projects
@@ -31,7 +20,7 @@ $ mkdir hello_world
$ cd hello_world
```
-For Windows CMD, enter this:
+برای CMD ویندوز، این دستورات را وارد کنید:
```cmd
> mkdir "%USERPROFILE%\projects"
@@ -40,16 +29,13 @@ For Windows CMD, enter this:
> cd hello_world
```
-### Writing and Running a Rust Program
+### نوشتن و اجرای یک برنامه Rust
-Next, make a new source file and call it _main.rs_. Rust files always end with
-the _.rs_ extension. If you’re using more than one word in your filename, the
-convention is to use an underscore to separate them. For example, use
-_hello_world.rs_ rather than _helloworld.rs_.
+حالا یک فایل سورس جدید بسازید و آن را _main.rs_ نامگذاری کنید. فایلهای Rust همیشه با پسوند _.rs_ تمام میشوند. اگر از بیش از یک کلمه در نام فایل استفاده میکنید، سنت معمول این است که از خط تیره زیر برای جدا کردن آنها استفاده کنید. به عنوان مثال، از _hello_world.rs_ به جای _helloworld.rs_ استفاده کنید.
-Now open the _main.rs_ file you just created and enter the code in Listing 1-1.
+حالا فایل _main.rs_ که تازه ایجاد کردهاید را باز کنید و کد موجود در فهرست 1-1 را وارد کنید.
-
+
```rust
fn main() {
@@ -59,9 +45,8 @@ fn main() {
-Save the file and go back to your terminal window in the
-_~/projects/hello_world_ directory. On Linux or macOS, enter the following
-commands to compile and run the file:
+فایل را ذخیره کنید و به پنجره ترمینال خود در دایرکتوری
+_~/projects/hello_world_ برگردید. در لینوکس یا macOS، دستورات زیر را وارد کنید تا فایل را کامپایل کرده و اجرا کنید:
```console
$ rustc main.rs
@@ -69,7 +54,7 @@ $ ./main
Hello, world!
```
-On Windows, enter the command `.\main` instead of `./main`:
+در ویندوز، به جای `./main` دستور `.\main.exe` را وارد کنید:
```powershell
> rustc main.rs
@@ -77,18 +62,13 @@ On Windows, enter the command `.\main` instead of `./main`:
Hello, world!
```
-Regardless of your operating system, the string `Hello, world!` should print to
-the terminal. If you don’t see this output, refer back to the
-[“Troubleshooting”][troubleshooting] part of the Installation
-section for ways to get help.
+صرفنظر از سیستمعامل شما، رشته `Hello, world!` باید در ترمینال چاپ شود. اگر این خروجی را مشاهده نکردید، به بخش [“رفع مشکلات”][troubleshooting] در قسمت نصب مراجعه کنید تا روشهای دریافت کمک را بیابید.
-If `Hello, world!` did print, congratulations! You’ve officially written a Rust
-program. That makes you a Rust programmer—welcome!
+اگر `Hello, world!` چاپ شد، تبریک میگوییم! شما به طور رسمی یک برنامه نویس Rust شدهاید—خوش آمدید!
-### Anatomy of a Rust Program
+### آناتومی یک برنامه Rust
-Let’s review this “Hello, world!” program in detail. Here’s the first piece of
-the puzzle:
+بیایید این برنامه "Hello, world!" را به طور دقیق بررسی کنیم. این اولین بخش معما است:
```rust
fn main() {
@@ -96,105 +76,77 @@ fn main() {
}
```
-These lines define a function named `main`. The `main` function is special: it
-is always the first code that runs in every executable Rust program. Here, the
-first line declares a function named `main` that has no parameters and returns
-nothing. If there were parameters, they would go inside the parentheses `()`.
+این خطوط یک تابع به نام `main` تعریف میکنند. تابع `main` خاص است: همیشه اولین کدی است که در هر برنامه Rust اجرایی اجرا میشود. در اینجا، خط اول یک تابع به نام `main` اعلام میکند که هیچ پارامتر ندارد و هیچ چیزی را برنمیگرداند. اگر پارامترهایی وجود داشتند، آنها داخل پرانتزهای `()` قرار میگرفتند.
-The function body is wrapped in `{}`. Rust requires curly brackets around all
-function bodies. It’s good style to place the opening curly bracket on the same
-line as the function declaration, adding one space in between.
+بدن تابع در `{}` قرار دارد. Rust از آکولادها برای احاطه کردن تمام بدنههای توابع استفاده میکند. این یک سبک خوب است که آکولاد باز را در همان خط اعلام تابع قرار دهید و یک فضای خالی بین آنها اضافه کنید.
-> Note: If you want to stick to a standard style across Rust projects, you can
-> use an automatic formatter tool called `rustfmt` to format your code in a
-> particular style (more on `rustfmt` in
-> [Appendix D][devtools]). The Rust team has included this tool
-> with the standard Rust distribution, as `rustc` is, so it should already be
-> installed on your computer!
+> نکته: اگر میخواهید در پروژههای Rust خود از یک سبک استاندارد پیروی کنید، میتوانید از ابزاری به نام `rustfmt` برای فرمت کردن کد خود در یک سبک خاص استفاده کنید (بیشتر در مورد `rustfmt` در [ضمیمه د][devtools]). تیم Rust این ابزار را همراه با توزیع استاندارد Rust شامل کرده است، همانطور که `rustc` است، بنابراین باید قبلاً روی کامپیوتر شما نصب شده باشد!
-The body of the `main` function holds the following code:
+بدن تابع `main` شامل کد زیر است:
```rust
println!("Hello, world!");
```
-This line does all the work in this little program: it prints text to the
-screen. There are three important details to notice here.
+این خط، تمام کار این برنامهی کوچک را انجام میدهد:
+متنی را روی صفحه چاپ میکند. در اینجا سه نکتهی
+مهم وجود دارد که باید به آنها توجه کنید.
-First, `println!` calls a Rust macro. If it had called a function instead, it
-would be entered as `println` (without the `!`). Rust macros are a way to write
-code that generates code to extend Rust syntax, and we’ll discuss them in more
-detail in [Chapter 20][ch20-macros]. For now, you just need to
-know that using a `!` means that you’re calling a macro instead of a normal
-function and that macros don’t always follow the same rules as functions.
+نخست، `println!` یک ماکرو در Rust را فراخوانی میکند.
+اگر بهجای ماکرو، یک تابع فراخوانی شده بود،
+نگارش آن بهصورت `println` (بدون `!`) میبود.
+ماکروهای Rust روشی برای نوشتن کدی هستند که
+کد دیگری تولید میکنند و به گسترش نگارش Rust
+کمک میکنند. در فصل بیستم [Chapter 20][ch20-macros]
+آنها را با جزئیات بیشتری بررسی خواهیم کرد.
+در حال حاضر تنها کافیست بدانید که استفاده از
+`!` به این معناست که در حال فراخوانی یک ماکرو هستید
+و ماکروها همیشه از همان قواعدی که توابع پیروی میکنند،
+تبعیت نمیکنند.
-Second, you see the `"Hello, world!"` string. We pass this string as an argument
-to `println!`, and the string is printed to the screen.
+دوم، شما رشته `"Hello, world!"` را مشاهده میکنید. این رشته را به عنوان آرگومان به `println!` میدهیم و این رشته به صفحه نمایش چاپ میشود.
-Third, we end the line with a semicolon (`;`), which indicates that this
-expression is over and the next one is ready to begin. Most lines of Rust code
-end with a semicolon.
+سوم، خط را با یک نقطهویرگول (`;`) تمام میکنیم که نشان میدهد این عبارت تمام شده و عبارت بعدی آماده شروع است. بیشتر خطوط کد Rust با نقطهویرگول تمام میشوند.
-### Compiling and Running Are Separate Steps
+### کامپایل کردن و اجرا کردن مراحل جداگانه هستند
-You’ve just run a newly created program, so let’s examine each step in the
-process.
+شما به تازگی یک برنامه جدید ایجاد شده را اجرا کردهاید، بنابراین بیایید هر مرحله از فرآیند را بررسی کنیم.
-Before running a Rust program, you must compile it using the Rust compiler by
-entering the `rustc` command and passing it the name of your source file, like
-this:
+قبل از اجرای یک برنامه Rust، باید آن را با استفاده از کامپایلر Rust کامپایل کنید. برای این کار باید دستور `rustc` را وارد کرده و نام فایل سورس خود را به آن بدهید، مانند این:
```console
$ rustc main.rs
```
-If you have a C or C++ background, you’ll notice that this is similar to `gcc`
-or `clang`. After compiling successfully, Rust outputs a binary executable.
+اگر پیشزمینهای از C یا C++ دارید، متوجه خواهید شد که این مشابه دستور `gcc` یا `clang` است. پس از کامپایل موفق، Rust یک فایل اجرایی باینری تولید میکند.
-On Linux, macOS, and PowerShell on Windows, you can see the executable by
-entering the `ls` command in your shell:
+در لینوکس، macOS و PowerShell در ویندوز، میتوانید فایل اجرایی را با وارد کردن دستور `ls` در شل خود مشاهده کنید:
```console
$ ls
main main.rs
```
-On Linux and macOS, you’ll see two files. With PowerShell on Windows, you’ll
-see the same three files that you would see using CMD. With CMD on Windows, you
-would enter the following:
+در لینوکس و macOS، شما دو فایل خواهید دید. در PowerShell در ویندوز، همان سه فایلی را که با CMD میبینید مشاهده خواهید کرد. در CMD در ویندوز، باید دستور زیر را وارد کنید:
```cmd
-> dir /B %= the /B option says to only show the file names =%
+> dir /B %= گزینه /B میگوید که فقط نام فایلها نمایش داده شود =%
main.exe
main.pdb
main.rs
```
-This shows the source code file with the _.rs_ extension, the executable file
-(_main.exe_ on Windows, but _main_ on all other platforms), and, when using
-Windows, a file containing debugging information with the _.pdb_ extension.
-From here, you run the _main_ or _main.exe_ file, like this:
+این لیست فایل سورس با پسوند _.rs_، فایل اجرایی (_main.exe_ در ویندوز، اما _main_ در سایر پلتفرمها)، و در صورت استفاده از ویندوز، یک فایل شامل اطلاعات دیباگ با پسوند _.pdb_ را نشان میدهد. از اینجا، شما فایل _main_ یا _main.exe_ را اجرا میکنید، مانند این:
```console
-$ ./main # or .\main on Windows
+$ ./main # یا .\main.exe در ویندوز
```
-If your _main.rs_ is your “Hello, world!” program, this line prints `Hello,
-world!` to your terminal.
-
-If you’re more familiar with a dynamic language, such as Ruby, Python, or
-JavaScript, you might not be used to compiling and running a program as
-separate steps. Rust is an _ahead-of-time compiled_ language, meaning you can
-compile a program and give the executable to someone else, and they can run it
-even without having Rust installed. If you give someone a _.rb_, _.py_, or
-_.js_ file, they need to have a Ruby, Python, or JavaScript implementation
-installed (respectively). But in those languages, you only need one command to
-compile and run your program. Everything is a trade-off in language design.
-
-Just compiling with `rustc` is fine for simple programs, but as your project
-grows, you’ll want to manage all the options and make it easy to share your
-code. Next, we’ll introduce you to the Cargo tool, which will help you write
-real-world Rust programs.
+اگر فایل _main.rs_ شما برنامه "Hello, world!" باشد، این خط `Hello, world!` را در ترمینال شما چاپ میکند.
+
+اگر با زبانهای داینامیک مانند Ruby، Python یا JavaScript آشنایی بیشتری دارید، ممکن است عادت نداشته باشید که کامپایل و اجرای یک برنامه را به عنوان مراحل جداگانه انجام دهید. Rust یک زبان _کامپایل شده پیش از زمان_ است، به این معنی که شما میتوانید یک برنامه را کامپایل کرده و فایل اجرایی را به شخص دیگری بدهید تا آن را اجرا کند، حتی بدون اینکه Rust روی سیستم آن شخص نصب شده باشد. اگر به کسی فایل _.rb_، _.py_ یا _.js_ بدهید، آنها نیاز به نصب پیادهسازی Ruby، Python یا JavaScript (به ترتیب) دارند. اما در این زبانها، شما فقط به یک دستور نیاز دارید تا برنامه خود را کامپایل و اجرا کنید. همه چیز در طراحی زبانها یک تعادل است.
+
+فقط با کامپایل کردن با `rustc` برای برنامههای ساده کافی است، اما با رشد پروژه شما، میخواهید تمام گزینهها را مدیریت کرده و اشتراکگذاری کد خود را آسان کنید. در ادامه، ما ابزار Cargo را معرفی خواهیم کرد که به شما کمک میکند برنامههای واقعی Rust بنویسید.
[troubleshooting]: ch01-01-installation.html#troubleshooting
[devtools]: appendix-04-useful-development-tools.html
diff --git a/src/ch01-03-hello-cargo.md b/src/ch01-03-hello-cargo.md
index 100239322c..6706648561 100644
--- a/src/ch01-03-hello-cargo.md
+++ b/src/ch01-03-hello-cargo.md
@@ -1,64 +1,38 @@
-## Hello, Cargo!
-
-Cargo is Rust’s build system and package manager. Most Rustaceans use this tool
-to manage their Rust projects because Cargo handles a lot of tasks for you,
-such as building your code, downloading the libraries your code depends on, and
-building those libraries. (We call the libraries that your code needs
-_dependencies_.)
-
-The simplest Rust programs, like the one we’ve written so far, don’t have any
-dependencies. If we had built the “Hello, world!” project with Cargo, it would
-only use the part of Cargo that handles building your code. As you write more
-complex Rust programs, you’ll add dependencies, and if you start a project
-using Cargo, adding dependencies will be much easier to do.
-
-Because the vast majority of Rust projects use Cargo, the rest of this book
-assumes that you’re using Cargo too. Cargo comes installed with Rust if you
-used the official installers discussed in the
-[“Installation”][installation] section. If you installed Rust
-through some other means, check whether Cargo is installed by entering the
-following in your terminal:
+## سلام، Cargo!
+
+Cargo سیستم ساخت و مدیر بستههای Rust است. بیشتر Rustacean ها از این ابزار برای مدیریت پروژههای Rust خود استفاده میکنند زیرا Cargo بسیاری از وظایف را برای شما انجام میدهد، مانند ساختن کد شما، دانلود کتابخانههایی که کد شما به آنها وابسته است، و ساختن آن کتابخانهها. (ما به کتابخانههایی که کد شما به آنها نیاز دارد _وابستگیها_ میگوییم.)
+
+سادهترین برنامههای Rust، مانند برنامهای که تا کنون نوشتهایم، هیچ وابستگیای ندارند. اگر پروژه "Hello, world!" را با Cargo میساختیم، فقط از بخشی از Cargo استفاده میکرد که مسئول ساختن کد شما است. هنگامی که برنامههای پیچیدهتری در Rust بنویسید، وابستگیها را اضافه خواهید کرد و اگر پروژهای را با استفاده از Cargo شروع کنید، اضافه کردن وابستگیها بسیار راحتتر خواهد بود.
+
+به دلیل اینکه اکثریت عظیم پروژههای Rust از Cargo استفاده میکنند، بقیه این کتاب فرض میکند که شما نیز از Cargo استفاده میکنید. Cargo با Rust نصب میشود اگر از نصبکنندههای رسمی که در بخش
+[“نصب”][installation] بحث شدهاند استفاده کرده باشید. اگر Rust را از طریق روشهای دیگری نصب کردهاید، بررسی کنید که آیا Cargo نصب شده است یا نه با وارد کردن دستور زیر در ترمینال خود:
```console
$ cargo --version
```
-If you see a version number, you have it! If you see an error, such as `command
-not found`, look at the documentation for your method of installation to
-determine how to install Cargo separately.
+اگر شماره نسخهای مشاهده کردید، آن را دارید! اگر خطای `command not found` را دیدید، به مستندات روش نصب خود مراجعه کنید تا نحوه نصب جداگانه Cargo را پیدا کنید.
-### Creating a Project with Cargo
+### ایجاد یک پروژه با Cargo
-Let’s create a new project using Cargo and look at how it differs from our
-original “Hello, world!” project. Navigate back to your _projects_ directory
-(or wherever you decided to store your code). Then, on any operating system,
-run the following:
+بیایید یک پروژه جدید با استفاده از Cargo بسازیم و ببینیم چگونه از پروژه اولیه "Hello, world!" ما متفاوت است. به دایرکتوری _projects_ خود بروید (یا هر جایی که تصمیم گرفتهاید کد خود را ذخیره کنید). سپس، در هر سیستمعاملی، دستور زیر را وارد کنید:
```console
$ cargo new hello_cargo
$ cd hello_cargo
```
-The first command creates a new directory and project called _hello_cargo_.
-We’ve named our project _hello_cargo_, and Cargo creates its files in a
-directory of the same name.
+دستور اول یک دایرکتوری جدید به نام _hello_cargo_ ایجاد میکند و پروژهای به همین نام ایجاد میکند. ما پروژه خود را _hello_cargo_ نامگذاری کردهایم و Cargo فایلهای خود را در دایرکتوری به همین نام ایجاد میکند.
-Go into the _hello_cargo_ directory and list the files. You’ll see that Cargo
-has generated two files and one directory for us: a _Cargo.toml_ file and a
-_src_ directory with a _main.rs_ file inside.
+به دایرکتوری _hello_cargo_ بروید و فایلها را لیست کنید. خواهید دید که Cargo دو فایل و یک دایرکتوری برای ما ایجاد کرده است: یک فایل _Cargo.toml_ و یک دایرکتوری _src_ که داخل آن یک فایل _main.rs_ است.
-It has also initialized a new Git repository along with a _.gitignore_ file.
-Git files won’t be generated if you run `cargo new` within an existing Git
-repository; you can override this behavior by using `cargo new --vcs=git`.
+همچنین یک مخزن Git جدید به همراه یک فایل _.gitignore_ ایجاد شده است. فایلهای Git در صورتی که دستور `cargo new` را در یک مخزن Git موجود اجرا کنید، ایجاد نمیشوند؛ میتوانید این رفتار را با استفاده از `cargo new --vcs=git` لغو کنید.
-> Note: Git is a common version control system. You can change `cargo new` to
-> use a different version control system or no version control system by using
-> the `--vcs` flag. Run `cargo new --help` to see the available options.
+> نکته: Git یک سیستم کنترل نسخه رایج است. شما میتوانید دستور `cargo new` را تغییر دهید تا از سیستم کنترل نسخهای متفاوت یا هیچ سیستم کنترل نسخهای استفاده کند با استفاده از پرچم `--vcs`. برای دیدن گزینههای موجود، دستور `cargo new --help` را اجرا کنید.
-Open _Cargo.toml_ in your text editor of choice. It should look similar to the
-code in Listing 1-2.
+فایل _Cargo.toml_ را در ویرایشگر متن دلخواه خود باز کنید. این فایل باید مشابه کدی باشد که در فهرست 1-2 آمده است.
-
+
```toml
[package]
@@ -71,23 +45,15 @@ edition = "2024"
-This file is in the [_TOML_][toml] (_Tom’s Obvious, Minimal
-Language_) format, which is Cargo’s configuration format.
+این فایل در فرمت [_TOML_][toml] (_زبان ساده و آشکار تام_) است که فرمت پیکربندی Cargo است.
-The first line, `[package]`, is a section heading that indicates that the
-following statements are configuring a package. As we add more information to
-this file, we’ll add other sections.
+خط اول، `[package]`، یک عنوان بخش است که نشان میدهد بیانیههای بعدی در حال پیکربندی یک بسته هستند. همانطور که اطلاعات بیشتری به این فایل اضافه میکنیم، بخشهای دیگری را اضافه خواهیم کرد.
-The next three lines set the configuration information Cargo needs to compile
-your program: the name, the version, and the edition of Rust to use. We’ll talk
-about the `edition` key in [Appendix E][appendix-e].
+سه خط بعدی اطلاعات پیکربندیای را تنظیم میکنند که Cargo برای کامپایل برنامه شما به آنها نیاز دارد: نام، نسخه و نسخهای از Rust که باید استفاده شود. در مورد کلید `edition` در [ضمیمه ه][appendix-e] صحبت خواهیم کرد.
-The last line, `[dependencies]`, is the start of a section for you to list any
-of your project’s dependencies. In Rust, packages of code are referred to as
-_crates_. We won’t need any other crates for this project, but we will in the
-first project in Chapter 2, so we’ll use this dependencies section then.
+آخرین خط، `[dependencies]`، شروع یک بخش است که شما باید وابستگیهای پروژه خود را در آن ذکر کنید. در Rust، بستههای کد به نام _کرِیتها_ شناخته میشوند. برای این پروژه نیازی به کرِیتهای دیگر نداریم، اما در پروژه اول فصل 2 به آنها نیاز خواهیم داشت، بنابراین در آن زمان از این بخش وابستگیها استفاده خواهیم کرد.
-Now open _src/main.rs_ and take a look:
+حالا فایل _src/main.rs_ را باز کنید و نگاهی بیندازید:
Filename: src/main.rs
@@ -97,28 +63,15 @@ fn main() {
}
```
-Cargo has generated a “Hello, world!” program for you, just like the one we
-wrote in Listing 1-1! So far, the differences between our project and the
-project Cargo generated are that Cargo placed the code in the _src_ directory
-and we have a _Cargo.toml_ configuration file in the top directory.
+Cargo یک برنامه "Hello, world!" برای شما ایجاد کرده است، درست مانند برنامهای که در فهرست 1-1 نوشتیم! تا کنون، تفاوتهای بین پروژه ما و پروژهای که Cargo ایجاد کرده این است که Cargo کد را در دایرکتوری _src_ قرار داده و ما یک فایل پیکربندی _Cargo.toml_ در دایرکتوری بالای پروژه داریم.
-Cargo expects your source files to live inside the _src_ directory. The
-top-level project directory is just for README files, license information,
-configuration files, and anything else not related to your code. Using Cargo
-helps you organize your projects. There’s a place for everything, and
-everything is in its place.
+Cargo انتظار دارد که فایلهای منبع شما داخل دایرکتوری _src_ قرار داشته باشند. دایرکتوری بالای پروژه فقط برای فایلهای README، اطلاعات مجوز، فایلهای پیکربندی و هر چیز دیگری که مربوط به کد شما نباشد، استفاده میشود. استفاده از Cargo به شما کمک میکند پروژههایتان را سازماندهی کنید. برای هر چیز جایی وجود دارد و همه چیز در جای خود قرار دارد.
-If you started a project that doesn’t use Cargo, as we did with the “Hello,
-world!” project, you can convert it to a project that does use Cargo. Move the
-project code into the _src_ directory and create an appropriate _Cargo.toml_
-file. One easy way to get that _Cargo.toml_ file is to run `cargo init`, which
-will create it for you automatically.
+اگر پروژهای شروع کردهاید که از Cargo استفاده نمیکند، همانطور که در پروژه "Hello, world!" انجام دادیم، میتوانید آن را به پروژهای که از Cargo استفاده میکند تبدیل کنید. کد پروژه را به دایرکتوری _src_ منتقل کرده و یک فایل _Cargo.toml_ مناسب ایجاد کنید. یکی از راههای آسان برای بهدست آوردن آن فایل _Cargo.toml_ این است که دستور `cargo init` را اجرا کنید که بهطور خودکار آن را برای شما ایجاد میکند.
-### Building and Running a Cargo Project
+### ساخت و اجرای پروژه با Cargo
-Now let’s look at what’s different when we build and run the “Hello, world!”
-program with Cargo! From your _hello_cargo_ directory, build your project by
-entering the following command:
+حالا بیایید ببینیم که چه تفاوتی در زمانی که برنامه "Hello, world!" را با Cargo میسازیم و اجرا میکنیم وجود دارد! از دایرکتوری _hello_cargo_ خود، پروژه را با وارد کردن دستور زیر بسازید:
```console
$ cargo build
@@ -126,26 +79,16 @@ $ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs
```
-This command creates an executable file in _target/debug/hello_cargo_ (or
-_target\debug\hello_cargo.exe_ on Windows) rather than in your current
-directory. Because the default build is a debug build, Cargo puts the binary in
-a directory named _debug_. You can run the executable with this command:
+این دستور یک فایل اجرایی در _target/debug/hello_cargo_ (یا _target\debug\hello_cargo.exe_ در ویندوز) ایجاد میکند به جای این که آن را در دایرکتوری فعلی شما قرار دهد. زیرا ساخت پیشفرض یک ساخت دیباگ است، Cargo فایل باینری را در دایرکتوری به نام _debug_ قرار میدهد. شما میتوانید فایل اجرایی را با این دستور اجرا کنید:
```console
-$ ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
+$ ./target/debug/hello_cargo # یا .\target\debug\hello_cargo.exe در ویندوز
Hello, world!
```
-If all goes well, `Hello, world!` should print to the terminal. Running `cargo
-build` for the first time also causes Cargo to create a new file at the top
-level: _Cargo.lock_. This file keeps track of the exact versions of
-dependencies in your project. This project doesn’t have dependencies, so the
-file is a bit sparse. You won’t ever need to change this file manually; Cargo
-manages its contents for you.
+اگر همه چیز درست پیش رفته باشد، `Hello, world!` باید در ترمینال چاپ شود. اجرای `cargo build` برای اولین بار همچنین باعث میشود که Cargo یک فایل جدید در بالای دایرکتوری ایجاد کند: _Cargo.lock_. این فایل نسخههای دقیق وابستگیهای پروژه شما را پیگیری میکند. چون این پروژه وابستگی ندارد، این فایل کمی خالی است. شما هیچگاه نیازی به تغییر دستی این فایل نخواهید داشت؛ Cargo محتویات آن را برای شما مدیریت میکند.
-We just built a project with `cargo build` and ran it with
-`./target/debug/hello_cargo`, but we can also use `cargo run` to compile the
-code and then run the resultant executable all in one command:
+ما همین حالا پروژه را با دستور `cargo build` ساختیم و با `./target/debug/hello_cargo` اجرا کردیم، اما همچنین میتوانیم از `cargo run` برای کامپایل کردن کد و سپس اجرای باینری حاصل در یک دستور استفاده کنیم:
```console
$ cargo run
@@ -154,15 +97,9 @@ $ cargo run
Hello, world!
```
-Using `cargo run` is more convenient than having to remember to run `cargo
-build` and then use the whole path to the binary, so most developers use `cargo
-run`.
+استفاده از `cargo run` راحتتر از این است که بخواهید دستور `cargo build` را اجرا کرده و سپس مسیر کامل به باینری را استفاده کنید، بنابراین بیشتر توسعهدهندگان از `cargo run` استفاده میکنند.
-Notice that this time we didn’t see output indicating that Cargo was compiling
-`hello_cargo`. Cargo figured out that the files hadn’t changed, so it didn’t
-rebuild but just ran the binary. If you had modified your source code, Cargo
-would have rebuilt the project before running it, and you would have seen this
-output:
+توجه کنید که این بار، خروجیای که نشان دهد Cargo در حال کامپایل کردن `hello_cargo` است، مشاهده نکردیم. Cargo متوجه شد که فایلها تغییر نکردهاند، بنابراین بازسازی نکرد و فقط باینری را اجرا کرد. اگر کد منبع خود را تغییر داده بودید، Cargo ابتدا پروژه را بازسازی میکرد و سپس آن را اجرا میکرد، و شما این خروجی را میدیدید:
```console
$ cargo run
@@ -172,8 +109,7 @@ $ cargo run
Hello, world!
```
-Cargo also provides a command called `cargo check`. This command quickly checks
-your code to make sure it compiles but doesn’t produce an executable:
+Cargo همچنین یک دستور به نام `cargo check` را فراهم میکند. این دستور کد شما را به سرعت بررسی میکند تا مطمئن شود که کامپایل میشود اما هیچ اجرایی تولید نمیکند:
```console
$ cargo check
@@ -181,52 +117,27 @@ $ cargo check
Finished dev [unoptimized + debuginfo] target(s) in 0.32 secs
```
-Why would you not want an executable? Often, `cargo check` is much faster than
-`cargo build` because it skips the step of producing an executable. If you’re
-continually checking your work while writing the code, using `cargo check` will
-speed up the process of letting you know if your project is still compiling! As
-such, many Rustaceans run `cargo check` periodically as they write their
-program to make sure it compiles. Then they run `cargo build` when they’re
-ready to use the executable.
-
-Let’s recap what we’ve learned so far about Cargo:
-
-- We can create a project using `cargo new`.
-- We can build a project using `cargo build`.
-- We can build and run a project in one step using `cargo run`.
-- We can build a project without producing a binary to check for errors using
- `cargo check`.
-- Instead of saving the result of the build in the same directory as our code,
- Cargo stores it in the _target/debug_ directory.
-
-An additional advantage of using Cargo is that the commands are the same no
-matter which operating system you’re working on. So, at this point, we’ll no
-longer provide specific instructions for Linux and macOS versus Windows.
-
-### Building for Release
-
-When your project is finally ready for release, you can use `cargo build
---release` to compile it with optimizations. This command will create an
-executable in _target/release_ instead of _target/debug_. The optimizations
-make your Rust code run faster, but turning them on lengthens the time it takes
-for your program to compile. This is why there are two different profiles: one
-for development, when you want to rebuild quickly and often, and another for
-building the final program you’ll give to a user that won’t be rebuilt
-repeatedly and that will run as fast as possible. If you’re benchmarking your
-code’s running time, be sure to run `cargo build --release` and benchmark with
-the executable in _target/release_.
-
-### Cargo as Convention
-
-With simple projects, Cargo doesn’t provide a lot of value over just using
-`rustc`, but it will prove its worth as your programs become more intricate.
-Once programs grow to multiple files or need a dependency, it’s much easier to
-let Cargo coordinate the build.
-
-Even though the `hello_cargo` project is simple, it now uses much of the real
-tooling you’ll use in the rest of your Rust career. In fact, to work on any
-existing projects, you can use the following commands to check out the code
-using Git, change to that project’s directory, and build:
+چرا شما به یک فایل اجرایی نیاز ندارید؟ اغلب، `cargo check` بسیار سریعتر از `cargo build` است زیرا مرحله تولید یک فایل اجرایی را رد میکند. اگر شما به طور مداوم در حال بررسی کد خود هستید، استفاده از `cargo check` سرعت فرایند اطلاع دادن به شما از این که پروژه هنوز کامپایل میشود را افزایش میدهد! به همین دلیل، بسیاری از Rustaceans به طور دورهای `cargo check` را در حین نوشتن کد خود اجرا میکنند تا مطمئن شوند که پروژهشان کامپایل میشود. سپس زمانی که آماده استفاده از باینری شدند، از دستور `cargo build` استفاده میکنند.
+
+بیایید خلاصهای از آنچه که تا به حال در مورد Cargo آموختهایم مرور کنیم:
+
+- ما میتوانیم یک پروژه با استفاده از `cargo new` بسازیم.
+- ما میتوانیم یک پروژه را با استفاده از `cargo build` بسازیم.
+- ما میتوانیم یک پروژه را با یک مرحله از ساخت و اجرا با استفاده از `cargo run` بسازیم و اجرا کنیم.
+- ما میتوانیم یک پروژه را بدون تولید باینری برای بررسی خطاها با استفاده از `cargo check` بسازیم.
+- به جای ذخیره نتیجه ساخت در همان دایرکتوری که کد ما قرار دارد، Cargo آن را در دایرکتوری _target/debug_ ذخیره میکند.
+
+یک مزیت اضافی استفاده از Cargo این است که دستورات آن در همه سیستمعاملها یکسان است. بنابراین، از این پس، دیگر دستورالعملهای خاصی برای لینوکس و macOS در مقابل ویندوز ارائه نخواهیم کرد.
+
+### ساخت برای انتشار
+
+وقتی پروژه شما آماده انتشار است، میتوانید از دستور `cargo build --release` برای کامپایل کردن آن با بهینهسازیها استفاده کنید. این دستور یک فایل اجرایی در دایرکتوری _target/release_ به جای _target/debug_ ایجاد میکند. بهینهسازیها باعث میشوند که کد Rust شما سریعتر اجرا شود، اما فعال کردن آنها زمان کامپایل برنامه را طولانیتر میکند. به همین دلیل، دو پروفایل مختلف وجود دارد: یکی برای توسعه که شما میخواهید سریعاً و به دفعات پروژه را بازسازی کنید، و دیگری برای ساختن برنامه نهایی که به کاربر تحویل خواهید داد، که به دفعات بازسازی نمیشود و باید سریعترین اجرا را داشته باشد. اگر در حال اندازهگیری زمان اجرای کد خود هستید، حتماً از دستور `cargo build --release` استفاده کنید و با فایل اجرایی در _target/release_ اندازهگیری کنید.
+
+### Cargo به عنوان یک کنوانسیون
+
+در پروژههای ساده، Cargo نسبت به استفاده از `rustc` مزیت زیادی ندارد، اما با پیچیدهتر شدن برنامهها، ارزش خود را نشان میدهد. زمانی که برنامهها به چندین فایل نیاز پیدا میکنند یا وابستگی دارند، استفاده از Cargo برای هماهنگ کردن فرایند ساخت بسیار راحتتر میشود.
+
+حتی اگر پروژه `hello_cargo` ساده باشد، اکنون از بسیاری از ابزارهای واقعی استفاده میکند که در طول مسیر Rust خود به آنها نیاز خواهید داشت. در واقع، برای کار بر روی هر پروژه موجود، میتوانید از دستورات زیر برای بررسی کد با استفاده از Git، تغییر به دایرکتوری آن پروژه و ساخت آن استفاده کنید:
```console
$ git clone example.org/someproject
@@ -234,25 +145,16 @@ $ cd someproject
$ cargo build
```
-For more information about Cargo, check out [its documentation][cargo].
-
-## Summary
+برای اطلاعات بیشتر در مورد Cargo، میتوانید به [مستندات آن](https://doc.rust-lang.org/cargo/) مراجعه کنید.
-You’re already off to a great start on your Rust journey! In this chapter,
-you’ve learned how to:
+## خلاصه
-- Install the latest stable version of Rust using `rustup`
-- Update to a newer Rust version
-- Open locally installed documentation
-- Write and run a “Hello, world!” program using `rustc` directly
-- Create and run a new project using the conventions of Cargo
+شما در حال حاضر شروع بسیار خوبی برای سفر خود در Rust دارید! در این فصل، شما یاد گرفتهاید که چگونه:
-This is a great time to build a more substantial program to get used to reading
-and writing Rust code. So, in Chapter 2, we’ll build a guessing game program.
-If you would rather start by learning how common programming concepts work in
-Rust, see Chapter 3 and then return to Chapter 2.
+- آخرین نسخه پایدار Rust را با استفاده از `rustup` نصب کنید.
+- به نسخه جدیدتر Rust بروزرسانی کنید.
+- مستندات محلی نصبشده را باز کنید.
+- یک برنامه “Hello, world!” را با استفاده از `rustc` مستقیماً بنویسید و اجرا کنید.
+- یک پروژه جدید را با استفاده از کنوانسیونهای Cargo بسازید و اجرا کنید.
-[installation]: ch01-01-installation.html#installation
-[toml]: https://toml.io
-[appendix-e]: appendix-05-editions.html
-[cargo]: https://doc.rust-lang.org/cargo/
+این زمان بسیار خوبی است که برنامهای بزرگتر بسازید تا با خواندن و نوشتن کد Rust بیشتر آشنا شوید. بنابراین، در فصل 2، یک برنامه بازی حدس زدن خواهیم ساخت. اگر ترجیح میدهید ابتدا یاد بگیرید که مفاهیم برنامهنویسی رایج در Rust چگونه کار میکنند، فصل 3 را مطالعه کنید و سپس به فصل 2 بازگردید.
diff --git a/src/ch02-00-guessing-game-tutorial.md b/src/ch02-00-guessing-game-tutorial.md
index 015a42a5f6..0a8ffc4901 100644
--- a/src/ch02-00-guessing-game-tutorial.md
+++ b/src/ch02-00-guessing-game-tutorial.md
@@ -1,42 +1,21 @@
-# Programming a Guessing Game
+# برنامهنویسی یک بازی حدس زدن
-Let’s jump into Rust by working through a hands-on project together! This
-chapter introduces you to a few common Rust concepts by showing you how to use
-them in a real program. You’ll learn about `let`, `match`, methods, associated
-functions, external crates, and more! In the following chapters, we’ll explore
-these ideas in more detail. In this chapter, you’ll just practice the
-fundamentals.
+بیایید با کار روی یک پروژه عملی با هم به دنیای Rust وارد شویم! این فصل با نشان دادن نحوه استفاده از مفاهیم رایج Rust در یک برنامه واقعی، شما را با آنها آشنا میکند. درباره `let`، `match`، متدها، توابع مرتبط (associated functions)، جعبهها (crates)ی خارجی و موارد دیگر خواهید آموخت! در فصلهای بعدی، این ایدهها را به طور مفصل بررسی خواهیم کرد. در این فصل، فقط اصول اولیه را تمرین میکنید.
-We’ll implement a classic beginner programming problem: a guessing game. Here’s
-how it works: the program will generate a random integer between 1 and 100. It
-will then prompt the player to enter a guess. After a guess is entered, the
-program will indicate whether the guess is too low or too high. If the guess is
-correct, the game will print a congratulatory message and exit.
+ما یک مسئله کلاسیک برنامهنویسی برای مبتدیان را پیادهسازی خواهیم کرد: یک بازی حدس زدن. این بازی به این صورت عمل میکند: برنامه یک عدد صحیح تصادفی بین 1 تا 100 تولید میکند. سپس از بازیکن میخواهد که یک حدس وارد کند. پس از وارد کردن حدس، برنامه مشخص میکند که آیا حدس خیلی پایین است یا خیلی بالا. اگر حدس درست باشد، برنامه یک پیام تبریک چاپ میکند و از بازی خارج میشود.
-## Setting Up a New Project
+## راهاندازی یک پروژه جدید
-To set up a new project, go to the _projects_ directory that you created in
-Chapter 1 and make a new project using Cargo, like so:
+برای راهاندازی یک پروژه جدید، به دایرکتوری _projects_ که در فصل 1 ایجاد کردید بروید و یک پروژه جدید با استفاده از Cargo ایجاد کنید، به این صورت:
```console
$ cargo new guessing_game
$ cd guessing_game
```
-The first command, `cargo new`, takes the name of the project (`guessing_game`)
-as the first argument. The second command changes to the new project’s
-directory.
+دستور اول، `cargo new`، نام پروژه (`guessing_game`) را به عنوان آرگومان اول میگیرد. دستور دوم به دایرکتوری پروژه جدید منتقل میشود.
-Look at the generated _Cargo.toml_ file:
-
-
+فایل _Cargo.toml_ تولیدشده را مشاهده کنید:
Filename: Cargo.toml
@@ -44,8 +23,7 @@ cd ../../..
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml}}
```
-As you saw in Chapter 1, `cargo new` generates a “Hello, world!” program for
-you. Check out the _src/main.rs_ file:
+همانطور که در فصل 1 دیدید، `cargo new` یک برنامه "Hello, world!" برای شما تولید میکند. فایل _src/main.rs_ را بررسی کنید:
Filename: src/main.rs
@@ -53,27 +31,21 @@ you. Check out the _src/main.rs_ file:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs}}
```
-Now let’s compile this “Hello, world!” program and run it in the same step
-using the `cargo run` command:
+حالا این برنامه "Hello, world!" را کامپایل کرده و در همان مرحله با استفاده از دستور `cargo run` اجرا کنید:
```console
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt}}
```
-The `run` command comes in handy when you need to rapidly iterate on a project,
-as we’ll do in this game, quickly testing each iteration before moving on to
-the next one.
+دستور `run` زمانی که نیاز دارید به سرعت روی یک پروژه تکرار کنید مفید است، همانطور که در این بازی انجام خواهیم داد، و به سرعت هر مرحله را قبل از ادامه به مرحله بعدی آزمایش میکنیم.
-Reopen the _src/main.rs_ file. You’ll be writing all the code in this file.
+فایل _src/main.rs_ را دوباره باز کنید. شما تمام کد را در این فایل خواهید نوشت.
-## Processing a Guess
+## پردازش یک حدس
-The first part of the guessing game program will ask for user input, process
-that input, and check that the input is in the expected form. To start, we’ll
-allow the player to input a guess. Enter the code in Listing 2-1 into
-_src/main.rs_.
+اولین بخش از برنامه بازی حدس زدن از کاربر درخواست ورودی میکند، آن ورودی را پردازش میکند و بررسی میکند که ورودی در قالب مورد انتظار باشد. برای شروع، به بازیکن اجازه میدهیم یک حدس وارد کند. کد موجود در لیستینگ 2-1 را در فایل _src/main.rs_ وارد کنید.
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}}
@@ -81,208 +53,124 @@ _src/main.rs_.
-This code contains a lot of information, so let’s go over it line by line. To
-obtain user input and then print the result as output, we need to bring the
-`io` input/output library into scope. The `io` library comes from the standard
-library, known as `std`:
+این کد اطلاعات زیادی دارد، پس بیایید خط به خط آن را بررسی کنیم. برای گرفتن ورودی کاربر و سپس چاپ نتیجه بهعنوان خروجی، نیاز داریم که کتابخانه ورودی/خروجی `io` را به دامنه بیاوریم. کتابخانه `io` از کتابخانه استاندارد که با نام `std` شناخته میشود، میآید:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:io}}
```
-By default, Rust has a set of items defined in the standard library that it
-brings into the scope of every program. This set is called the _prelude_, and
-you can see everything in it [in the standard library documentation][prelude].
+بهطور پیشفرض، Rust مجموعهای از آیتمها را که در کتابخانه استاندارد تعریف شدهاند به دامنه هر برنامه وارد میکند. این مجموعه _prelude_ نامیده میشود و میتوانید همه چیز در آن را [در مستندات کتابخانه استاندارد][prelude] ببینید.
-If a type you want to use isn’t in the prelude, you have to bring that type
-into scope explicitly with a `use` statement. Using the `std::io` library
-provides you with a number of useful features, including the ability to accept
-user input.
+اگر نوعی که میخواهید استفاده کنید در prelude نباشد، باید آن نوع را بهطور صریح با یک دستور `use` به دامنه بیاورید. استفاده از کتابخانه `std::io` به شما ویژگیهای مفیدی مانند امکان پذیرش ورودی کاربر میدهد.
-As you saw in Chapter 1, the `main` function is the entry point into the
-program:
+همانطور که در فصل 1 دیدید، تابع `main` نقطه ورود به برنامه است:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:main}}
```
-The `fn` syntax declares a new function; the parentheses, `()`, indicate there
-are no parameters; and the curly bracket, `{`, starts the body of the function.
+نحو `fn` یک تابع جدید را اعلام میکند؛ پرانتزها `()` نشان میدهند که هیچ پارامتری وجود ندارد و کروشه باز `{` بدنه تابع را شروع میکند.
-As you also learned in Chapter 1, `println!` is a macro that prints a string to
-the screen:
+همچنین در فصل 1 آموختید که `println!` یک ماکرو است که یک رشته را به صفحه چاپ میکند:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}}
```
-This code is printing a prompt stating what the game is and requesting input
-from the user.
+این کد یک پیغام اعلام میکند که بازی چیست و از کاربر درخواست ورودی میکند.
-### Storing Values with Variables
+### ذخیره مقادیر با متغیرها
-Next, we’ll create a _variable_ to store the user input, like this:
+سپس، یک _متغیر_ ایجاد میکنیم تا ورودی کاربر را ذخیره کند، مانند این:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:string}}
```
-Now the program is getting interesting! There’s a lot going on in this little
-line. We use the `let` statement to create the variable. Here’s another example:
+حالا برنامه جالبتر میشود! در این خط کوچک چیزهای زیادی در حال اتفاق است. ما از دستور `let` برای ایجاد متغیر استفاده میکنیم. در اینجا یک مثال دیگر آورده شده است:
```rust,ignore
let apples = 5;
```
-This line creates a new variable named `apples` and binds it to the value 5. In
-Rust, variables are immutable by default, meaning once we give the variable a
-value, the value won’t change. We’ll be discussing this concept in detail in
-the [“Variables and Mutability”][variables-and-mutability]
-section in Chapter 3. To make a variable mutable, we add `mut` before the
-variable name:
+این خط یک متغیر جدید به نام `apples` ایجاد میکند و آن را به مقدار 5 متصل میکند. در Rust، متغیرها بهطور پیشفرض غیرقابلتغییر هستند، به این معنا که پس از اختصاص مقدار به متغیر، مقدار تغییر نخواهد کرد. این مفهوم را بهطور مفصل در بخش [“متغیرها و تغییرپذیری”][variables-and-mutability] در فصل 3 بررسی خواهیم کرد. برای متغیری که تغییرپذیر باشد، `mut` را قبل از نام متغیر اضافه میکنیم:
```rust,ignore
let apples = 5; // immutable
let mut bananas = 5; // mutable
```
-> Note: The `//` syntax starts a comment that continues until the end of the
-> line. Rust ignores everything in comments. We’ll discuss comments in more
-> detail in [Chapter 3][comments].
+> نکته: نحو `//` یک نظر (comment) را آغاز میکند که تا انتهای خط ادامه دارد. Rust همه چیز در نظرات را نادیده میگیرد. نظرات را در [فصل 3][comments] با جزئیات بیشتری بررسی خواهیم کرد.
-Returning to the guessing game program, you now know that `let mut guess` will
-introduce a mutable variable named `guess`. The equal sign (`=`) tells Rust we
-want to bind something to the variable now. On the right of the equal sign is
-the value that `guess` is bound to, which is the result of calling
-`String::new`, a function that returns a new instance of a `String`.
-[`String`][string] is a string type provided by the standard
-library that is a growable, UTF-8 encoded bit of text.
+بازگشت به برنامه بازی حدس زدن: اکنون میدانید که `let mut guess` یک متغیر تغییرپذیر به نام `guess` معرفی میکند. علامت مساوی (`=`) به Rust میگوید که میخواهیم چیزی را به این متغیر متصل کنیم. در سمت راست علامت مساوی، مقداری قرار دارد که `guess` به آن متصل میشود، که نتیجه فراخوانی `String::new` است، یک تابع که یک نمونه جدید از نوع `String` بازمیگرداند. [`String`][string] یک نوع رشتهای ارائهشده توسط کتابخانه استاندارد است که بخشی از متن قابل رشد و با رمزگذاری UTF-8 است.
-The `::` syntax in the `::new` line indicates that `new` is an associated
-function of the `String` type. An _associated function_ is a function that’s
-implemented on a type, in this case `String`. This `new` function creates a
-new, empty string. You’ll find a `new` function on many types because it’s a
-common name for a function that makes a new value of some kind.
+نحو `::` در خط `::new` نشان میدهد که `new` یک تابع مرتبط با نوع `String` است. یک _تابع مرتبط_ تابعی است که روی یک نوع پیادهسازی شده است، در اینجا `String`. این تابع `new` یک رشته جدید و خالی ایجاد میکند. شما در بسیاری از انواع یک تابع `new` پیدا خواهید کرد، زیرا این نام معمولاً برای تابعی که یک مقدار جدید از یک نوع خاص ایجاد میکند استفاده میشود.
-In full, the `let mut guess = String::new();` line has created a mutable
-variable that is currently bound to a new, empty instance of a `String`. Whew!
+در مجموع، خط `let mut guess = String::new();` یک متغیر تغییرپذیر ایجاد کرده است که در حال حاضر به یک نمونه جدید و خالی از `String` متصل شده است. خوب!
-### Receiving User Input
+### دریافت ورودی کاربر
-Recall that we included the input/output functionality from the standard
-library with `use std::io;` on the first line of the program. Now we’ll call
-the `stdin` function from the `io` module, which will allow us to handle user
-input:
+به یاد آورید که با `use std::io;` در اولین خط برنامه، قابلیت ورودی/خروجی را از کتابخانه استاندارد اضافه کردیم. اکنون تابع `stdin` را از ماژول `io` فراخوانی میکنیم که به ما امکان مدیریت ورودی کاربر را میدهد:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:read}}
```
-If we hadn’t imported the `io` module with `use std::io;` at the beginning of
-the program, we could still use the function by writing this function call as
-`std::io::stdin`. The `stdin` function returns an instance of
-[`std::io::Stdin`][iostdin], which is a type that represents a
-handle to the standard input for your terminal.
+اگر ماژول `io` را با دستور `use std::io;`
+در ابتدای برنامه وارد نکرده بودیم، همچنان
+میتوانستیم از این تابع استفاده کنیم، به این شکل
+که آن را بهصورت `std::io::stdin` فراخوانی کنیم.
+تابع `stdin` نمونهای از [`std::io::Stdin`][iostdin]
+بازمیگرداند، که نوعی است برای نمایش یک
+دسته (handle) به ورودی استاندارد ترمینال شما.
-Next, the line `.read_line(&mut guess)` calls the [`read_line`][read_line] method on the standard input handle to get input from the user.
-We’re also passing `&mut guess` as the argument to `read_line` to tell it what
-string to store the user input in. The full job of `read_line` is to take
-whatever the user types into standard input and append that into a string
-(without overwriting its contents), so we therefore pass that string as an
-argument. The string argument needs to be mutable so the method can change the
-string’s content.
+در خط بعدی، متد `.read_line(&mut guess)` را روی handle ورودی استاندارد فراخوانی میکنیم تا ورودی کاربر را دریافت کنیم. همچنین `&mut guess` را بهعنوان آرگومان به `read_line` ارسال میکنیم تا به آن بگوییم ورودی کاربر را در چه رشتهای ذخیره کند. وظیفه کامل `read_line` این است که هر چیزی را که کاربر در ورودی استاندارد تایپ میکند به رشتهای اضافه کند (بدون بازنویسی محتوای آن)، بنابراین این رشته را بهعنوان آرگومان ارسال میکنیم. آرگومان رشته باید تغییرپذیر باشد تا متد بتواند محتوای رشته را تغییر دهد.
-The `&` indicates that this argument is a _reference_, which gives you a way to
-let multiple parts of your code access one piece of data without needing to
-copy that data into memory multiple times. References are a complex feature,
-and one of Rust’s major advantages is how safe and easy it is to use
-references. You don’t need to know a lot of those details to finish this
-program. For now, all you need to know is that, like variables, references are
-immutable by default. Hence, you need to write `&mut guess` rather than
-`&guess` to make it mutable. (Chapter 4 will explain references more
-thoroughly.)
+علامت `&` نشان میدهد که این آرگومان یک _ارجاع_ است، که به شما راهی میدهد تا به چندین بخش از کد اجازه دهید به یک قطعه داده دسترسی داشته باشند بدون اینکه نیاز به کپی کردن آن داده در حافظه چندین بار داشته باشید. ارجاعات یک ویژگی پیچیده هستند و یکی از مزایای اصلی Rust این است که استفاده از ارجاعات ایمن و آسان است. نیازی نیست جزئیات زیادی درباره آن بدانید تا این برنامه را کامل کنید. فعلاً، تنها چیزی که باید بدانید این است که، مانند متغیرها، ارجاعات بهطور پیشفرض غیرقابل تغییر هستند. بنابراین، باید `&mut guess` بنویسید بهجای `&guess` تا آن را تغییرپذیر کنید. (فصل 4 ارجاعات را بهطور کامل توضیح خواهد داد.)
-
+### مدیریت خطای احتمالی با `Result`
-
-
-### Handling Potential Failure with `Result`
-
-We’re still working on this line of code. We’re now discussing a third line of
-text, but note that it’s still part of a single logical line of code. The next
-part is this method:
+ما همچنان روی همین خط کد کار میکنیم. اکنون در حال بحث درباره خط سوم هستیم، اما توجه داشته باشید که این هنوز بخشی از یک خط منطقی از کد است. قسمت بعدی این متد است:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:expect}}
```
-We could have written this code as:
+ما میتوانستیم این کد را به این صورت بنویسیم:
```rust,ignore
io::stdin().read_line(&mut guess).expect("Failed to read line");
```
-However, one long line is difficult to read, so it’s best to divide it. It’s
-often wise to introduce a newline and other whitespace to help break up long
-lines when you call a method with the `.method_name()` syntax. Now let’s
-discuss what this line does.
+با این حال، یک خط طولانی خواندن آن را دشوار میکند، بنابراین بهتر است آن را تقسیم کنیم. اغلب توصیه میشود یک خط جدید و فضای سفید معرفی کنید تا خطوط طولانی را هنگام فراخوانی متدی با نحو `.method_name()` تقسیم کنید. حالا بیایید ببینیم این خط چه میکند.
-As mentioned earlier, `read_line` puts whatever the user enters into the string
-we pass to it, but it also returns a `Result` value. [`Result`][result] is an [_enumeration_][enums], often called an _enum_,
-which is a type that can be in one of multiple possible states. We call each
-possible state a _variant_.
+همانطور که قبلاً ذکر شد، `read_line` هر چیزی که کاربر وارد میکند را در رشتهای که به آن ارسال میکنیم قرار میدهد، اما همچنین یک مقدار `Result` بازمیگرداند. [`Result`][result] یک [_enumeration_][enums] است که اغلب به عنوان _enum_ نامیده میشود و نوعی است که میتواند در یکی از چندین حالت ممکن باشد. ما هر حالت ممکن را یک _متغیر_ (variant) مینامیم.
-[Chapter 6][enums] will cover enums in more detail. The purpose
-of these `Result` types is to encode error-handling information.
+[فصل 6][enums] به جزئیات بیشتری در مورد enumها خواهد پرداخت. هدف از انواع `Result` رمزگذاری اطلاعات مدیریت خطا است.
-`Result`’s variants are `Ok` and `Err`. The `Ok` variant indicates the
-operation was successful, and it contains the successfully generated value.
-The `Err` variant means the operation failed, and it contains information
-about how or why the operation failed.
+متغیرهای `Result` شامل `Ok` و `Err` هستند. متغیر `Ok` نشان میدهد که عملیات موفقیتآمیز بوده و مقداری که با موفقیت تولید شده است را در خود دارد. متغیر `Err` به معنای این است که عملیات شکست خورده و اطلاعاتی درباره چگونگی یا دلیل شکست عملیات در خود دارد.
-Values of the `Result` type, like values of any type, have methods defined on
-them. An instance of `Result` has an [`expect` method][expect]
-that you can call. If this instance of `Result` is an `Err` value, `expect`
-will cause the program to crash and display the message that you passed as an
-argument to `expect`. If the `read_line` method returns an `Err`, it would
-likely be the result of an error coming from the underlying operating system.
-If this instance of `Result` is an `Ok` value, `expect` will take the return
-value that `Ok` is holding and return just that value to you so you can use it.
-In this case, that value is the number of bytes in the user’s input.
+مقادیر نوع `Result`، مانند مقادیر هر نوع دیگری، متدهایی تعریفشده بر روی خود دارند. یک نمونه از `Result` یک [متد `expect`][expect] دارد که میتوانید آن را فراخوانی کنید. اگر این نمونه از `Result` یک مقدار `Err` باشد، `expect` باعث میشود برنامه متوقف شده و پیغام خطایی که بهعنوان آرگومان به `expect` پاس دادهاید را نمایش دهد. اگر متد `read_line` یک `Err` بازگرداند، احتمالاً به دلیل خطایی از سیستمعامل زیربنایی است. اگر این نمونه از `Result` یک مقدار `Ok` باشد، `expect` مقدار بازگشتی که `Ok` در خود دارد را میگیرد و فقط آن مقدار را بازمیگرداند تا بتوانید از آن استفاده کنید. در این مورد، آن مقدار تعداد بایتهای ورودی کاربر است.
-If you don’t call `expect`, the program will compile, but you’ll get a warning:
+اگر `expect` را فراخوانی نکنید، برنامه کامپایل میشود، اما هشداری دریافت خواهید کرد:
```console
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt}}
```
-Rust warns that you haven’t used the `Result` value returned from `read_line`,
-indicating that the program hasn’t handled a possible error.
+Rust هشدار میدهد که از مقدار `Result` بازگشتی از `read_line` استفاده نکردهاید، که نشان میدهد برنامه یک خطای ممکن را مدیریت نکرده است.
-The right way to suppress the warning is to actually write error-handling code,
-but in our case we just want to crash this program when a problem occurs, so we
-can use `expect`. You’ll learn about recovering from errors in [Chapter
-9][recover].
+روش درست برای جلوگیری از هشدار این است که واقعاً کد مدیریت خطا بنویسید، اما در مورد ما فقط میخواهیم وقتی مشکلی پیش آمد این برنامه متوقف شود، بنابراین میتوانیم از `expect` استفاده کنیم. درباره بازیابی از خطاها در [فصل 9][recover] خواهید آموخت.
-### Printing Values with `println!` Placeholders
+### چاپ مقادیر با جاینگهدارهای `println!`
-Aside from the closing curly bracket, there’s only one more line to discuss in
-the code so far:
+علاوه بر کروشه بسته، فقط یک خط دیگر برای بحث در کدی که تاکنون نوشتهایم باقی مانده است:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print_guess}}
```
-This line prints the string that now contains the user’s input. The `{}` set of
-curly brackets is a placeholder: think of `{}` as little crab pincers that hold
-a value in place. When printing the value of a variable, the variable name can
-go inside the curly brackets. When printing the result of evaluating an
-expression, place empty curly brackets in the format string, then follow the
-format string with a comma-separated list of expressions to print in each empty
-curly bracket placeholder in the same order. Printing a variable and the result
-of an expression in one call to `println!` would look like this:
+این خط رشتهای را که اکنون ورودی کاربر را در خود دارد چاپ میکند. مجموعه `{}` از کروشههای باز و بسته یک جاینگهدار است: به `{}` بهعنوان پنجههای کوچک خرچنگی فکر کنید که یک مقدار را در جای خود نگه میدارند. هنگام چاپ مقدار یک متغیر، نام متغیر میتواند داخل کروشهها قرار گیرد. هنگام چاپ نتیجه ارزیابی یک عبارت، کروشههای باز و بسته خالی را در رشته فرمت قرار دهید، سپس رشته فرمت را با لیستی از عبارات جداشده با کاما دنبال کنید تا در هر جاینگهدار خالی به همان ترتیب چاپ شوند. چاپ یک متغیر و نتیجه یک عبارت در یک فراخوانی `println!` به این صورت خواهد بود:
```rust
let x = 5;
@@ -291,17 +179,11 @@ let y = 10;
println!("x = {x} and y + 2 = {}", y + 2);
```
-This code would print `x = 5 and y + 2 = 12`.
+این کد `x = 5 and y + 2 = 12` را چاپ میکند.
-### Testing the First Part
+### آزمایش بخش اول
-Let’s test the first part of the guessing game. Run it using `cargo run`:
-
-
+بیایید بخش اول بازی حدس زدن را آزمایش کنیم. با استفاده از دستور `cargo run` آن را اجرا کنید:
```console
$ cargo run
@@ -314,37 +196,17 @@ Please input your guess.
You guessed: 6
```
-At this point, the first part of the game is done: we’re getting input from the
-keyboard and then printing it.
+در این مرحله، بخش اول بازی تمام شده است: ما ورودی را از صفحهکلید میگیریم و سپس آن را چاپ میکنیم.
-## Generating a Secret Number
+## تولید یک عدد مخفی
-Next, we need to generate a secret number that the user will try to guess. The
-secret number should be different every time so the game is fun to play more
-than once. We’ll use a random number between 1 and 100 so the game isn’t too
-difficult. Rust doesn’t yet include random number functionality in its standard
-library. However, the Rust team does provide a [`rand` crate][randcrate] with
-said functionality.
+در مرحله بعد، باید یک عدد مخفی تولید کنیم که کاربر سعی خواهد کرد آن را حدس بزند. عدد مخفی باید هر بار متفاوت باشد تا بازی بارها قابل بازی و لذتبخش باشد. از یک عدد تصادفی بین 1 تا 100 استفاده میکنیم تا بازی خیلی سخت نباشد. Rust هنوز قابلیت تولید اعداد تصادفی را در کتابخانه استاندارد خود ندارد. با این حال، تیم Rust یک [crate `rand`][randcrate] با این قابلیت ارائه میدهد.
-### Using a Crate to Get More Functionality
+### استفاده از یک crate برای دسترسی به قابلیتهای بیشتر
-Remember that a crate is a collection of Rust source code files. The project
-we’ve been building is a _binary crate_, which is an executable. The `rand`
-crate is a _library crate_, which contains code that is intended to be used in
-other programs and can’t be executed on its own.
+به یاد داشته باشید که یک crate مجموعهای از فایلهای کد منبع Rust است. پروژهای که ما در حال ساخت آن هستیم یک _crate دودویی_ است که یک فایل اجرایی است. crate `rand` یک _crate کتابخانهای_ است که حاوی کدی است که قرار است در برنامههای دیگر استفاده شود و به تنهایی قابل اجرا نیست.
-Cargo’s coordination of external crates is where Cargo really shines. Before we
-can write code that uses `rand`, we need to modify the _Cargo.toml_ file to
-include the `rand` crate as a dependency. Open that file now and add the
-following line to the bottom, beneath the `[dependencies]` section header that
-Cargo created for you. Be sure to specify `rand` exactly as we have here, with
-this version number, or the code examples in this tutorial may not work:
-
-
+هماهنگی Cargo با جعبهها (crates)ی خارجی یکی از نقاط قوت آن است. قبل از اینکه بتوانیم کدی بنویسیم که از `rand` استفاده کند، باید فایل _Cargo.toml_ را تغییر دهیم تا crate `rand` را به عنوان وابستگی اضافه کنیم. اکنون آن فایل را باز کنید و خط زیر را به انتهای آن، زیر بخش `[dependencies]` که Cargo برای شما ایجاد کرده است، اضافه کنید. مطمئن شوید که `rand` را دقیقاً همانطور که در اینجا آمده است با این شماره نسخه مشخص کنید، وگرنه مثالهای کد در این آموزش ممکن است کار نکنند:
Filename: Cargo.toml
@@ -352,23 +214,11 @@ this version number, or the code examples in this tutorial may not work:
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml:8:}}
```
-In the _Cargo.toml_ file, everything that follows a header is part of that
-section that continues until another section starts. In `[dependencies]` you
-tell Cargo which external crates your project depends on and which versions of
-those crates you require. In this case, we specify the `rand` crate with the
-semantic version specifier `0.8.5`. Cargo understands [Semantic
-Versioning][semver] (sometimes called _SemVer_), which is a
-standard for writing version numbers. The specifier `0.8.5` is actually
-shorthand for `^0.8.5`, which means any version that is at least 0.8.5 but
-below 0.9.0.
+در فایل _Cargo.toml_، هر چیزی که بعد از یک سرآیند بیاید بخشی از آن بخش است و تا زمانی که بخش دیگری شروع نشود ادامه مییابد. در `[dependencies]` به Cargo میگویید پروژه شما به کدام جعبهها (crates)ی خارجی وابسته است و کدام نسخه از آن جعبهها (crates) را نیاز دارید. در این مورد، ما crate `rand` را با مشخصکننده نسخه `0.8.5` مشخص میکنیم. Cargo [نسخهبندی معنایی][semver] (گاهی اوقات _SemVer_ نامیده میشود) را درک میکند، که یک استاندارد برای نوشتن شماره نسخهها است. مشخصکننده `0.8.5` در واقع مخفف `^0.8.5` است که به این معناست که هر نسخهای که حداقل 0.8.5 باشد ولی کمتر از 0.9.0 باشد.
-Cargo considers these versions to have public APIs compatible with version
-0.8.5, and this specification ensures you’ll get the latest patch release that
-will still compile with the code in this chapter. Any version 0.9.0 or greater
-is not guaranteed to have the same API as what the following examples use.
+Cargo این نسخهها را دارای API عمومی سازگار با نسخه 0.8.5 در نظر میگیرد و این مشخصه تضمین میکند که آخرین نسخه patch را دریافت خواهید کرد که همچنان با کد موجود در این فصل کامپایل میشود. هیچ تضمینی وجود ندارد که نسخه 0.9.0 یا بالاتر همان API را داشته باشد که مثالهای زیر استفاده میکنند.
-Now, without changing any of the code, let’s build the project, as shown in
-Listing 2-2.
+اکنون، بدون تغییر هیچ کدی، بیایید پروژه را بسازیم، همانطور که در لیستینگ 2-2 نشان داده شده است.
-
+
+
```console
$ cargo build
@@ -403,35 +254,15 @@ $ cargo build
-You may see different version numbers (but they will all be compatible with the
-code, thanks to SemVer!) and different lines (depending on the operating
-system), and the lines may be in a different order.
-
-When we include an external dependency, Cargo fetches the latest versions of
-everything that dependency needs from the _registry_, which is a copy of data
-from [Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem
-post their open source Rust projects for others to use.
+ممکن است نسخههای متفاوتی را ببینید (اما همه آنها با کد سازگار خواهند بود، به لطف SemVer!) و خطوط متفاوتی (بسته به سیستمعامل) داشته باشید، و این خطوط ممکن است به ترتیب متفاوتی ظاهر شوند.
-After updating the registry, Cargo checks the `[dependencies]` section and
-downloads any crates listed that aren’t already downloaded. In this case,
-although we only listed `rand` as a dependency, Cargo also grabbed other crates
-that `rand` depends on to work. After downloading the crates, Rust compiles
-them and then compiles the project with the dependencies available.
+وقتی یک وابستگی خارجی اضافه میکنیم، Cargo جدیدترین نسخههای هر چیزی که آن وابستگی نیاز دارد را از _رجیستری_ دریافت میکند، که یک کپی از دادههای [Crates.io][cratesio] است. Crates.io جایی است که افراد در اکوسیستم Rust پروژههای منبعباز Rust خود را برای استفاده دیگران ارسال میکنند.
-If you immediately run `cargo build` again without making any changes, you
-won’t get any output aside from the `Finished` line. Cargo knows it has already
-downloaded and compiled the dependencies, and you haven’t changed anything
-about them in your _Cargo.toml_ file. Cargo also knows that you haven’t changed
-anything about your code, so it doesn’t recompile that either. With nothing to
-do, it simply exits.
+پس از بهروزرسانی رجیستری، Cargo بخش `[dependencies]` را بررسی میکند و هر crateی را که در لیست نیست و هنوز دانلود نشده است دانلود میکند. در این مورد، اگرچه ما فقط `rand` را بهعنوان یک وابستگی لیست کردهایم، Cargo سایر جعبهها (crates)یی را که `rand` برای کارکردن به آنها وابسته است نیز دریافت کرده است. پس از دانلود جعبهها (crates)، Rust آنها را کامپایل میکند و سپس پروژه را با وابستگیهای موجود کامپایل میکند.
-If you open the _src/main.rs_ file, make a trivial change, and then save it and
-build again, you’ll only see two lines of output:
+اگر بلافاصله دوباره دستور `cargo build` را اجرا کنید بدون اینکه هیچ تغییری ایجاد کرده باشید، خروجیای بهجز خط `Finished` دریافت نخواهید کرد. Cargo میداند که قبلاً وابستگیها را دانلود و کامپایل کرده است، و شما هیچ تغییری در فایل _Cargo.toml_ خود ندادهاید. Cargo همچنین میداند که شما هیچ تغییری در کد خود ندادهاید، بنابراین آن را هم دوباره کامپایل نمیکند. وقتی کاری برای انجام دادن وجود ندارد، فقط خارج میشود.
-
+اگر فایل _src/main.rs_ را باز کنید، یک تغییر جزئی در آن ایجاد کنید، و سپس آن را ذخیره کرده و دوباره بسازید، فقط دو خط خروجی خواهید دید:
```console
$ cargo build
@@ -439,46 +270,17 @@ $ cargo build
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
```
-These lines show that Cargo only updates the build with your tiny change to the
-_src/main.rs_ file. Your dependencies haven’t changed, so Cargo knows it can
-reuse what it has already downloaded and compiled for those.
-
-#### Ensuring Reproducible Builds with the _Cargo.lock_ File
-
-Cargo has a mechanism that ensures you can rebuild the same artifact every time
-you or anyone else builds your code: Cargo will use only the versions of the
-dependencies you specified until you indicate otherwise. For example, say that
-next week version 0.8.6 of the `rand` crate comes out, and that version
-contains an important bug fix, but it also contains a regression that will
-break your code. To handle this, Rust creates the _Cargo.lock_ file the first
-time you run `cargo build`, so we now have this in the _guessing_game_
-directory.
-
-When you build a project for the first time, Cargo figures out all the versions
-of the dependencies that fit the criteria and then writes them to the
-_Cargo.lock_ file. When you build your project in the future, Cargo will see
-that the _Cargo.lock_ file exists and will use the versions specified there
-rather than doing all the work of figuring out versions again. This lets you
-have a reproducible build automatically. In other words, your project will
-remain at 0.8.5 until you explicitly upgrade, thanks to the _Cargo.lock_ file.
-Because the _Cargo.lock_ file is important for reproducible builds, it’s often
-checked into source control with the rest of the code in your project.
-
-#### Updating a Crate to Get a New Version
-
-When you _do_ want to update a crate, Cargo provides the command `update`,
-which will ignore the _Cargo.lock_ file and figure out all the latest versions
-that fit your specifications in _Cargo.toml_. Cargo will then write those
-versions to the _Cargo.lock_ file. In this case, Cargo will only look for
-versions greater than 0.8.5 and less than 0.9.0. If the `rand` crate has
-released the two new versions 0.8.6 and 0.9.0, you would see the following if
-you ran `cargo update`:
+این خطوط نشان میدهند که Cargo فقط با تغییر کوچک شما در فایل _src/main.rs_ بیلد را بهروزرسانی کرده است. وابستگیهای شما تغییری نکردهاند، بنابراین Cargo میداند که میتواند از آنچه قبلاً دانلود و کامپایل کرده است استفاده مجدد کند.
-
+#### اطمینان از بیلدهای قابل بازتولید با فایل _Cargo.lock_
+
+Cargo مکانیزمی دارد که اطمینان میدهد شما یا هر کس دیگری بتوانید هر بار که کد خود را بیلد میکنید، همان نتیجه را دریافت کنید: Cargo تنها از نسخههایی از وابستگیها که مشخص کردهاید استفاده میکند، مگر اینکه خلاف آن را اعلام کنید. برای مثال، فرض کنید هفته آینده نسخه 0.8.6 از crate `rand` منتشر میشود و آن نسخه شامل یک رفع باگ مهم است، اما همچنین شامل یک برگشت (regression) است که کد شما را خراب میکند. برای مدیریت این موضوع، Rust فایل _Cargo.lock_ را در اولین باری که `cargo build` را اجرا میکنید ایجاد میکند، بنابراین اکنون این فایل در دایرکتوری _guessing_game_ وجود دارد.
+
+وقتی برای اولین بار پروژهای را بیلد میکنید، Cargo همه نسخههای وابستگیهایی که با معیارها تطابق دارند را پیدا میکند و سپس آنها را به فایل _Cargo.lock_ مینویسد. وقتی در آینده پروژه خود را بیلد میکنید، Cargo میبیند که فایل _Cargo.lock_ وجود دارد و از نسخههای مشخصشده در آن استفاده میکند، به جای اینکه تمام کار پیدا کردن نسخهها را دوباره انجام دهد. این کار به شما اجازه میدهد که بهطور خودکار یک بیلد قابل بازتولید داشته باشید. به عبارت دیگر، پروژه شما در نسخه 0.8.5 باقی خواهد ماند تا زمانی که به صورت صریح آن را بهروزرسانی کنید، به لطف فایل _Cargo.lock_. چون فایل _Cargo.lock_ برای بیلدهای قابل بازتولید مهم است، معمولاً همراه با بقیه کد پروژه در سیستم کنترل نسخه (source control) ذخیره میشود.
+
+#### بهروزرسانی یک crate برای دریافت نسخه جدید
+
+وقتی _میخواهید_ یک crate را بهروزرسانی کنید، Cargo دستور `update` را فراهم میکند که فایل _Cargo.lock_ را نادیده میگیرد و تمام نسخههای جدیدی که با مشخصات شما در فایل _Cargo.toml_ سازگار هستند را پیدا میکند. سپس Cargo آن نسخهها را به فایل _Cargo.lock_ مینویسد. در این مورد، Cargo تنها به دنبال نسخههایی میگردد که بالاتر از 0.8.5 و کمتر از 0.9.0 باشند. اگر crate `rand` دو نسخه جدید 0.8.6 و 0.9.0 را منتشر کرده باشد، با اجرای `cargo update` چنین چیزی را خواهید دید:
```console
$ cargo update
@@ -487,32 +289,23 @@ $ cargo update
Updating rand v0.8.5 -> v0.8.6 (available: v0.9.0)
```
-Cargo ignores the 0.9.0 release. At this point, you would also notice a change
-in your _Cargo.lock_ file noting that the version of the `rand` crate you are
-now using is 0.8.6. To use `rand` version 0.9.0 or any version in the 0.9._x_
-series, you’d have to update the _Cargo.toml_ file to look like this instead:
+Cargo نسخه 0.9.0 را نادیده میگیرد. در این مرحله، شما همچنین تغییری در فایل _Cargo.lock_ مشاهده میکنید که نشان میدهد نسخه crate `rand` که اکنون استفاده میکنید 0.8.6 است. برای استفاده از نسخه 0.9.0 `rand` یا هر نسخهای در سری 0.9._x_، باید فایل _Cargo.toml_ را به این شکل تغییر دهید:
```toml
[dependencies]
rand = "0.9.0"
```
-The next time you run `cargo build`, Cargo will update the registry of crates
-available and reevaluate your `rand` requirements according to the new version
-you have specified.
+دفعه بعد که `cargo build` را اجرا کنید، Cargo رجیستری جعبهها (crates)ی موجود را بهروزرسانی میکند و نیازمندیهای شما برای `rand` را بر اساس نسخه جدیدی که مشخص کردهاید ارزیابی میکند.
+
+چیزهای بیشتری درباره [Cargo][doccargo] و [اکوسیستم آن][doccratesio] وجود دارد که در فصل 14 بحث خواهیم کرد، اما فعلاً این تمام چیزی است که باید بدانید. Cargo استفاده از کتابخانهها را بسیار آسان میکند، بنابراین Rustaceans میتوانند پروژههای کوچکتری بنویسند که از تعدادی بسته تشکیل شدهاند.
-There’s a lot more to say about [Cargo][doccargo] and [its
-ecosystem][doccratesio], which we’ll discuss in Chapter 14, but
-for now, that’s all you need to know. Cargo makes it very easy to reuse
-libraries, so Rustaceans are able to write smaller projects that are assembled
-from a number of packages.
+### تولید یک عدد تصادفی
-### Generating a Random Number
+بیایید استفاده از `rand` را برای تولید یک عدد برای حدس زدن شروع کنیم. مرحله بعد بهروزرسانی فایل _src/main.rs_ است، همانطور که در لیستینگ 2-3 نشان داده شده است.
-Let’s start using `rand` to generate a number to guess. The next step is to
-update _src/main.rs_, as shown in Listing 2-3.
+
-
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:all}}
@@ -520,35 +313,15 @@ update _src/main.rs_, as shown in Listing 2-3.
-First we add the line `use rand::Rng;`. The `Rng` trait defines methods that
-random number generators implement, and this trait must be in scope for us to
-use those methods. Chapter 10 will cover traits in detail.
-
-Next, we’re adding two lines in the middle. In the first line, we call the
-`rand::thread_rng` function that gives us the particular random number
-generator we’re going to use: one that is local to the current thread of
-execution and is seeded by the operating system. Then we call the `gen_range`
-method on the random number generator. This method is defined by the `Rng`
-trait that we brought into scope with the `use rand::Rng;` statement. The
-`gen_range` method takes a range expression as an argument and generates a
-random number in the range. The kind of range expression we’re using here takes
-the form `start..=end` and is inclusive on the lower and upper bounds, so we
-need to specify `1..=100` to request a number between 1 and 100.
-
-> Note: You won’t just know which traits to use and which methods and functions
-> to call from a crate, so each crate has documentation with instructions for
-> using it. Another neat feature of Cargo is that running the `cargo doc
-> --open` command will build documentation provided by all your dependencies
-> locally and open it in your browser. If you’re interested in other
-> functionality in the `rand` crate, for example, run `cargo doc --open` and
-> click `rand` in the sidebar on the left.
-
-The second new line prints the secret number. This is useful while we’re
-developing the program to be able to test it, but we’ll delete it from the
-final version. It’s not much of a game if the program prints the answer as soon
-as it starts!
-
-Try running the program a few times:
+ابتدا خط `use rand::Rng;` را اضافه میکنیم. صفت (trait) `Rng` متدهایی را تعریف میکند که تولیدکنندگان اعداد تصادفی پیادهسازی میکنند، و این صفت باید در دامنه باشد تا بتوانیم از آن متدها استفاده کنیم. فصل 10 بهطور مفصل به بررسی صفتها خواهد پرداخت.
+
+سپس دو خط در وسط اضافه میکنیم. در خط اول، تابع `rand::thread_rng` را فراخوانی میکنیم که تولیدکننده اعداد تصادفی خاصی را که میخواهیم استفاده کنیم به ما میدهد: تولیدکنندهای که محلی برای نخ فعلی اجرا است و توسط سیستمعامل seed میشود. سپس متد `gen_range` را روی تولیدکننده اعداد تصادفی فراخوانی میکنیم. این متد توسط صفت `Rng` که با دستور `use rand::Rng;` وارد دامنه کردیم، تعریف شده است. متد `gen_range` یک عبارت بازهای را بهعنوان آرگومان میگیرد و یک عدد تصادفی در آن بازه تولید میکند. نوع عبارت بازهای که در اینجا استفاده میکنیم به صورت `start..=end` است و شامل حد پایین و بالا میشود، بنابراین باید `1..=100` را مشخص کنیم تا عددی بین 1 تا 100 درخواست کنیم.
+
+> نکته: شما نمیتوانید بهطور پیشفرض بدانید که کدام صفتها را باید استفاده کنید و کدام متدها و توابع را از یک crate فراخوانی کنید، بنابراین هر crate دارای مستنداتی با دستورالعملهایی برای استفاده از آن است. ویژگی جالب دیگر Cargo این است که اجرای دستور `cargo doc --open` مستندات ارائهشده توسط تمام وابستگیهای شما را بهصورت محلی میسازد و در مرورگر شما باز میکند. اگر به دیگر قابلیتهای crate `rand` علاقهمند هستید، برای مثال دستور `cargo doc --open` را اجرا کنید و روی `rand` در نوار کناری سمت چپ کلیک کنید.
+
+خط جدید دوم عدد مخفی را چاپ میکند. این خط در حین توسعه برنامه برای آزمایش آن مفید است، اما در نسخه نهایی آن را حذف خواهیم کرد. اگر برنامه به محض شروع پاسخ را چاپ کند، خیلی بازی هیجانانگیزی نخواهد بود!
+
+برنامه را چند بار اجرا کنید:
expression to decide what to do next based on
-which variant of `Ordering` was returned from the call to `cmp` with the values
-in `guess` and `secret_number`.
-
-A `match` expression is made up of _arms_. An arm consists of a _pattern_ to
-match against, and the code that should be run if the value given to `match`
-fits that arm’s pattern. Rust takes the value given to `match` and looks
-through each arm’s pattern in turn. Patterns and the `match` construct are
-powerful Rust features: they let you express a variety of situations your code
-might encounter and they make sure you handle them all. These features will be
-covered in detail in Chapter 6 and Chapter 19, respectively.
-
-Let’s walk through an example with the `match` expression we use here. Say that
-the user has guessed 50 and the randomly generated secret number this time is
-38.
-
-When the code compares 50 to 38, the `cmp` method will return
-`Ordering::Greater` because 50 is greater than 38. The `match` expression gets
-the `Ordering::Greater` value and starts checking each arm’s pattern. It looks
-at the first arm’s pattern, `Ordering::Less`, and sees that the value
-`Ordering::Greater` does not match `Ordering::Less`, so it ignores the code in
-that arm and moves to the next arm. The next arm’s pattern is
-`Ordering::Greater`, which _does_ match `Ordering::Greater`! The associated
-code in that arm will execute and print `Too big!` to the screen. The `match`
-expression ends after the first successful match, so it won’t look at the last
-arm in this scenario.
-
-However, the code in Listing 2-4 won’t compile yet. Let’s try it:
+ابتدا یک دستور `use` دیگر اضافه میکنیم تا نوعی به نام `std::cmp::Ordering` را از کتابخانه استاندارد وارد دامنه کنیم. نوع `Ordering` یک enum دیگر است و دارای متغیرهای `Less`، `Greater` و `Equal` است. اینها سه نتیجه ممکن هنگام مقایسه دو مقدار هستند.
+
+سپس پنج خط جدید در انتهای کد اضافه میکنیم که از نوع `Ordering` استفاده میکنند. متد `cmp` دو مقدار را مقایسه میکند و میتواند روی هر چیزی که قابل مقایسه باشد فراخوانی شود. این متد یک ارجاع به مقداری که میخواهید مقایسه کنید میگیرد: در اینجا مقایسه بین `guess` و `secret_number` است. سپس یکی از متغیرهای enum `Ordering` که با دستور `use` به دامنه آوردیم را بازمیگرداند. از یک عبارت [`match`][match] برای تصمیمگیری در مورد اقدام بعدی بر اساس اینکه کدام متغیر `Ordering` از فراخوانی `cmp` با مقادیر `guess` و `secret_number` بازگشته است استفاده میکنیم.
+
+یک عبارت `match` از _شاخهها (arms)_ تشکیل شده است. یک شاخه شامل یک _الگو_ برای مطابقت است و کدی که باید اجرا شود اگر مقدار دادهشده به `match` با الگوی آن شاخه تطابق داشته باشد. Rust مقدار دادهشده به `match` را گرفته و به ترتیب هر الگوی شاخه را بررسی میکند. الگوها و سازه `match` از ویژگیهای قدرتمند Rust هستند: آنها به شما اجازه میدهند موقعیتهای مختلفی که کد شما ممکن است با آنها روبرو شود را بیان کنید و اطمینان حاصل کنید که همه آنها را مدیریت میکنید. این ویژگیها بهطور مفصل در فصل 6 و فصل 19 پوشش داده خواهند شد.
+
+بیایید با یک مثال از عبارت `match` که در اینجا استفاده کردهایم، آن را بررسی کنیم. فرض کنید کاربر 50 را حدس زده و عدد مخفی که این بار بهطور تصادفی تولید شده 38 است.
+
+وقتی کد 50 را با 38 مقایسه میکند، متد `cmp` مقدار `Ordering::Greater` را بازمیگرداند زیرا 50 بزرگتر از 38 است. عبارت `match` مقدار `Ordering::Greater` را گرفته و شروع به بررسی هر الگوی شاخه میکند. به الگوی شاخه اول، `Ordering::Less` نگاه میکند و میبیند که مقدار `Ordering::Greater` با `Ordering::Less` تطابق ندارد، بنابراین کد موجود در آن شاخه را نادیده میگیرد و به شاخه بعدی میرود. الگوی شاخه بعدی `Ordering::Greater` است که با `Ordering::Greater` تطابق دارد! کد مرتبط با آن شاخه اجرا شده و عبارت `Too big!` را روی صفحه چاپ میکند. عبارت `match` پس از اولین تطابق موفقیتآمیز پایان مییابد، بنابراین در این سناریو به شاخه آخر نگاه نمیکند.
+
+با این حال، کد موجود در لیستینگ 2-4 هنوز کامپایل نخواهد شد. بیایید آن را امتحان کنیم:
, but for now, know that this feature is
-often used when you want to convert a value from one type to another type.
-
-We bind this new variable to the expression `guess.trim().parse()`. The `guess`
-in the expression refers to the original `guess` variable that contained the
-input as a string. The `trim` method on a `String` instance will eliminate any
-whitespace at the beginning and end, which we must do before we can convert the
-string to a `u32`, which can only contain numerical data. The user must press
-enter to satisfy `read_line` and input their guess, which adds a
-newline character to the string. For example, if the user types 5 and
-presses enter, `guess` looks like this: `5\n`. The `\n` represents
-“newline.” (On Windows, pressing enter results in a carriage return
-and a newline, `\r\n`.) The `trim` method eliminates `\n` or `\r\n`, resulting
-in just `5`.
-
-The [`parse` method on strings][parse] converts a string to
-another type. Here, we use it to convert from a string to a number. We need to
-tell Rust the exact number type we want by using `let guess: u32`. The colon
-(`:`) after `guess` tells Rust we’ll annotate the variable’s type. Rust has a
-few built-in number types; the `u32` seen here is an unsigned, 32-bit integer.
-It’s a good default choice for a small positive number. You’ll learn about
-other number types in [Chapter 3][integers].
-
-Additionally, the `u32` annotation in this example program and the comparison
-with `secret_number` means Rust will infer that `secret_number` should be a
-`u32` as well. So now the comparison will be between two values of the same
-type!
-
-The `parse` method will only work on characters that can logically be converted
-into numbers and so can easily cause errors. If, for example, the string
-contained `A👍%`, there would be no way to convert that to a number. Because it
-might fail, the `parse` method returns a `Result` type, much as the `read_line`
-method does (discussed earlier in [“Handling Potential Failure with
-`Result`”](#handling-potential-failure-with-result)). We’ll treat
-this `Result` the same way by using the `expect` method again. If `parse`
-returns an `Err` `Result` variant because it couldn’t create a number from the
-string, the `expect` call will crash the game and print the message we give it.
-If `parse` can successfully convert the string to a number, it will return the
-`Ok` variant of `Result`, and `expect` will return the number that we want from
-the `Ok` value.
-
-Let’s run the program now:
+ما یک متغیر به نام `guess` ایجاد میکنیم. اما صبر کنید، آیا برنامه قبلاً یک متغیر به نام `guess` ندارد؟ دارد، اما Rust بهطور مفیدی به ما اجازه میدهد مقدار قبلی `guess` را با یک مقدار جدید پوشش دهیم. _پوششدهی_ به ما اجازه میدهد که از نام متغیر `guess` دوباره استفاده کنیم، بهجای اینکه مجبور شویم دو متغیر منحصربهفرد مانند `guess_str` و `guess` ایجاد کنیم. این موضوع را در [فصل 3][shadowing] با جزئیات بیشتری بررسی خواهیم کرد، اما فعلاً بدانید که این ویژگی اغلب زمانی استفاده میشود که بخواهید مقدار را از یک نوع به نوع دیگری تبدیل کنید.
+
+ما این متغیر جدید را به عبارت `guess.trim().parse()` متصل میکنیم. `guess` در این عبارت به متغیر اصلی `guess` که ورودی بهصورت رشتهای بود اشاره دارد. متد `trim` روی یک نمونه `String` تمام فضای سفید در ابتدا و انتهای رشته را حذف میکند، که قبل از تبدیل رشته به `u32` که فقط میتواند دادههای عددی داشته باشد، باید این کار را انجام دهیم. کاربر باید کلید enter را فشار دهد تا `read_line` مقدار ورودی را دریافت کند، که یک کاراکتر newline به رشته اضافه میکند. برای مثال، اگر کاربر کلید 5 را تایپ کند و enter را فشار دهد، `guess` به این شکل خواهد بود: `5\n`. `\n` نشاندهنده "خط جدید" است. (در ویندوز، فشار دادن enter منجر به carriage return و newline، یعنی `\r\n` میشود.) متد `trim` `\n` یا `\r\n` را حذف میکند و نتیجه فقط `5` است.
+
+متد [`parse` روی رشتهها][parse] یک رشته را به نوع دیگری تبدیل میکند. اینجا از آن برای تبدیل یک رشته به عدد استفاده میکنیم. باید به Rust نوع عدد دقیق موردنظرمان را با استفاده از `let guess: u32` بگوییم. علامت `:` بعد از `guess` به Rust میگوید که نوع متغیر را مشخص خواهیم کرد. Rust چند نوع عدد داخلی دارد؛ `u32` که اینجا دیده میشود، یک عدد صحیح 32 بیتی بدون علامت است. این یک انتخاب پیشفرض خوب برای یک عدد مثبت کوچک است. درباره دیگر انواع عددی در [فصل 3][integers] خواهید آموخت.
+
+علاوه بر این، حاشیهنویسی `u32` در این برنامه نمونه و مقایسه با `secret_number` به این معناست که Rust استنباط خواهد کرد که `secret_number` نیز باید یک `u32` باشد. بنابراین اکنون مقایسه بین دو مقدار از یک نوع خواهد بود!
+
+متد `parse` فقط روی کاراکترهایی کار میکند که منطقی بتوان آنها را به اعداد تبدیل کرد و بنابراین بهراحتی میتواند باعث خطا شود. برای مثال، اگر رشتهای شامل `A👍%` باشد، هیچ راهی برای تبدیل آن به عدد وجود ندارد. چون ممکن است این عملیات شکست بخورد، متد `parse` نوع `Result` را برمیگرداند، دقیقاً مانند متد `read_line` (که قبلاً در [“مدیریت خطای احتمالی با `Result`”](#handling-potential-failure-with-result) بحث کردیم). ما این `Result` را همانطور که قبلاً انجام دادیم با استفاده مجدد از متد `expect` مدیریت خواهیم کرد. اگر `parse` متغیر `Err` از نوع `Result` را برگرداند زیرا نتوانست یک عدد از رشته ایجاد کند، فراخوانی `expect` بازی را متوقف کرده و پیام مشخصشده را چاپ میکند. اگر `parse` بتواند با موفقیت رشته را به عدد تبدیل کند، متغیر `Ok` از نوع `Result` را برمیگرداند و `expect` عدد مورد نظر را از مقدار `Ok` بازمیگرداند.
+
: if
-the user enters a non-number answer, the program will crash. We can take
-advantage of that to allow the user to quit, as shown here:
+کاربر همیشه میتواند برنامه را با استفاده از میانبر صفحهکلید ctrl-c متوقف کند. اما راه دیگری برای فرار از این هیولای سیریناپذیر وجود دارد، همانطور که در بحث `parse` در [“مقایسه حدس با عدد مخفی”](#comparing-the-guess-to-the-secret-number) ذکر شد: اگر کاربر پاسخی غیرعددی وارد کند، برنامه متوقف میشود. میتوانیم از این موضوع استفاده کنیم تا به کاربر اجازه دهیم خارج شود، همانطور که در اینجا نشان داده شده است:
.
+> زبان Rust مجموعهای از _کلمات کلیدی_ دارد که فقط برای استفاده توسط زبان رزرو شدهاند، همانند سایر زبانها. به خاطر داشته باشید که نمیتوانید از این کلمات بهعنوان نام متغیرها یا توابع استفاده کنید. اکثر کلمات کلیدی معانی خاصی دارند و شما از آنها برای انجام وظایف مختلف در برنامههای Rust خود استفاده خواهید کرد؛ تعدادی از آنها در حال حاضر هیچ عملکردی ندارند اما برای قابلیتهایی که ممکن است در آینده به Rust اضافه شوند رزرو شدهاند. شما میتوانید لیست کلمات کلیدی را در [ضمیمه الف][appendix_a] پیدا کنید.
[appendix_a]: appendix-01-keywords.md
diff --git a/src/ch03-01-variables-and-mutability.md b/src/ch03-01-variables-and-mutability.md
index e5d599c030..8564e4287a 100644
--- a/src/ch03-01-variables-and-mutability.md
+++ b/src/ch03-01-variables-and-mutability.md
@@ -1,134 +1,78 @@
-## Variables and Mutability
+## متغیرها و تغییرپذیری
-As mentioned in the [“Storing Values with
-Variables”][storing-values-with-variables] section, by default,
-variables are immutable. This is one of many nudges Rust gives you to write
-your code in a way that takes advantage of the safety and easy concurrency that
-Rust offers. However, you still have the option to make your variables mutable.
-Let’s explore how and why Rust encourages you to favor immutability and why
-sometimes you might want to opt out.
+همانطور که در بخش [“ذخیره مقادیر با استفاده از متغیرها”][storing-values-with-variables] ذکر شد، به طور پیشفرض متغیرها در Rust غیرقابلتغییر هستند. این یکی از راههایی است که Rust شما را به نوشتن کدی که از ایمنی و همزمانی آسان ارائهشده توسط این زبان بهره میبرد، تشویق میکند. با این حال، شما همچنان گزینهای دارید تا متغیرهای خود را قابلتغییر کنید. بیایید بررسی کنیم که چگونه و چرا Rust شما را تشویق به استفاده از غیرقابلتغییر بودن میکند و چرا ممکن است گاهی بخواهید این حالت را تغییر دهید.
-When a variable is immutable, once a value is bound to a name, you can’t change
-that value. To illustrate this, generate a new project called _variables_ in
-your _projects_ directory by using `cargo new variables`.
+وقتی یک متغیر غیرقابلتغییر است، وقتی مقداری به یک نام متصل شد، نمیتوانید آن مقدار را تغییر دهید. برای نشان دادن این موضوع، یک پروژه جدید به نام _variables_ در دایرکتوری _projects_ خود ایجاد کنید با استفاده از دستور `cargo new variables`.
-Then, in your new _variables_ directory, open _src/main.rs_ and replace its
-code with the following code, which won’t compile just yet:
+سپس، در دایرکتوری جدید _variables_ خود، فایل _src/main.rs_ را باز کنید و کد آن را با کد زیر جایگزین کنید، که هنوز کامپایل نخواهد شد:
-Filename: src/main.rs
+تام فایل: src/main.rs
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/src/main.rs}}
```
-Save and run the program using `cargo run`. You should receive an error message
-regarding an immutability error, as shown in this output:
+
+فایل را ذخیره کنید و برنامه را با استفاده از `cargo run` اجرا کنید. باید یک پیام خطا در مورد غیرقابلتغییر بودن دریافت کنید، همانطور که در این خروجی نشان داده شده است:
+
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-01-variables-are-immutable/output.txt}}
```
-This example shows how the compiler helps you find errors in your programs.
-Compiler errors can be frustrating, but really they only mean your program
-isn’t safely doing what you want it to do yet; they do _not_ mean that you’re
-not a good programmer! Experienced Rustaceans still get compiler errors.
-
-You received the error message `` cannot assign twice to immutable variable `x` `` because you tried to assign a second value to the immutable `x` variable.
-
-It’s important that we get compile-time errors when we attempt to change a
-value that’s designated as immutable because this very situation can lead to
-bugs. If one part of our code operates on the assumption that a value will
-never change and another part of our code changes that value, it’s possible
-that the first part of the code won’t do what it was designed to do. The cause
-of this kind of bug can be difficult to track down after the fact, especially
-when the second piece of code changes the value only _sometimes_. The Rust
-compiler guarantees that when you state that a value won’t change, it really
-won’t change, so you don’t have to keep track of it yourself. Your code is thus
-easier to reason through.
-
-But mutability can be very useful, and can make code more convenient to write.
-Although variables are immutable by default, you can make them mutable by
-adding `mut` in front of the variable name as you did in [Chapter
-2][storing-values-with-variables]. Adding `mut` also conveys
-intent to future readers of the code by indicating that other parts of the code
-will be changing this variable’s value.
-
-For example, let’s change _src/main.rs_ to the following:
-Filename: src/main.rs
+این مثال نشان میدهد که چگونه کامپایلر به شما کمک میکند تا خطاها را در برنامههای خود پیدا کنید. خطاهای کامپایلر ممکن است ناامیدکننده باشند، اما در واقع به این معنا هستند که برنامه شما هنوز به طور ایمن کاری را که میخواهید انجام نمیدهد؛ این به هیچ وجه به این معنا نیست که شما برنامهنویس خوبی نیستید! حتی برنامهنویسان باتجربه Rust نیز همچنان خطاهای کامپایلر دریافت میکنند.
+
+شما پیام خطای `` cannot assign twice to immutable variable `x` `` را دریافت کردید زیرا سعی کردید مقدار دوم را به متغیر غیرقابلتغییر `x` تخصیص دهید.
+
+این بسیار مهم است که ما خطاهای زمان کامپایل را دریافت کنیم وقتی سعی میکنیم مقدار یک متغیر غیرقابلتغییر را تغییر دهیم زیرا این وضعیت میتواند به باگ منجر شود. اگر یک بخش از کد ما با این فرض عمل کند که یک مقدار هرگز تغییر نمیکند و بخش دیگری از کد آن مقدار را تغییر دهد، ممکن است بخش اول کد کاری که برای انجام آن طراحی شده بود را به درستی انجام ندهد. علت این نوع باگ میتواند بعد از وقوع به سختی قابلردیابی باشد، بهویژه وقتی که بخش دوم کد فقط _گاهی اوقات_ مقدار را تغییر میدهد. کامپایلر Rust تضمین میکند که وقتی بیان میکنید یک مقدار تغییر نخواهد کرد، واقعاً تغییر نخواهد کرد، بنابراین نیازی نیست که خودتان این موضوع را پیگیری کنید. به این ترتیب کد شما راحتتر قابلدرک خواهد بود.
+
+اما قابلیت تغییر میتواند بسیار مفید باشد و نوشتن کد را راحتتر کند. اگرچه متغیرها به طور پیشفرض غیرقابلتغییر هستند، میتوانید با اضافه کردن `mut` قبل از نام متغیر آنها را قابلتغییر کنید، همانطور که در [فصل ۲][storing-values-with-variables] انجام دادید. اضافه کردن `mut` همچنین به خوانندگان آینده کد نیت شما را نشان میدهد که قسمتهای دیگر کد مقدار این متغیر را تغییر خواهند داد.
+
+برای مثال، بیایید فایل _src/main.rs_ را به کد زیر تغییر دهیم:
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-02-adding-mut/src/main.rs}}
```
-When we run the program now, we get this:
+
+وقتی اکنون برنامه را اجرا میکنیم، این خروجی را دریافت میکنیم:
+
+
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-02-adding-mut/output.txt}}
```
-We’re allowed to change the value bound to `x` from `5` to `6` when `mut` is
-used. Ultimately, deciding whether to use mutability or not is up to you and
-depends on what you think is clearest in that particular situation.
-### Constants
-Like immutable variables, _constants_ are values that are bound to a name and
-are not allowed to change, but there are a few differences between constants
-and variables.
+ما اجازه داریم مقدار مرتبط با `x` را از `5` به `6` تغییر دهیم وقتی که از `mut` استفاده شود. در نهایت، تصمیمگیری در مورد استفاده یا عدم استفاده از قابلیت تغییر به عهده شما است و به این بستگی دارد که در آن موقعیت خاص چه چیزی واضحتر به نظر میرسد.
+
+### ثابت ها
-First, you aren’t allowed to use `mut` with constants. Constants aren’t just
-immutable by default—they’re always immutable. You declare constants using the
-`const` keyword instead of the `let` keyword, and the type of the value _must_
-be annotated. We’ll cover types and type annotations in the next section,
-[“Data Types”][data-types], so don’t worry about the details
-right now. Just know that you must always annotate the type.
+مانند متغیرهای غیرقابلتغییر، _ثابتها_ مقادیری هستند که به یک نام متصل میشوند و اجازه تغییر ندارند، اما چند تفاوت بین ثابتها و متغیرها وجود دارد.
-Constants can be declared in any scope, including the global scope, which makes
-them useful for values that many parts of code need to know about.
+اول، شما نمیتوانید از `mut` با ثابتها استفاده کنید. ثابتها نه تنها به طور پیشفرض غیرقابلتغییر هستند، بلکه همیشه غیرقابلتغییر هستند. شما ثابتها را با استفاده از کلیدواژه `const` به جای کلیدواژه `let` تعریف میکنید و نوع مقدار _باید_ مشخص شود. ما در بخش بعدی [“انواع داده”][data-types] درباره انواع و حاشیهنویسی نوع صحبت خواهیم کرد، بنابراین نگران جزئیات آن در حال حاضر نباشید. فقط بدانید که همیشه باید نوع را مشخص کنید.
-The last difference is that constants may be set only to a constant expression,
-not the result of a value that could only be computed at runtime.
+ثابتها میتوانند در هر دامنهای، از جمله دامنهی جهانی، تعریف شوند، که این ویژگی آنها را برای مقادیری که بخشهای مختلف کد باید بدانند مفید میسازد.
-Here’s an example of a constant declaration:
+آخرین تفاوت این است که ثابتها فقط میتوانند به یک عبارت ثابت تنظیم شوند، نه نتیجهای که فقط میتواند در زمان اجرا محاسبه شود.
+
+در اینجا یک مثال از تعریف ثابت آورده شده است:
```rust
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
```
-The constant’s name is `THREE_HOURS_IN_SECONDS` and its value is set to the
-result of multiplying 60 (the number of seconds in a minute) by 60 (the number
-of minutes in an hour) by 3 (the number of hours we want to count in this
-program). Rust’s naming convention for constants is to use all uppercase with
-underscores between words. The compiler is able to evaluate a limited set of
-operations at compile time, which lets us choose to write out this value in a
-way that’s easier to understand and verify, rather than setting this constant
-to the value 10,800. See the [Rust Reference’s section on constant
-evaluation][const-eval] for more information on what operations can be used
-when declaring constants.
-
-Constants are valid for the entire time a program runs, within the scope in
-which they were declared. This property makes constants useful for values in
-your application domain that multiple parts of the program might need to know
-about, such as the maximum number of points any player of a game is allowed to
-earn, or the speed of light.
-
-Naming hardcoded values used throughout your program as constants is useful in
-conveying the meaning of that value to future maintainers of the code. It also
-helps to have only one place in your code you would need to change if the
-hardcoded value needed to be updated in the future.
+نام ثابت `THREE_HOURS_IN_SECONDS` است و مقدار آن برابر با نتیجه ضرب ۶۰ (تعداد ثانیهها در یک دقیقه) در ۶۰ (تعداد دقیقهها در یک ساعت) در ۳ (تعداد ساعتهایی که میخواهیم در این برنامه شمارش کنیم) تنظیم شده است. قانون نامگذاری ثابتها در Rust استفاده از حروف بزرگ با خط زیر (_) بین کلمات است. کامپایلر قادر است مجموعه محدودی از عملیات را در زمان کامپایل ارزیابی کند، که به ما این امکان را میدهد تا این مقدار را به صورتی بنویسیم که آسانتر قابلدرک و بررسی باشد، به جای تنظیم این ثابت به مقدار ۱۰،۸۰۰. برای اطلاعات بیشتر در مورد اینکه چه عملیاتهایی میتوانند در زمان تعریف ثابتها استفاده شوند، به [بخش ارزیابی ثابتها در مرجع Rust][const-eval] مراجعه کنید.
+
+ثابتها برای تمام مدت اجرای یک برنامه، در دامنهای که در آن تعریف شدهاند، معتبر هستند. این ویژگی، ثابتها را برای مقادیر موجود در دامنه برنامه شما که ممکن است بخشهای مختلف برنامه نیاز به دانستن آنها داشته باشند، مانند حداکثر تعداد امتیازاتی که هر بازیکن یک بازی میتواند کسب کند یا سرعت نور، مفید میسازد.
+
+نامگذاری مقادیر ثابت در سراسر برنامه شما به عنوان ثابتها، در انتقال معنی آن مقدار به نگهدارندگان آینده کد شما مفید است. همچنین این کمک میکند که فقط یک مکان در کد وجود داشته باشد که اگر مقدار ثابت نیاز به بهروزرسانی داشت، باید تغییر کند.
### Shadowing
-As you saw in the guessing game tutorial in [Chapter
-2][comparing-the-guess-to-the-secret-number], you can declare a
-new variable with the same name as a previous variable. Rustaceans say that the
-first variable is _shadowed_ by the second, which means that the second
-variable is what the compiler will see when you use the name of the variable.
-In effect, the second variable overshadows the first, taking any uses of the
-variable name to itself until either it itself is shadowed or the scope ends.
-We can shadow a variable by using the same variable’s name and repeating the
-use of the `let` keyword as follows:
+همانطور که در آموزش بازی حدس زدن در [فصل ۲][comparing-the-guess-to-the-secret-number] دیدید، شما میتوانید یک متغیر جدید با همان نام متغیر قبلی تعریف کنید. Rustaceanها میگویند که متغیر اول توسط متغیر دوم _سایه انداخته شده است_، به این معنا که متغیر دوم چیزی است که کامپایلر وقتی از نام متغیر استفاده میکنید میبیند. در واقع، متغیر دوم متغیر اول را تحتالشعاع قرار میدهد، استفادههای مربوط به نام متغیر را به خود اختصاص میدهد تا زمانی که یا خودش تحتالشعاع قرار بگیرد یا دامنه تمام شود. ما میتوانیم یک متغیر را با استفاده از همان نام متغیر و تکرار استفاده از کلیدواژه `let` به شرح زیر سایهاندازی کنیم:
Filename: src/main.rs
@@ -136,54 +80,30 @@ use of the `let` keyword as follows:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-03-shadowing/src/main.rs}}
```
-This program first binds `x` to a value of `5`. Then it creates a new variable
-`x` by repeating `let x =`, taking the original value and adding `1` so the
-value of `x` is then `6`. Then, within an inner scope created with the curly
-brackets, the third `let` statement also shadows `x` and creates a new
-variable, multiplying the previous value by `2` to give `x` a value of `12`.
-When that scope is over, the inner shadowing ends and `x` returns to being `6`.
-When we run this program, it will output the following:
+این برنامه ابتدا `x` را به مقدار `۵` متصل میکند. سپس یک متغیر جدید `x` با تکرار `let x =` ایجاد میکند و مقدار اصلی را میگیرد و `۱` اضافه میکند، بنابراین مقدار `x` به `۶` تغییر میکند. سپس، در یک دامنه داخلی که با آکولادها ایجاد شده است، عبارت سوم `let` نیز `x` را سایهاندازی میکند و یک متغیر جدید ایجاد میکند که مقدار قبلی را در `۲` ضرب میکند و به `x` مقدار `۱۲` میدهد. وقتی آن دامنه تمام میشود، سایهاندازی داخلی پایان مییابد و `x` به مقدار `۶` بازمیگردد. وقتی این برنامه را اجرا میکنیم، خروجی زیر را دریافت میکنیم:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-03-shadowing/output.txt}}
```
-Shadowing is different from marking a variable as `mut` because we’ll get a
-compile-time error if we accidentally try to reassign to this variable without
-using the `let` keyword. By using `let`, we can perform a few transformations
-on a value but have the variable be immutable after those transformations have
-been completed.
+سایهاندازی با علامتگذاری متغیر بهعنوان `mut` متفاوت است، زیرا اگر به طور تصادفی سعی کنید به این متغیر بدون استفاده از کلیدواژه `let` مقدار جدیدی تخصیص دهید، یک خطای زمان کامپایل دریافت میکنید. با استفاده از `let`، ما میتوانیم چند تبدیل روی یک مقدار انجام دهیم، اما متغیر بعد از اتمام این تبدیلها غیرقابل تغییر باقی میماند.
-The other difference between `mut` and shadowing is that because we’re
-effectively creating a new variable when we use the `let` keyword again, we can
-change the type of the value but reuse the same name. For example, say our
-program asks a user to show how many spaces they want between some text by
-inputting space characters, and then we want to store that input as a number:
+تفاوت دیگر بین `mut` و سایهاندازی این است که به دلیل اینکه ما عملاً یک متغیر جدید ایجاد میکنیم وقتی دوباره از کلیدواژه `let` استفاده میکنیم، میتوانیم نوع مقدار را تغییر دهیم اما همان نام را دوباره استفاده کنیم. برای مثال، فرض کنید برنامه ما از یک کاربر میخواهد تا نشان دهد که چند فاصله میخواهد بین متنهای خاص داشته باشد با وارد کردن کاراکترهای فاصله، و سپس میخواهیم آن ورودی را بهعنوان یک عدد ذخیره کنیم:
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-04-shadowing-can-change-types/src/main.rs:here}}
```
-The first `spaces` variable is a string type and the second `spaces` variable
-is a number type. Shadowing thus spares us from having to come up with
-different names, such as `spaces_str` and `spaces_num`; instead, we can reuse
-the simpler `spaces` name. However, if we try to use `mut` for this, as shown
-here, we’ll get a compile-time error:
+اولین متغیر `spaces` یک نوع رشته است و دومین متغیر `spaces` یک نوع عدد است. سایهاندازی در نتیجه ما را از نیاز به یافتن نامهای مختلف، مانند `spaces_str` و `spaces_num` نجات میدهد. به جای آن، میتوانیم از نام سادهتر `spaces` استفاده کنیم. با این حال، اگر سعی کنیم برای این کار از `mut` استفاده کنیم، همانطور که در اینجا نشان داده شده است، یک خطای زمان کامپایل دریافت میکنیم:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/src/main.rs:here}}
```
-The error says we’re not allowed to mutate a variable’s type:
+خطا میگوید که مجاز نیستیم نوع متغیر را تغییر دهیم:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-05-mut-cant-change-types/output.txt}}
```
-Now that we’ve explored how variables work, let’s look at more data types they
-can have.
-
-[comparing-the-guess-to-the-secret-number]: ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number
-[data-types]: ch03-02-data-types.html#data-types
-[storing-values-with-variables]: ch02-00-guessing-game-tutorial.html#storing-values-with-variables
-[const-eval]: ../reference/const_eval.html
+حال که بررسی کردیم متغیرها چگونه کار میکنند، بیایید نگاهی به انواع دادههای بیشتری بیندازیم که متغیرها میتوانند داشته باشند.
diff --git a/src/ch03-02-data-types.md b/src/ch03-02-data-types.md
index 7d0c32891c..2e43f103c2 100644
--- a/src/ch03-02-data-types.md
+++ b/src/ch03-02-data-types.md
@@ -1,138 +1,92 @@
-## Data Types
+## انواع دادهها
-Every value in Rust is of a certain _data type_, which tells Rust what kind of
-data is being specified so it knows how to work with that data. We’ll look at
-two data type subsets: scalar and compound.
+هر مقدار در زبان راست نوع خاصی از _داده_ را دارد که به راست میگوید چه نوع دادهای مشخص شده است تا بداند چگونه با آن داده کار کند. ما به دو زیرمجموعه از انواع داده نگاه خواهیم کرد: انواع ساده و ترکیبی.
-Keep in mind that Rust is a _statically typed_ language, which means that it
-must know the types of all variables at compile time. The compiler can usually
-infer what type we want to use based on the value and how we use it. In cases
-when many types are possible, such as when we converted a `String` to a numeric
-type using `parse` in the [“Comparing the Guess to the Secret
-Number”][comparing-the-guess-to-the-secret-number] section in
-Chapter 2, we must add a type annotation, like this:
+به خاطر داشته باشید که راست یک زبان _ایستا-تایپ_ است، به این معنا که باید نوع تمام متغیرها در زمان کامپایل مشخص باشد. کامپایلر معمولاً میتواند بر اساس مقدار و نحوه استفاده از آن، نوع مورد نظر ما را حدس بزند. در مواردی که انواع متعددی ممکن است، مانند زمانی که یک `String` را به نوع عددی تبدیل کردیم در بخش [“مقایسه حدس با عدد مخفی”][comparing-the-guess-to-the-secret-number] در فصل 2، باید یک تعریف نوع اضافه کنیم، مانند این:
```rust
let guess: u32 = "42".parse().expect("Not a number!");
```
-If we don’t add the `: u32` type annotation shown in the preceding code, Rust
-will display the following error, which means the compiler needs more
-information from us to know which type we want to use:
+اگر تعریف نوع `: u32` را که در کد بالا آمده است اضافه نکنیم، راست خطای زیر را نمایش میدهد، که به معنای این است که کامپایلر به اطلاعات بیشتری از ما نیاز دارد تا بداند کدام نوع را میخواهیم استفاده کنیم:
```console
{{#include ../listings/ch03-common-programming-concepts/output-only-01-no-type-annotations/output.txt}}
```
-You’ll see different type annotations for other data types.
-
-### Scalar Types
-
-A _scalar_ type represents a single value. Rust has four primary scalar types:
-integers, floating-point numbers, Booleans, and characters. You may recognize
-these from other programming languages. Let’s jump into how they work in Rust.
-
-#### Integer Types
-
-An _integer_ is a number without a fractional component. We used one integer
-type in Chapter 2, the `u32` type. This type declaration indicates that the
-value it’s associated with should be an unsigned integer (signed integer types
-start with `i` instead of `u`) that takes up 32 bits of space. Table 3-1 shows
-the built-in integer types in Rust. We can use any of these variants to declare
-the type of an integer value.
-
-Table 3-1: Integer Types in Rust
-
-| Length | Signed | Unsigned |
-| ------- | ------- | -------- |
-| 8-bit | `i8` | `u8` |
-| 16-bit | `i16` | `u16` |
-| 32-bit | `i32` | `u32` |
-| 64-bit | `i64` | `u64` |
-| 128-bit | `i128` | `u128` |
-| architecture dependent | `isize` | `usize` |
-
-Each variant can be either signed or unsigned and has an explicit size.
-_Signed_ and _unsigned_ refer to whether it’s possible for the number to be
-negative—in other words, whether the number needs to have a sign with it
-(signed) or whether it will only ever be positive and can therefore be
-represented without a sign (unsigned). It’s like writing numbers on paper: when
-the sign matters, a number is shown with a plus sign or a minus sign; however,
-when it’s safe to assume the number is positive, it’s shown with no sign.
-Signed numbers are stored using [two’s complement][twos-complement] representation.
-
-Each signed variant can store numbers from −(2n − 1) to 2n −
-1 − 1 inclusive, where _n_ is the number of bits that variant uses. So an
-`i8` can store numbers from −(27) to 27 − 1, which equals
-−128 to 127. Unsigned variants can store numbers from 0 to 2n − 1,
-so a `u8` can store numbers from 0 to 28 − 1, which equals 0 to 255.
-
-Additionally, the `isize` and `usize` types depend on the architecture of the
-computer your program is running on: 64 bits if you’re on a 64-bit architecture
-and 32 bits if you’re on a 32-bit architecture.
-
-You can write integer literals in any of the forms shown in Table 3-2. Note
-that number literals that can be multiple numeric types allow a type suffix,
-such as `57u8`, to designate the type. Number literals can also use `_` as a
-visual separator to make the number easier to read, such as `1_000`, which will
-have the same value as if you had specified `1000`.
-
-Table 3-2: Integer Literals in Rust
-
-| Number literals | Example |
-| ---------------- | ------------- |
-| Decimal | `98_222` |
-| Hex | `0xff` |
-| Octal | `0o77` |
-| Binary | `0b1111_0000` |
-| Byte (`u8` only) | `b'A'` |
-
-So how do you know which type of integer to use? If you’re unsure, Rust’s
-defaults are generally good places to start: integer types default to `i32`.
-The primary situation in which you’d use `isize` or `usize` is when indexing
-some sort of collection.
-
-> ##### Integer Overflow
+شما تعریفهای نوع مختلفی برای انواع دادههای دیگر خواهید دید.
+
+### انواع ساده
+
+یک نوع _ساده_ نمایانگر یک مقدار واحد است. راست چهار نوع ساده اصلی دارد: اعداد صحیح، اعداد اعشاری، بولینها و کاراکترها. ممکن است اینها را از زبانهای برنامهنویسی دیگر بشناسید. بیایید ببینیم چگونه در راست کار میکنند.
+
+#### انواع اعداد صحیح
+
+یک _عدد صحیح_ عددی بدون جزء اعشاری است. ما در فصل 2 از یک نوع عدد صحیح به نام `u32` استفاده کردیم. این تعریف نوع نشان میدهد که مقدار مرتبط باید یک عدد صحیح بدون علامت (انواع اعداد صحیح با علامت با `i` به جای `u` شروع میشوند) باشد که 32 بیت فضا اشغال میکند. جدول 3-1 انواع اعداد صحیح ساخته شده در راست را نشان میدهد. ما میتوانیم از هر یک از این حالتها برای تعریف نوع یک مقدار عدد صحیح استفاده کنیم.
+
+جدول 3-1: انواع اعداد صحیح در راست
+
+| طول | علامتدار | بدونعلامت |
+| ---------------- | --------- | ---------- |
+| ۸-بیتی | `i8` | `u8` |
+| ۱۶-بیتی | `i16` | `u16` |
+| ۳۲-بیتی | `i32` | `u32` |
+| ۶۴-بیتی | `i64` | `u64` |
+| ۱۲۸-بیتی | `i128` | `u128` |
+| وابسته به معماری | `isize` | `usize` |
+
+هر حالت میتواند یا با علامت یا بدون علامت باشد و اندازه صریحی دارد. _با علامت_ و _بدون علامت_ به این اشاره دارند که آیا ممکن است عدد منفی باشد یا خیر؛ به عبارت دیگر، آیا عدد نیاز به علامت دارد (با علامت) یا اینکه فقط مثبت خواهد بود و بنابراین میتوان آن را بدون علامت نشان داد (بدون علامت). این شبیه به نوشتن اعداد روی کاغذ است: وقتی علامت مهم باشد، عدد با علامت مثبت یا منفی نشان داده میشود؛ اما وقتی فرض مثبت بودن عدد ایمن باشد، بدون علامت نشان داده میشود. اعداد با علامت با استفاده از نمایش [دو مکمل][twos-complement] ذخیره میشوند.
+
+هر نوع عدد صحیح علامتدار میتواند مقادیری از
+−(2n − 1) تا 2n − 1 − 1 را
+در بر بگیرد، که در آن *n* تعداد بیتهای استفادهشده
+توسط آن نوع است. بنابراین، یک `i8` میتواند
+مقادیر بین −(27) تا 27 − 1
+را نگه دارد، یعنی از −۱۲۸ تا ۱۲۷.
+
+انواع بدونعلامت (unsigned) میتوانند مقادیر
+بین ۰ تا 2n − 1 را نگهداری کنند؛
+مثلاً یک `u8` میتواند مقادیری از ۰ تا
+28 − 1، یعنی از ۰ تا ۲۵۵ را ذخیره کند.
+
+علاوه بر این، نوعهای `isize` و `usize` به
+معماری سیستمی بستگی دارند که برنامه روی آن
+اجرا میشود: اگر معماری ۶۴ بیتی باشد، این نوعها
+۶۴ بیتی هستند، و اگر ۳۲ بیتی باشد، ۳۲ بیتی خواهند بود.
+
+شما میتوانید اعداد صحیح را به هر یک از اشکال نشان داده شده در جدول 3-2 بنویسید. توجه داشته باشید که عددهایی که میتوانند به چندین نوع عددی تبدیل شوند، یک پسوند نوع دارند، مانند `57u8`، برای تعیین نوع. اعداد همچنین میتوانند از `_` به عنوان جداکننده بصری برای خواناتر کردن استفاده کنند، مانند `1_000`، که همان مقدار `1000` را دارد.
+
+جدول 3-2: نمایش اعداد صحیح در راست
+
+| نوع اعداد | مثال |
+| -------------- | ---------- |
+| دهدهی | `98_222` |
+| هگزادسیمال | `0xff` |
+| اکتال | `0o77` |
+| باینری | `0b1111_0000` |
+| بایت (فقط `u8`) | `b'A'` |
+
+حال چگونه میدانید که از کدام نوع عدد صحیح استفاده کنید؟ اگر مطمئن نیستید، مقادیر پیشفرض راست معمولاً مکان خوبی برای شروع هستند: نوعهای عدد صحیح پیشفرض به `i32` تبدیل میشوند. وضعیت اصلی که در آن ممکن است از `isize` یا `usize` استفاده کنید زمانی است که میخواهید به یک نوع مجموعه اشاره کنید.
+
+> ##### سرریز عدد صحیح
>
-> Let’s say you have a variable of type `u8` that can hold values between 0 and
-> 255. If you try to change the variable to a value outside that range, such as
-> 256, _integer overflow_ will occur, which can result in one of two behaviors.
-> When you’re compiling in debug mode, Rust includes checks for integer overflow
-> that cause your program to _panic_ at runtime if this behavior occurs. Rust
-> uses the term _panicking_ when a program exits with an error; we’ll discuss
-> panics in more depth in the [“Unrecoverable Errors with
-> `panic!`”][unrecoverable-errors-with-panic] section in Chapter
-> 9.
+> فرض کنید یک متغیر از نوع `u8` دارید که میتواند مقادیر بین 0 و 255 را نگه دارد. اگر تلاش کنید مقدار متغیر را به عددی خارج از این بازه، مانند 256، تغییر دهید، _سرریز عدد صحیح_ رخ خواهد داد که میتواند منجر به یکی از دو رفتار شود. وقتی برنامه خود را در حالت دیباگ کامپایل میکنید، راست شامل بررسیهایی برای سرریز عدد صحیح است که باعث میشود برنامه شما در زمان اجرا _پانیک_ کند اگر این رفتار رخ دهد. راست از اصطلاح _پانیک کردن_ زمانی استفاده میکند که برنامه با یک خطا خارج شود؛ ما در بخش [“خطاهای غیرقابل بازیابی با `panic!`”][unrecoverable-errors-with-panic] در فصل 9 به طور عمیقتر درباره پانیکها بحث خواهیم کرد.
>
-> When you’re compiling in release mode with the `--release` flag, Rust does
-> _not_ include checks for integer overflow that cause panics. Instead, if
-> overflow occurs, Rust performs _two’s complement wrapping_. In short, values
-> greater than the maximum value the type can hold “wrap around” to the minimum
-> of the values the type can hold. In the case of a `u8`, the value 256 becomes
-> 0, the value 257 becomes 1, and so on. The program won’t panic, but the
-> variable will have a value that probably isn’t what you were expecting it to
-> have. Relying on integer overflow’s wrapping behavior is considered an error.
+> وقتی برنامه خود را در حالت انتشار با پرچم `--release` کامپایل میکنید، راست این بررسیها را برای سرریز عدد صحیح شامل نمیشود. در عوض، اگر سرریز رخ دهد، راست از _دو مکمل بستهبندی_ استفاده میکند. به طور خلاصه، مقادیر بزرگتر از حداکثر مقداری که نوع میتواند نگه دارد به "حداقل مقادیر" بازه نوع بستهبندی میشوند. در مورد یک `u8`، مقدار 256 به 0 تبدیل میشود، مقدار 257 به 1 و غیره. برنامه پانیک نخواهد کرد، اما متغیر مقدار متفاوتی نسبت به آنچه انتظار میرفت خواهد داشت. اعتماد به رفتار بستهبندی سرریز عدد صحیح یک خطا محسوب میشود.
>
-> To explicitly handle the possibility of overflow, you can use these families
-> of methods provided by the standard library for primitive numeric types:
+> برای مدیریت صریح امکان سرریز، میتوانید از این خانوادههای روشها استفاده کنید که توسط کتابخانه استاندارد برای نوعهای عددی اولیه ارائه شدهاند:
>
-> - Wrap in all modes with the `wrapping_*` methods, such as `wrapping_add`.
-> - Return the `None` value if there is overflow with the `checked_*` methods.
-> - Return the value and a Boolean indicating whether there was overflow with
-> the `overflowing_*` methods.
-> - Saturate at the value’s minimum or maximum values with the `saturating_*`
-> methods.
+> - در همه حالتها با استفاده از متدهای `wrapping_*` مانند `wrapping_add`، مقدار را wrap میکند.
+> - در صورت بروز overflow، مقدار `None` را با متدهای `checked_*` بازمیگرداند.
+> - مقدار و یک مقدار Boolean که نشان میدهد overflow رخ داده یا نه، با متدهای `overflowing_*` بازمیگردد.
+> - در مقدار حداقل یا حداکثر نوع متوقف میشود (saturate) با استفاده از متدهای `saturating_*`.
-#### Floating-Point Types
-Rust also has two primitive types for _floating-point numbers_, which are
-numbers with decimal points. Rust’s floating-point types are `f32` and `f64`,
-which are 32 bits and 64 bits in size, respectively. The default type is `f64`
-because on modern CPUs, it’s roughly the same speed as `f32` but is capable of
-more precision. All floating-point types are signed.
+#### انواع اعداد اعشاری
-Here’s an example that shows floating-point numbers in action:
+راست همچنین دو نوع اولیه برای _اعداد اعشاری_ دارد، که اعدادی با نقطه اعشار هستند. نوعهای اعشاری راست `f32` و `f64` هستند که به ترتیب 32 بیت و 64 بیت اندازه دارند. نوع پیشفرض `f64` است زیرا روی CPUهای مدرن، سرعت آن تقریباً مشابه `f32` است اما دقت بیشتری دارد. همه نوعهای اعشاری علامتدار هستند.
+
+در اینجا مثالی که اعداد اعشاری را در عمل نشان میدهد آورده شده است:
Filename: src/main.rs
@@ -140,14 +94,11 @@ Here’s an example that shows floating-point numbers in action:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-06-floating-point/src/main.rs}}
```
-Floating-point numbers are represented according to the IEEE-754 standard.
+اعداد اعشاری طبق استاندارد IEEE-754 نمایش داده میشوند.
-#### Numeric Operations
+#### عملیات عددی
-Rust supports the basic mathematical operations you’d expect for all the number
-types: addition, subtraction, multiplication, division, and remainder. Integer
-division truncates toward zero to the nearest integer. The following code shows
-how you’d use each numeric operation in a `let` statement:
+راست از عملیات ریاضی پایهای که برای تمام انواع عددی انتظار دارید پشتیبانی میکند: جمع، تفریق، ضرب، تقسیم و باقیمانده. تقسیم اعداد صحیح به نزدیکترین عدد صحیح به سمت صفر گرد میشود. کد زیر نشان میدهد چگونه میتوانید از هر عملیات عددی در یک عبارت `let` استفاده کنید:
Filename: src/main.rs
@@ -155,16 +106,11 @@ how you’d use each numeric operation in a `let` statement:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-07-numeric-operations/src/main.rs}}
```
-Each expression in these statements uses a mathematical operator and evaluates
-to a single value, which is then bound to a variable. [Appendix
-B][appendix_b] contains a list of all operators that Rust
-provides.
+هر عبارت در این دستورات از یک عملگر ریاضی استفاده میکند و به یک مقدار واحد ارزیابی میشود، که سپس به یک متغیر متصل میشود. [ضمیمه ب][appendix_b] شامل لیستی از تمام عملگرهایی است که راست فراهم میکند.
-#### The Boolean Type
+#### نوع بولین
-As in most other programming languages, a Boolean type in Rust has two possible
-values: `true` and `false`. Booleans are one byte in size. The Boolean type in
-Rust is specified using `bool`. For example:
+مانند اکثر زبانهای برنامهنویسی دیگر، نوع بولین در راست دو مقدار ممکن دارد: `true` و `false`. نوع بولین در راست یک بایت اندازه دارد. نوع بولین در راست با استفاده از `bool` مشخص میشود. برای مثال:
Filename: src/main.rs
@@ -172,14 +118,11 @@ Rust is specified using `bool`. For example:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-08-boolean/src/main.rs}}
```
-The main way to use Boolean values is through conditionals, such as an `if`
-expression. We’ll cover how `if` expressions work in Rust in the [“Control
-Flow”][control-flow] section.
+راه اصلی استفاده از مقادیر بولین از طریق عبارات شرطی، مانند عبارت `if` است. ما در بخش [“جریان کنترل”][control-flow] توضیح میدهیم که چگونه عبارات `if` در راست کار میکنند.
-#### The Character Type
+#### نوع کاراکتر
-Rust’s `char` type is the language’s most primitive alphabetic type. Here are
-some examples of declaring `char` values:
+نوع `char` در راست ابتداییترین نوع الفبایی زبان است. در اینجا برخی از مثالهای اعلام مقادیر `char` آورده شده است:
Filename: src/main.rs
@@ -187,32 +130,32 @@ some examples of declaring `char` values:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-09-char/src/main.rs}}
```
-Note that we specify `char` literals with single quotes, as opposed to string
-literals, which use double quotes. Rust’s `char` type is four bytes in size and
-represents a Unicode scalar value, which means it can represent a lot more than
-just ASCII. Accented letters; Chinese, Japanese, and Korean characters; emoji;
-and zero-width spaces are all valid `char` values in Rust. Unicode scalar
-values range from `U+0000` to `U+D7FF` and `U+E000` to `U+10FFFF` inclusive.
-However, a “character” isn’t really a concept in Unicode, so your human
-intuition for what a “character” is may not match up with what a `char` is in
-Rust. We’ll discuss this topic in detail in [“Storing UTF-8 Encoded Text with
-Strings”][strings] in Chapter 8.
+توجه داشته باشید که literals نوع `char` با
+نقلقولهای تکی مشخص میشوند، در حالی که literals
+رشتهای (string) از نقلقولهای دوتایی استفاده میکنند.
+نوع `char` در Rust اندازهای برابر با چهار بایت
+دارد و نمایانگر یک مقدار اسکالر یونیکد است، به این
+معنا که میتواند بسیار بیشتر از فقط کاراکترهای
+ASCII را نمایش دهد. حروف دارای اعراب، کاراکترهای
+چینی، ژاپنی و کرهای، ایموجیها و فضاهای بدون عرض
+همگی مقادیر معتبر `char` در Rust هستند. مقدارهای
+اسکالر یونیکد در بازهی `U+0000` تا `U+D7FF` و
+`U+E000` تا `U+10FFFF` شامل میشوند. با این حال،
+مفهوم “کاراکتر” در یونیکد واقعاً وجود ندارد،
+بنابراین تصور انسانی شما از “کاراکتر” ممکن است با
+آنچه `char` در Rust است تفاوت داشته باشد. این موضوع
+را در بخش [«ذخیره متن کدگذاریشده UTF-8 با رشتهها»][strings]
+در فصل ۸ بهطور مفصل بررسی خواهیم کرد.
-### Compound Types
+### انواع ترکیبی
-_Compound types_ can group multiple values into one type. Rust has two
-primitive compound types: tuples and arrays.
+_انواع ترکیبی_ میتوانند چندین مقدار را در یک نوع گروهبندی کنند. راست دو نوع ترکیبی اولیه دارد: تاپلها و آرایهها.
-#### The Tuple Type
+#### نوع تاپل
-A _tuple_ is a general way of grouping together a number of values with a
-variety of types into one compound type. Tuples have a fixed length: once
-declared, they cannot grow or shrink in size.
+_تاپل_ یک روش کلی برای گروهبندی چند مقدار با انواع مختلف در یک نوع ترکیبی است. تاپلها طول ثابتی دارند: پس از اعلام، نمیتوانند بزرگتر یا کوچکتر شوند.
-We create a tuple by writing a comma-separated list of values inside
-parentheses. Each position in the tuple has a type, and the types of the
-different values in the tuple don’t have to be the same. We’ve added optional
-type annotations in this example:
+ما یک تاپل را با نوشتن یک لیست جدا شده با کاما از مقادیر در داخل پرانتز ایجاد میکنیم. هر موقعیت در تاپل یک نوع دارد، و انواع مقادیر مختلف در تاپل نیازی به یکسان بودن ندارند. ما در این مثال حاشیهنویسی نوع اختیاری اضافه کردهایم:
Filename: src/main.rs
@@ -220,9 +163,7 @@ type annotations in this example:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-10-tuples/src/main.rs}}
```
-The variable `tup` binds to the entire tuple because a tuple is considered a
-single compound element. To get the individual values out of a tuple, we can
-use pattern matching to destructure a tuple value, like this:
+متغیر `tup` به کل تاپل متصل میشود زیرا یک تاپل به عنوان یک عنصر ترکیبی واحد در نظر گرفته میشود. برای استخراج مقادیر جداگانه از یک تاپل، میتوانیم از تطابق الگو برای تجزیه مقدار تاپل استفاده کنیم، مانند این:
Filename: src/main.rs
@@ -230,14 +171,9 @@ use pattern matching to destructure a tuple value, like this:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-11-destructuring-tuples/src/main.rs}}
```
-This program first creates a tuple and binds it to the variable `tup`. It then
-uses a pattern with `let` to take `tup` and turn it into three separate
-variables, `x`, `y`, and `z`. This is called _destructuring_ because it breaks
-the single tuple into three parts. Finally, the program prints the value of
-`y`, which is `6.4`.
+این برنامه ابتدا یک تاپل ایجاد کرده و آن را به متغیر `tup` متصل میکند. سپس از یک الگو با `let` برای گرفتن `tup` و تبدیل آن به سه متغیر جداگانه، `x`، `y`، و `z` استفاده میکند. این فرآیند _تجزیه_ نامیده میشود زیرا تاپل واحد را به سه قسمت تقسیم میکند. در نهایت، برنامه مقدار `y` را که `6.4` است، چاپ میکند.
-We can also access a tuple element directly by using a period (`.`) followed by
-the index of the value we want to access. For example:
+ما همچنین میتوانیم یک عنصر از تاپل را مستقیماً با استفاده از یک نقطه (`.`) به دنبال شماره شاخص مقدار مورد نظر دسترسی داشته باشیم. برای مثال:
Filename: src/main.rs
@@ -245,23 +181,15 @@ the index of the value we want to access. For example:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-12-tuple-indexing/src/main.rs}}
```
-This program creates the tuple `x` and then accesses each element of the tuple
-using their respective indices. As with most programming languages, the first
-index in a tuple is 0.
+این برنامه تاپل `x` را ایجاد کرده و سپس به هر عنصر تاپل با استفاده از شاخصهای مربوطه آنها دسترسی پیدا میکند. همانند اکثر زبانهای برنامهنویسی، اولین شاخص در یک تاپل `0` است.
-The tuple without any values has a special name, _unit_. This value and its
-corresponding type are both written `()` and represent an empty value or an
-empty return type. Expressions implicitly return the unit value if they don’t
-return any other value.
+تاپل بدون هیچ مقداری یک نام خاص دارد، _واحد_. این مقدار و نوع مربوط به آن هر دو با `()` نوشته میشوند و یک مقدار خالی یا یک نوع بازگشت خالی را نشان میدهند. عبارات به طور ضمنی مقدار واحد را بازمیگردانند اگر هیچ مقدار دیگری بازنگردانند.
-#### The Array Type
+#### نوع آرایه
-Another way to have a collection of multiple values is with an _array_. Unlike
-a tuple, every element of an array must have the same type. Unlike arrays in
-some other languages, arrays in Rust have a fixed length.
+روش دیگری برای داشتن مجموعهای از چند مقدار، استفاده از _آرایه_ است. برخلاف تاپل، هر عنصر آرایه باید از یک نوع باشد. برخلاف آرایهها در برخی زبانهای دیگر، آرایهها در راست طول ثابتی دارند.
-We write the values in an array as a comma-separated list inside square
-brackets:
+ما مقادیر یک آرایه را به صورت یک لیست جدا شده با کاما در داخل کروشه مینویسیم:
Filename: src/main.rs
@@ -269,53 +197,45 @@ brackets:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-13-arrays/src/main.rs}}
```
-Arrays are useful when you want your data allocated on the stack, the same as
-the other types we have seen so far, rather than the heap (we will discuss the
-stack and the heap more in [Chapter 4][stack-and-heap]) or when
-you want to ensure you always have a fixed number of elements. An array isn’t
-as flexible as the vector type, though. A _vector_ is a similar collection type
-provided by the standard library that _is_ allowed to grow or shrink in size
-because its contents live on the heap. If you’re unsure whether to use an array
-or a vector, chances are you should use a vector. [Chapter 8][vectors] discusses vectors in more detail.
-
-However, arrays are more useful when you know the number of elements will not
-need to change. For example, if you were using the names of the month in a
-program, you would probably use an array rather than a vector because you know
-it will always contain 12 elements:
+آرایهها زمانی کاربردی هستند که بخواهید دادههایتان
+روی stack تخصیص یابند، مشابه سایر نوعهایی که تاکنون
+دیدیم، نه روی heap (که در [فصل ۴][stack-and-heap] بیشتر
+دربارهی stack و heap صحبت خواهیم کرد) یا زمانی که
+میخواهید همیشه تعداد ثابتی از عناصر داشته باشید.
+با این حال، آرایه به اندازهی نوع vector انعطافپذیر
+نیست. *وکتور* نوعی مجموعه مشابه است که توسط کتابخانه
+استاندارد ارائه شده و اجازه دارد اندازهاش تغییر کند،
+چون محتوای آن روی heap ذخیره میشود. اگر مطمئن نیستید
+که از آرایه استفاده کنید یا وکتور، احتمالاً بهتر است
+وکتور را انتخاب کنید. [فصل ۸][vectors] بهطور دقیقتر
+دربارهی وکتورها بحث میکند.
+
+با این حال، آرایهها زمانی مفیدتر هستند که بدانید تعداد عناصر نیاز به تغییر ندارد. برای مثال، اگر از نامهای ماه در یک برنامه استفاده میکردید، احتمالاً از یک آرایه به جای یک وکتور استفاده میکردید زیرا میدانید همیشه ۱۲ عنصر خواهد داشت:
```rust
let months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
```
-You write an array’s type using square brackets with the type of each element,
-a semicolon, and then the number of elements in the array, like so:
+شما نوع یک آرایه را با استفاده از کروشهها به همراه نوع هر عنصر، یک نقطه ویرگول، و سپس تعداد عناصر در آرایه مینویسید، مانند این:
```rust
let a: [i32; 5] = [1, 2, 3, 4, 5];
```
-Here, `i32` is the type of each element. After the semicolon, the number `5`
-indicates the array contains five elements.
+در اینجا، `i32` نوع هر عنصر است. پس از نقطه ویرگول، عدد `۵` نشان میدهد که آرایه شامل پنج عنصر است.
-You can also initialize an array to contain the same value for each element by
-specifying the initial value, followed by a semicolon, and then the length of
-the array in square brackets, as shown here:
+شما همچنین میتوانید یک آرایه را طوری مقداردهی اولیه کنید که هر عنصر مقدار یکسانی داشته باشد، با مشخص کردن مقدار اولیه، یک نقطه ویرگول، و سپس طول آرایه در کروشهها، مانند این:
```rust
let a = [3; 5];
```
-The array named `a` will contain `5` elements that will all be set to the value
-`3` initially. This is the same as writing `let a = [3, 3, 3, 3, 3];` but in a
-more concise way.
+آرایهای با نام `a` شامل `۵` عنصر خواهد بود که همه ابتدا مقدار `۳` دارند. این همان نوشتن `let a = [3, 3, 3, 3, 3];` است، اما به شیوهای مختصرتر.
-##### Accessing Array Elements
+##### دسترسی به عناصر آرایه
-An array is a single chunk of memory of a known, fixed size that can be
-allocated on the stack. You can access elements of an array using indexing,
-like this:
+یک آرایه یک بخش واحد از حافظه با اندازهای مشخص و ثابت است که میتواند روی استک تخصیص داده شود. شما میتوانید به عناصر یک آرایه با استفاده از ایندکس دسترسی پیدا کنید، مانند این:
Filename: src/main.rs
@@ -323,15 +243,11 @@ like this:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-14-array-indexing/src/main.rs}}
```
-In this example, the variable named `first` will get the value `1` because that
-is the value at index `[0]` in the array. The variable named `second` will get
-the value `2` from index `[1]` in the array.
+در این مثال، متغیری با نام `first` مقدار `1` را میگیرد زیرا این مقدار در ایندکس `[0]` در آرایه قرار دارد. متغیری با نام `second` مقدار `2` را از ایندکس `[1]` در آرایه میگیرد.
-##### Invalid Array Element Access
+##### دسترسی نامعتبر به عنصر آرایه
-Let’s see what happens if you try to access an element of an array that is past
-the end of the array. Say you run this code, similar to the guessing game in
-Chapter 2, to get an array index from the user:
+ببینیم چه اتفاقی میافتد اگر بخواهید به عنصری از آرایه دسترسی پیدا کنید که خارج از محدوده آرایه است. فرض کنید این کد را اجرا کنید که مشابه بازی حدس در فصل ۲ است، تا یک ایندکس آرایه را از کاربر دریافت کند:
Filename: src/main.rs
@@ -339,10 +255,7 @@ Chapter 2, to get an array index from the user:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs}}
```
-This code compiles successfully. If you run this code using `cargo run` and
-enter `0`, `1`, `2`, `3`, or `4`, the program will print out the corresponding
-value at that index in the array. If you instead enter a number past the end of
-the array, such as `10`, you’ll see output like this:
+این کد به درستی کامپایل میشود. اگر این کد را با استفاده از `cargo run` اجرا کنید و مقادیری مانند `0`، `1`، `2`، `3` یا `4` را وارد کنید، برنامه مقدار متناظر در آن ایندکس از آرایه را چاپ میکند. اما اگر به جای آن عددی خارج از محدوده آرایه، مانند `10`، وارد کنید، خروجی چیزی شبیه به این خواهد بود:
-section of Chapter 14.
+راست همچنین نوع دیگری از کامنتها، کامنتهای مستندات (documentation comments) دارد که آنها را در بخش [“انتشار یک کرات در Crates.io”][publishing] از فصل 14 بررسی خواهیم کرد.
[publishing]: ch14-02-publishing-to-crates-io.html
diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md
index ee80626d1f..15bd7707e3 100644
--- a/src/ch03-05-control-flow.md
+++ b/src/ch03-05-control-flow.md
@@ -1,18 +1,12 @@
-## Control Flow
+## کنترل جریان
-The ability to run some code depending on whether a condition is `true` and to
-run some code repeatedly while a condition is `true` are basic building blocks
-in most programming languages. The most common constructs that let you control
-the flow of execution of Rust code are `if` expressions and loops.
+توانایی اجرای کدی که وابسته به درست بودن یا نبودن یک شرط است و اجرای مکرر کدی در حالی که یک شرط درست است، از ساختارهای اساسی در بیشتر زبانهای برنامهنویسی محسوب میشود. رایجترین ساختارهایی که به شما امکان کنترل جریان اجرای کد در راست را میدهند، عبارتند از عبارات `if` و حلقهها.
-### `if` Expressions
+### عبارات `if`
-An `if` expression allows you to branch your code depending on conditions. You
-provide a condition and then state, “If this condition is met, run this block
-of code. If the condition is not met, do not run this block of code.”
+یک عبارت `if` به شما امکان میدهد کد خود را بسته به شرایطی شاخهبندی کنید. شما یک شرط مشخص میکنید و سپس میگویید: «اگر این شرط برقرار بود، این بلوک کد اجرا شود. اگر شرط برقرار نبود، این بلوک کد اجرا نشود.»
-Create a new project called _branches_ in your _projects_ directory to explore
-the `if` expression. In the _src/main.rs_ file, input the following:
+یک پروژه جدید به نام _branches_ در دایرکتوری _projects_ خود ایجاد کنید تا عبارت `if` را بررسی کنید. در فایل _src/main.rs_ کد زیر را وارد کنید:
Filename: src/main.rs
@@ -20,43 +14,29 @@ the `if` expression. In the _src/main.rs_ file, input the following:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-26-if-true/src/main.rs}}
```
-All `if` expressions start with the keyword `if`, followed by a condition. In
-this case, the condition checks whether or not the variable `number` has a
-value less than 5. We place the block of code to execute if the condition is
-`true` immediately after the condition inside curly brackets. Blocks of code
-associated with the conditions in `if` expressions are sometimes called _arms_,
-just like the arms in `match` expressions that we discussed in the [“Comparing
-the Guess to the Secret Number”][comparing-the-guess-to-the-secret-number] section of Chapter 2.
+تمام عبارات `if` با کلمه کلیدی `if` شروع میشوند و سپس یک شرط دنبال میشود. در این مثال، شرط بررسی میکند که آیا مقدار متغیر `number` کمتر از 5 است یا خیر. بلوک کدی که در صورت درست بودن شرط باید اجرا شود، بلافاصله بعد از شرط و داخل کروشهها قرار میگیرد. بلوکهای کدی که با شرایط در عبارات `if` مرتبط هستند، گاهی _بازو_ (arm) نامیده میشوند، همانند بازوهای موجود در عبارات `match` که در بخش [“مقایسه حدس با عدد مخفی”][comparing-the-guess-to-the-secret-number] از فصل 2 مورد بحث قرار گرفت.
-Optionally, we can also include an `else` expression, which we chose to do
-here, to give the program an alternative block of code to execute should the
-condition evaluate to `false`. If you don’t provide an `else` expression and
-the condition is `false`, the program will just skip the `if` block and move on
-to the next bit of code.
+بهصورت اختیاری، میتوانیم یک عبارت `else` نیز اضافه کنیم، همانطور که اینجا انتخاب کردیم، تا به برنامه یک بلوک کد جایگزین برای اجرا ارائه دهیم، در صورتی که شرط به `false` ارزیابی شود. اگر عبارت `else` ارائه ندهید و شرط `false` باشد، برنامه بلوک `if` را نادیده گرفته و به بخش بعدی کد میرود.
-Try running this code; you should see the following output:
+این کد را اجرا کنید؛ باید خروجی زیر را مشاهده کنید:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-26-if-true/output.txt}}
```
-Let’s try changing the value of `number` to a value that makes the condition
-`false` to see what happens:
+بیایید مقدار `number` را به مقداری تغییر دهیم که شرط `false` شود تا ببینیم چه اتفاقی میافتد:
```rust,ignore
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-27-if-false/src/main.rs:here}}
```
-Run the program again, and look at the output:
+برنامه را دوباره اجرا کنید و خروجی را مشاهده کنید:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-27-if-false/output.txt}}
```
-It’s also worth noting that the condition in this code _must_ be a `bool`. If
-the condition isn’t a `bool`, we’ll get an error. For example, try running the
-following code:
+همچنین قابل توجه است که شرط در این کد _باید_ یک `bool` باشد. اگر شرط یک `bool` نباشد، خطا دریافت خواهیم کرد. به عنوان مثال، این کد را اجرا کنید:
Filename: src/main.rs
@@ -64,19 +44,13 @@ following code:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/src/main.rs}}
```
-The `if` condition evaluates to a value of `3` this time, and Rust throws an
-error:
+این بار شرط `if` به مقدار `3` ارزیابی میشود و راست خطا میدهد:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-28-if-condition-must-be-bool/output.txt}}
```
-The error indicates that Rust expected a `bool` but got an integer. Unlike
-languages such as Ruby and JavaScript, Rust will not automatically try to
-convert non-Boolean types to a Boolean. You must be explicit and always provide
-`if` with a Boolean as its condition. If we want the `if` code block to run
-only when a number is not equal to `0`, for example, we can change the `if`
-expression to the following:
+خطا نشان میدهد که راست انتظار یک `bool` داشت اما یک عدد صحیح دریافت کرد. برخلاف زبانهایی مانند Ruby و JavaScript، راست بهصورت خودکار تلاش نمیکند انواع غیر `bool` را به یک `bool` تبدیل کند. شما باید صریح باشید و همیشه یک `bool` را بهعنوان شرط به `if` بدهید. اگر میخواهید بلوک کد `if` فقط زمانی اجرا شود که یک عدد برابر `0` نباشد، میتوانید عبارت `if` را به این صورت تغییر دهید:
Filename: src/main.rs
@@ -84,12 +58,11 @@ expression to the following:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-29-if-not-equal-0/src/main.rs}}
```
-Running this code will print `number was something other than zero`.
+اجرای این کد `number was something other than zero` را چاپ خواهد کرد.
-#### Handling Multiple Conditions with `else if`
+#### مدیریت شرایط متعدد با `else if`
-You can use multiple conditions by combining `if` and `else` in an `else if`
-expression. For example:
+شما میتوانید با ترکیب `if` و `else` در یک عبارت `else if`، شرایط متعددی را مدیریت کنید. به عنوان مثال:
Filename: src/main.rs
@@ -97,30 +70,21 @@ expression. For example:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-30-else-if/src/main.rs}}
```
-This program has four possible paths it can take. After running it, you should
-see the following output:
+این برنامه چهار مسیر ممکن برای اجرا دارد. پس از اجرای آن، باید خروجی زیر را مشاهده کنید:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-30-else-if/output.txt}}
```
-When this program executes, it checks each `if` expression in turn and executes
-the first body for which the condition evaluates to `true`. Note that even
-though 6 is divisible by 2, we don’t see the output `number is divisible by 2`,
-nor do we see the `number is not divisible by 4, 3, or 2` text from the `else`
-block. That’s because Rust only executes the block for the first `true`
-condition, and once it finds one, it doesn’t even check the rest.
+هنگامی که این برنامه اجرا میشود، هر عبارت `if` را به ترتیب بررسی کرده و اولین بلوکی که شرط آن به `true` ارزیابی شود، اجرا میکند. توجه داشته باشید که حتی با وجود اینکه 6 بر 2 بخشپذیر است، خروجی `number is divisible by 2` را نمیبینیم و همچنین متن `number is not divisible by 4, 3, or 2` از بلوک `else` را نیز نمیبینیم. این به این دلیل است که راست فقط بلوک مربوط به اولین شرط درست را اجرا میکند و پس از یافتن آن، بقیه را بررسی نمیکند.
-Using too many `else if` expressions can clutter your code, so if you have more
-than one, you might want to refactor your code. Chapter 6 describes a powerful
-Rust branching construct called `match` for these cases.
+استفاده از تعداد زیادی عبارت `else if` میتواند کد شما را شلوغ کند، بنابراین اگر بیش از یک مورد دارید، ممکن است بخواهید کد خود را بازنویسی کنید. فصل 6 یک ساختار شاخهبندی قدرتمند در راست به نام `match` را برای این موارد توضیح میدهد.
-#### Using `if` in a `let` Statement
+#### استفاده از `if` در یک عبارت `let`
-Because `if` is an expression, we can use it on the right side of a `let`
-statement to assign the outcome to a variable, as in Listing 3-2.
+از آنجایی که `if` یک عبارت است، میتوانیم از آن در سمت راست یک عبارت `let` برای تخصیص نتیجه به یک متغیر استفاده کنیم، همانطور که در لیست 3-2 نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-02/src/main.rs}}
@@ -128,20 +92,13 @@ statement to assign the outcome to a variable, as in Listing 3-2.
-The `number` variable will be bound to a value based on the outcome of the `if`
-expression. Run this code to see what happens:
+متغیر `number` به مقداری بر اساس نتیجه عبارت `if` متصل خواهد شد. این کد را اجرا کنید تا ببینید چه اتفاقی میافتد:
```console
{{#include ../listings/ch03-common-programming-concepts/listing-03-02/output.txt}}
```
-Remember that blocks of code evaluate to the last expression in them, and
-numbers by themselves are also expressions. In this case, the value of the
-whole `if` expression depends on which block of code executes. This means the
-values that have the potential to be results from each arm of the `if` must be
-the same type; in Listing 3-2, the results of both the `if` arm and the `else`
-arm were `i32` integers. If the types are mismatched, as in the following
-example, we’ll get an error:
+به خاطر داشته باشید که بلوکهای کد به آخرین عبارت در آنها ارزیابی میشوند و اعداد به تنهایی نیز عبارات محسوب میشوند. در این حالت، مقدار کل عبارت `if` بستگی به این دارد که کدام بلوک کد اجرا شود. این بدان معناست که مقادیری که میتوانند نتایج هر بازوی `if` باشند، باید از یک نوع باشند. در لیست 3-2، نتایج بازوی `if` و بازوی `else` هر دو اعداد صحیح `i32` بودند. اگر انواع ناسازگار باشند، مانند مثال زیر، خطایی دریافت خواهیم کرد:
Filename: src/main.rs
@@ -149,39 +106,25 @@ example, we’ll get an error:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/src/main.rs}}
```
-When we try to compile this code, we’ll get an error. The `if` and `else` arms
-have value types that are incompatible, and Rust indicates exactly where to
-find the problem in the program:
+هنگامی که تلاش میکنیم این کد را کامپایل کنیم، خطایی دریافت میکنیم. بازوهای `if` و `else` دارای انواع مقداری ناسازگار هستند و راست دقیقاً نشان میدهد که مشکل در برنامه کجاست:
```console
{{#include ../listings/ch03-common-programming-concepts/no-listing-31-arms-must-return-same-type/output.txt}}
```
-The expression in the `if` block evaluates to an integer, and the expression in
-the `else` block evaluates to a string. This won’t work because variables must
-have a single type, and Rust needs to know at compile time what type the
-`number` variable is, definitively. Knowing the type of `number` lets the
-compiler verify the type is valid everywhere we use `number`. Rust wouldn’t be
-able to do that if the type of `number` was only determined at runtime; the
-compiler would be more complex and would make fewer guarantees about the code
-if it had to keep track of multiple hypothetical types for any variable.
+عبارت موجود در بلوک `if` به یک عدد صحیح ارزیابی میشود و عبارت موجود در بلوک `else` به یک رشته ارزیابی میشود. این کار نمیکند زیرا متغیرها باید یک نوع مشخص داشته باشند و راست باید در زمان کامپایل بداند که نوع متغیر `number` چیست. دانستن نوع `number` به کامپایلر این امکان را میدهد که بررسی کند نوع آن در هر جایی که از `number` استفاده میکنیم معتبر است. راست نمیتوانست این کار را انجام دهد اگر نوع `number` تنها در زمان اجرا مشخص میشد. کامپایلر پیچیدهتر میشد و تضمینهای کمتری درباره کد ارائه میداد اگر مجبور بود انواع فرضی مختلفی را برای هر متغیر پیگیری کند.
-### Repetition with Loops
+### تکرار با حلقهها
-It’s often useful to execute a block of code more than once. For this task,
-Rust provides several _loops_, which will run through the code inside the loop
-body to the end and then start immediately back at the beginning. To experiment
-with loops, let’s make a new project called _loops_.
+اغلب مفید است که یک بلوک کد بیش از یک بار اجرا شود. برای این کار، Rust چندین _حلقه_ ارائه میدهد که کد داخل بدنه حلقه را اجرا کرده و سپس بلافاصله به ابتدای حلقه بازمیگردند. برای آزمایش با حلقهها، یک پروژه جدید به نام _loops_ ایجاد کنید.
-Rust has three kinds of loops: `loop`, `while`, and `for`. Let’s try each one.
+Rust سه نوع حلقه دارد: `loop`، `while` و `for`. بیایید هر کدام را امتحان کنیم.
-#### Repeating Code with `loop`
+#### تکرار کد با `loop`
-The `loop` keyword tells Rust to execute a block of code over and over again
-forever or until you explicitly tell it to stop.
+کلمه کلیدی `loop` به Rust میگوید که یک بلوک کد را بارها و بارها اجرا کند، تا زمانی که شما به طور صریح به آن بگویید متوقف شود.
-As an example, change the _src/main.rs_ file in your _loops_ directory to look
-like this:
+به عنوان مثال، فایل _src/main.rs_ را در دایرکتوری _loops_ خود به شکل زیر تغییر دهید:
Filename: src/main.rs
@@ -189,10 +132,7 @@ like this:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-loop/src/main.rs}}
```
-When we run this program, we’ll see `again!` printed over and over continuously
-until we stop the program manually. Most terminals support the keyboard shortcut
-ctrl-c to interrupt a program that is stuck in a continual
-loop. Give it a try:
+وقتی این برنامه را اجرا کنیم، `again!` بارها و بارها به طور مداوم چاپ میشود تا زمانی که برنامه را به صورت دستی متوقف کنیم. اکثر ترمینالها از میانبر صفحه کلید ctrl-c برای متوقف کردن برنامهای که در یک حلقه بیپایان گیر کرده است، پشتیبانی میکنند. آن را امتحان کنید:
section of Chapter 2 to exit the program when the user won the game by
-guessing the correct number.
+خوشبختانه، Rust همچنین روشی برای خروج از یک حلقه با استفاده از کد ارائه میدهد. شما میتوانید کلمه کلیدی `break` را درون حلقه قرار دهید تا به برنامه بگویید که چه زمانی اجرای حلقه را متوقف کند. به یاد داشته باشید که این کار را در بازی حدس عدد در بخش [“خروج پس از یک حدس درست”][quitting-after-a-correct-guess] در فصل 2 انجام دادیم تا زمانی که کاربر با حدس درست بازی را برنده شد، برنامه خاتمه یابد.
-We also used `continue` in the guessing game, which in a loop tells the program
-to skip over any remaining code in this iteration of the loop and go to the
-next iteration.
+ما همچنین از `continue` در بازی حدس عدد استفاده کردیم که در یک حلقه به برنامه میگوید هر کد باقیمانده در این تکرار حلقه را نادیده بگیرد و به تکرار بعدی برود.
-#### Returning Values from Loops
+#### بازگرداندن مقادیر از حلقهها
-One of the uses of a `loop` is to retry an operation you know might fail, such
-as checking whether a thread has completed its job. You might also need to pass
-the result of that operation out of the loop to the rest of your code. To do
-this, you can add the value you want returned after the `break` expression you
-use to stop the loop; that value will be returned out of the loop so you can
-use it, as shown here:
+یکی از کاربردهای `loop` این است که یک عملیات را که ممکن است شکست بخورد دوباره امتحان کنید، مثلاً بررسی کنید که آیا یک نخ (thread) کار خود را تمام کرده است یا نه. همچنین ممکن است نیاز داشته باشید نتیجه این عملیات را از حلقه به بقیه کد خود منتقل کنید. برای انجام این کار، میتوانید مقداری که میخواهید برگردانده شود را پس از عبارت `break` اضافه کنید. این مقدار از حلقه بازگردانده میشود تا بتوانید از آن استفاده کنید، همانطور که در اینجا نشان داده شده است:
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-33-return-value-from-loop/src/main.rs}}
```
-Before the loop, we declare a variable named `counter` and initialize it to
-`0`. Then we declare a variable named `result` to hold the value returned from
-the loop. On every iteration of the loop, we add `1` to the `counter` variable,
-and then check whether the `counter` is equal to `10`. When it is, we use the
-`break` keyword with the value `counter * 2`. After the loop, we use a
-semicolon to end the statement that assigns the value to `result`. Finally, we
-print the value in `result`, which in this case is `20`.
+قبل از حلقه، یک متغیر به نام `counter` اعلام میکنیم و مقدار آن را `0` مقداردهی اولیه میکنیم. سپس یک متغیر به نام `result` اعلام میکنیم تا مقدار بازگشتی از حلقه را نگه دارد. در هر تکرار حلقه، مقدار `1` را به متغیر `counter` اضافه میکنیم و سپس بررسی میکنیم که آیا مقدار `counter` برابر با `10` است یا نه. زمانی که این شرط برقرار باشد، از کلمه کلیدی `break` با مقدار `counter * 2` استفاده میکنیم. پس از حلقه، با استفاده از یک سمیکالن، مقدار به `result` تخصیص داده میشود. در نهایت، مقدار `result` را چاپ میکنیم که در این مثال برابر با `20` است.
-You can also `return` from inside a loop. While `break` only exits the current
-loop, `return` always exits the current function.
+شما همچنین میتوانید از داخل یک حلقه `return` استفاده کنید. در حالی که `break` فقط از حلقه جاری خارج میشود، `return` همیشه از تابع جاری خارج میشود.
-#### Loop Labels to Disambiguate Between Multiple Loops
+#### برچسب حلقهها برای رفع ابهام بین چندین حلقه
-If you have loops within loops, `break` and `continue` apply to the innermost
-loop at that point. You can optionally specify a _loop label_ on a loop that
-you can then use with `break` or `continue` to specify that those keywords
-apply to the labeled loop instead of the innermost loop. Loop labels must begin
-with a single quote. Here’s an example with two nested loops:
+اگر حلقههایی تو در تو داشته باشید، `break` و `continue` به حلقه داخلیترین حلقه در آن نقطه اعمال میشوند. به طور اختیاری میتوانید یک _برچسب حلقه_ روی یک حلقه مشخص کنید که سپس میتوانید از آن برچسب با `break` یا `continue` استفاده کنید تا مشخص کنید که این کلمات کلیدی به حلقه برچسبدار اعمال میشوند نه حلقه داخلیترین. برچسبهای حلقه باید با یک آپاستروف شروع شوند. در اینجا یک مثال با دو حلقه تو در تو آمده است:
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/src/main.rs}}
```
-The outer loop has the label `'counting_up`, and it will count up from 0 to 2.
-The inner loop without a label counts down from 10 to 9. The first `break` that
-doesn’t specify a label will exit the inner loop only. The `break
-'counting_up;` statement will exit the outer loop. This code prints:
+حلقه بیرونی دارای برچسب `'counting_up` است و از 0 تا 2 شمارش میکند. حلقه داخلی بدون برچسب از 10 تا 9 شمارش معکوس میکند. اولین `break` که برچسبی مشخص نمیکند فقط از حلقه داخلی خارج میشود. عبارت `break 'counting_up;` از حلقه بیرونی خارج میشود. این کد موارد زیر را چاپ میکند:
```console
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-32-5-loop-labels/output.txt}}
```
-#### Conditional Loops with `while`
+#### حلقههای شرطی با `while`
-A program will often need to evaluate a condition within a loop. While the
-condition is `true`, the loop runs. When the condition ceases to be `true`, the
-program calls `break`, stopping the loop. It’s possible to implement behavior
-like this using a combination of `loop`, `if`, `else`, and `break`; you could
-try that now in a program, if you’d like. However, this pattern is so common
-that Rust has a built-in language construct for it, called a `while` loop. In
-Listing 3-3, we use `while` to loop the program three times, counting down each
-time, and then, after the loop, print a message and exit.
+یک برنامه اغلب نیاز دارد که یک شرط را درون یک حلقه ارزیابی کند. تا زمانی که شرط `true` باشد، حلقه اجرا میشود. زمانی که شرط دیگر `true` نباشد، برنامه با فراخوانی `break`، حلقه را متوقف میکند. امکان پیادهسازی چنین رفتاری با استفاده از ترکیب `loop`، `if`، `else` و `break` وجود دارد. میتوانید این را اکنون در یک برنامه امتحان کنید، اگر مایل هستید. با این حال، این الگو آنقدر رایج است که Rust یک سازه زبان داخلی برای آن دارد که به آن حلقه `while` گفته میشود. در Listing 3-3، از `while` برای اجرای برنامه سه بار، شمارش معکوس در هر بار، و سپس چاپ یک پیام و خروج از حلقه استفاده میکنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-03/src/main.rs}}
@@ -292,17 +201,16 @@ time, and then, after the loop, print a message and exit.
-This construct eliminates a lot of nesting that would be necessary if you used
-`loop`, `if`, `else`, and `break`, and it’s clearer. While a condition
-evaluates to `true`, the code runs; otherwise, it exits the loop.
+این سازه مقدار زیادی از تو در تویی که در صورت استفاده از `loop`، `if`، `else` و `break` لازم بود را حذف میکند و واضحتر است. تا زمانی که یک شرط به مقدار `true` ارزیابی شود، کد اجرا میشود؛ در غیر این صورت، حلقه متوقف میشود.
-#### Looping Through a Collection with `for`
+#### تکرار از طریق یک مجموعه با `for`
-You can choose to use the `while` construct to loop over the elements of a
-collection, such as an array. For example, the loop in Listing 3-4 prints each
-element in the array `a`.
+میتوانید از ساختار `while` برای تکرار روی عناصر
+یک مجموعه، مانند آرایه، استفاده کنید. بهعنوان مثال،
+حلقهی موجود در فهرست 3-4، هر عنصر موجود در آرایه
+`a` را چاپ میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-04/src/main.rs}}
@@ -310,30 +218,19 @@ element in the array `a`.
-Here, the code counts up through the elements in the array. It starts at index
-`0`, and then loops until it reaches the final index in the array (that is,
-when `index < 5` is no longer `true`). Running this code will print every
-element in the array:
+در اینجا، کد از طریق عناصر آرایه شمارش میکند. از اندیس (index)`0` شروع میکند و سپس تا زمانی که به آخرین اندیس (index)در آرایه برسد (یعنی وقتی که `index < 5` دیگر `true` نباشد) حلقه میزند. اجرای این کد هر عنصر در آرایه را چاپ میکند:
```console
{{#include ../listings/ch03-common-programming-concepts/listing-03-04/output.txt}}
```
-All five array values appear in the terminal, as expected. Even though `index`
-will reach a value of `5` at some point, the loop stops executing before trying
-to fetch a sixth value from the array.
+همه پنج مقدار آرایه همانطور که انتظار میرود در ترمینال ظاهر میشوند. حتی اگر `index` در نهایت به مقدار `5` برسد، حلقه قبل از تلاش برای گرفتن مقدار ششم از آرایه متوقف میشود.
-However, this approach is error prone; we could cause the program to panic if
-the index value or test condition is incorrect. For example, if you changed the
-definition of the `a` array to have four elements but forgot to update the
-condition to `while index < 4`, the code would panic. It’s also slow, because
-the compiler adds runtime code to perform the conditional check of whether the
-index is within the bounds of the array on every iteration through the loop.
+با این حال، این روش مستعد خطاست؛ ما میتوانیم باعث شویم برنامه در صورت اشتباه بودن مقدار اندیس (index)یا شرط آزمایشی متوقف شود. به عنوان مثال، اگر تعریف آرایه `a` را به چهار عنصر تغییر دهید اما فراموش کنید شرط را به `while index < 4` بهروزرسانی کنید، کد متوقف خواهد شد. همچنین این روش کند است، زیرا کامپایلر کد زمان اجرا را برای انجام بررسی شرطی در مورد اینکه آیا اندیس (index)در محدوده آرایه است یا نه در هر تکرار حلقه اضافه میکند.
-As a more concise alternative, you can use a `for` loop and execute some code
-for each item in a collection. A `for` loop looks like the code in Listing 3-5.
+به عنوان یک جایگزین مختصرتر، میتوانید از حلقه `for` استفاده کنید و برای هر مورد در یک مجموعه، کدی اجرا کنید. یک حلقه `for` شبیه کدی در Listing 3-5 است.
-
+
```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-05/src/main.rs}}
@@ -341,27 +238,20 @@ for each item in a collection. A `for` loop looks like the code in Listing 3-5.
-When we run this code, we’ll see the same output as in Listing 3-4. More
-importantly, we’ve now increased the safety of the code and eliminated the
-chance of bugs that might result from going beyond the end of the array or not
-going far enough and missing some items. Machine code generated from `for`
-loops can be more efficient as well, because the index doesn’t need to be
-compared to the length of the array at every iteration.
+وقتی این کد را اجرا کنیم، خروجی مشابه فهرست 3-4
+را خواهیم دید. مهمتر اینکه، اکنون ایمنی کد افزایش
+یافته و احتمال بروز خطاهایی که ممکن است از دسترسی
+فراتر از انتهای آرایه یا عدم پیمایش کامل عناصر
+نشأت بگیرند، حذف شده است. همچنین، کد ماشینی که
+از حلقههای `for` تولید میشود میتواند کارآمدتر
+باشد، زیرا در هر تکرار نیازی به مقایسهی اندیس با
+طول آرایه نیست.
-Using the `for` loop, you wouldn’t need to remember to change any other code if
-you changed the number of values in the array, as you would with the method
-used in Listing 3-4.
+با استفاده از حلقه `for`، نیازی به به خاطر سپردن تغییر کد دیگری ندارید اگر تعداد مقادیر در آرایه را تغییر دهید، همانطور که با روش استفاده شده در Listing 3-4 باید انجام میدادید.
-The safety and conciseness of `for` loops make them the most commonly used loop
-construct in Rust. Even in situations in which you want to run some code a
-certain number of times, as in the countdown example that used a `while` loop
-in Listing 3-3, most Rustaceans would use a `for` loop. The way to do that
-would be to use a `Range`, provided by the standard library, which generates
-all numbers in sequence starting from one number and ending before another
-number.
+ایمنی و مختصر بودن حلقههای `for` آنها را به رایجترین سازه حلقهای در Rust تبدیل کرده است. حتی در موقعیتهایی که میخواهید کدی را تعداد مشخصی از دفعات اجرا کنید، مانند مثال شمارش معکوس که از حلقه `while` در Listing 3-3 استفاده میکرد، اکثر برنامهنویسان Rust از حلقه `for` استفاده میکنند. روش انجام این کار استفاده از `Range`، که توسط کتابخانه استاندارد ارائه میشود، است که تمام اعداد را به ترتیب از یک عدد شروع کرده و قبل از عدد دیگری به پایان میرساند.
-Here’s what the countdown would look like using a `for` loop and another method
-we’ve not yet talked about, `rev`, to reverse the range:
+این چیزی است که شمارش معکوس با استفاده از یک حلقه `for` و روش دیگری که هنوز در مورد آن صحبت نکردهایم، یعنی `rev` برای معکوس کردن محدوده، به نظر میرسد:
Filename: src/main.rs
@@ -369,22 +259,17 @@ we’ve not yet talked about, `rev`, to reverse the range:
{{#rustdoc_include ../listings/ch03-common-programming-concepts/no-listing-34-for-range/src/main.rs}}
```
-This code is a bit nicer, isn’t it?
+این کد کمی بهتر نیست؟
-## Summary
+## خلاصه
-You made it! This was a sizable chapter: you learned about variables, scalar
-and compound data types, functions, comments, `if` expressions, and loops! To
-practice with the concepts discussed in this chapter, try building programs to
-do the following:
+شما موفق شدید! این یک فصل بزرگ بود: شما درباره متغیرها، انواع داده اسکالر و مرکب، توابع، نظرات، عبارات `if` و حلقهها یاد گرفتید! برای تمرین با مفاهیم مطرحشده در این فصل، سعی کنید برنامههایی برای انجام موارد زیر بسازید:
-- Convert temperatures between Fahrenheit and Celsius.
-- Generate the *n*th Fibonacci number.
-- Print the lyrics to the Christmas carol “The Twelve Days of Christmas,”
- taking advantage of the repetition in the song.
+- تبدیل دما بین فارنهایت و سلسیوس.
+- تولید عدد *n*ام دنباله فیبوناچی.
+- چاپ متن سرود کریسمس "The Twelve Days of Christmas"، با استفاده از تکرار موجود در این آهنگ.
-When you’re ready to move on, we’ll talk about a concept in Rust that _doesn’t_
-commonly exist in other programming languages: ownership.
+وقتی آماده شدید تا به مرحله بعد بروید، ما درباره مفهومی در Rust صحبت خواهیم کرد که _معمولاً_ در زبانهای برنامهنویسی دیگر وجود ندارد: مالکیت.
[comparing-the-guess-to-the-secret-number]: ch02-00-guessing-game-tutorial.html#comparing-the-guess-to-the-secret-number
[quitting-after-a-correct-guess]: ch02-00-guessing-game-tutorial.html#quitting-after-a-correct-guess
diff --git a/src/ch04-00-understanding-ownership.md b/src/ch04-00-understanding-ownership.md
index 52eda6a12f..ec8149f768 100644
--- a/src/ch04-00-understanding-ownership.md
+++ b/src/ch04-00-understanding-ownership.md
@@ -1,7 +1,3 @@
-# Understanding Ownership
+# درک مالکیت
-Ownership is Rust’s most unique feature and has deep implications for the rest
-of the language. It enables Rust to make memory safety guarantees without
-needing a garbage collector, so it’s important to understand how ownership
-works. In this chapter, we’ll talk about ownership as well as several related
-features: borrowing, slices, and how Rust lays data out in memory.
+مالکیت یکی از ویژگیهای منحصر به فرد Rust است و تأثیرات عمیقی بر سایر بخشهای زبان دارد. این ویژگی به Rust اجازه میدهد تا بدون نیاز به یک جمعآوری زباله (garbage collector)، تضمینهای ایمنی حافظه را فراهم کند، بنابراین درک چگونگی کارکرد مالکیت بسیار مهم است. در این فصل، ما درباره مالکیت و چند ویژگی مرتبط دیگر صحبت خواهیم کرد: قرض گرفتن (borrowing)، برشها (slices) و نحوه چیدمان دادهها در حافظه توسط Rust.
diff --git a/src/ch04-01-what-is-ownership.md b/src/ch04-01-what-is-ownership.md
index d3d73b2b41..eb38e14d91 100644
--- a/src/ch04-01-what-is-ownership.md
+++ b/src/ch04-01-what-is-ownership.md
@@ -1,119 +1,110 @@
-## What Is Ownership?
-
-_Ownership_ is a set of rules that govern how a Rust program manages memory.
-All programs have to manage the way they use a computer’s memory while running.
-Some languages have garbage collection that regularly looks for no-longer-used
-memory as the program runs; in other languages, the programmer must explicitly
-allocate and free the memory. Rust uses a third approach: memory is managed
-through a system of ownership with a set of rules that the compiler checks. If
-any of the rules are violated, the program won’t compile. None of the features
-of ownership will slow down your program while it’s running.
-
-Because ownership is a new concept for many programmers, it does take some time
-to get used to. The good news is that the more experienced you become with Rust
-and the rules of the ownership system, the easier you’ll find it to naturally
-develop code that is safe and efficient. Keep at it!
-
-When you understand ownership, you’ll have a solid foundation for understanding
-the features that make Rust unique. In this chapter, you’ll learn ownership by
-working through some examples that focus on a very common data structure:
-strings.
-
-> ### The Stack and the Heap
+## مالکیت چیست؟
+
+_مالکیت_ مجموعهای از قوانین است که نحوه مدیریت حافظه را در برنامههای Rust تعیین میکند. همه برنامهها باید نحوه استفاده از حافظه کامپیوتر را در هنگام اجرا مدیریت کنند. برخی زبانها از جمعآوری زباله استفاده میکنند که به طور منظم حافظهای را که دیگر استفاده نمیشود بررسی میکند؛ در دیگر زبانها، برنامهنویس باید حافظه را به صورت صریح تخصیص داده و آزاد کند. Rust از یک روش سوم استفاده میکند: حافظه از طریق سیستمی از مالکیت با مجموعهای از قوانین مدیریت میشود که کامپایلر آنها را بررسی میکند. اگر هر یک از این قوانین نقض شود، برنامه کامپایل نخواهد شد. هیچیک از ویژگیهای مالکیت برنامه شما را در هنگام اجرا کند نمیکند.
+
+از آنجا که مالکیت یک مفهوم جدید برای بسیاری از برنامهنویسان است، زمان میبرد تا به آن عادت کنید. خبر خوب این است که هر چه بیشتر با Rust و قوانین سیستم مالکیت آن آشنا شوید، نوشتن کدی که امن و کارآمد باشد برایتان آسانتر خواهد شد. به تلاش ادامه دهید!
+
+وقتی مالکیت را درک کنید، پایهای محکم برای درک ویژگیهایی که Rust را منحصر به فرد میکنند خواهید داشت. در این فصل، مالکیت را با کار بر روی چند مثال که بر یک ساختار داده بسیار رایج تمرکز دارند یاد خواهید گرفت: رشتهها.
+
+> ### استک (Stack) و هیپ (Heap)
>
-> Many programming languages don’t require you to think about the stack and the
-> heap very often. But in a systems programming language like Rust, whether a
-> value is on the stack or the heap affects how the language behaves and why
-> you have to make certain decisions. Parts of ownership will be described in
-> relation to the stack and the heap later in this chapter, so here is a brief
-> explanation in preparation.
+> بسیاری از زبانهای برنامهنویسی نیاز ندارند که
+> شما زیاد دربارهی استک و هیپ فکر کنید. اما در زبانهای
+> برنامهنویسی سیستمی مانند Rust، محل قرارگیری مقدار
+> روی استک یا هیپ روی رفتار زبان و تصمیماتی که باید
+> بگیرید تأثیر میگذارد. بخشهایی از مالکیت (ownership)
+> در ارتباط با استک و هیپ در ادامهی این فصل شرح داده
+> خواهند شد، بنابراین در اینجا توضیح کوتاهی در
+> آمادهسازی برای آن ارائه میدهیم.
>
-> Both the stack and the heap are parts of memory available to your code to use
-> at runtime, but they are structured in different ways. The stack stores
-> values in the order it gets them and removes the values in the opposite
-> order. This is referred to as _last in, first out_. Think of a stack of
-> plates: when you add more plates, you put them on top of the pile, and when
-> you need a plate, you take one off the top. Adding or removing plates from
-> the middle or bottom wouldn’t work as well! Adding data is called _pushing
-> onto the stack_, and removing data is called _popping off the stack_. All
-> data stored on the stack must have a known, fixed size. Data with an unknown
-> size at compile time or a size that might change must be stored on the heap
-> instead.
+> استک و هیپ هر دو بخشهایی از حافظه هستند که
+> در زمان اجرا برای کد شما در دسترساند، اما ساختار
+> متفاوتی دارند. استک مقادیر را به ترتیبی که دریافت
+> میکند ذخیره کرده و آنها را به ترتیب معکوس حذف میکند.
+> به این مدل *آخرین وارد شده، اولین خارج شده* (last in, first out)
+> گفته میشود. تصور کنید یک دسته بشقاب: زمانی که
+> بشقاب جدیدی اضافه میکنید، آن را روی بالای دسته قرار
+> میدهید و وقتی بخواهید بشقابی بردارید، از بالای دسته
+> برمیدارید. اضافه یا حذف کردن بشقاب از وسط یا پایین
+> دسته به خوبی کار نخواهد کرد! افزودن داده به استک
+> را *push کردن روی استک* و حذف داده را *pop کردن از استک*
+> مینامند. تمامی دادههای ذخیرهشده روی استک باید
+> اندازهای مشخص و ثابت داشته باشند. دادههایی که اندازه
+> آنها هنگام کامپایل مشخص نیست یا ممکن است تغییر کند،
+> باید روی هیپ ذخیره شوند.
>
-> The heap is less organized: when you put data on the heap, you request a
-> certain amount of space. The memory allocator finds an empty spot in the heap
-> that is big enough, marks it as being in use, and returns a _pointer_, which
-> is the address of that location. This process is called _allocating on the
-> heap_ and is sometimes abbreviated as just _allocating_ (pushing values onto
-> the stack is not considered allocating). Because the pointer to the heap is a
-> known, fixed size, you can store the pointer on the stack, but when you want
-> the actual data, you must follow the pointer. Think of being seated at a
-> restaurant. When you enter, you state the number of people in your group, and
-> the host finds an empty table that fits everyone and leads you there. If
-> someone in your group comes late, they can ask where you’ve been seated to
-> find you.
+> هیپ ساختار کمتری دارد: وقتی دادهای را روی هیپ میگذارید،
+> فضایی مشخص درخواست میکنید. تخصیصدهندهی حافظه
+> (memory allocator) محلی خالی در هیپ پیدا میکند که
+> بهاندازه کافی بزرگ باشد، آن را بهعنوان فضای استفادهشده
+> علامتگذاری میکند و یک *اشارهگر* که آدرس آن مکان است
+> بازمیگرداند. این فرایند را *تخصیص در هیپ* مینامند
+> و گاهی بهسادگی *تخصیص* خوانده میشود (push کردن روی
+> استک بهعنوان تخصیص محسوب نمیشود). چون اندازه
+> اشارهگر روی هیپ ثابت و مشخص است، میتوانید اشارهگر را
+> روی استک ذخیره کنید، اما وقتی به دادهی واقعی نیاز دارید،
+> باید از طریق آن اشارهگر مراجعه کنید. این موضوع را
+> میتوان به نشستن در رستوران تشبیه کرد: وقتی وارد
+> میشوید، تعداد افراد گروه را میگویید، میزبان
+> میز خالیای پیدا میکند که همه را در خود جای دهد
+> و شما را به آنجا هدایت میکند. اگر کسی دیر برسد،
+> میتواند بپرسد شما کجا نشستهاید تا شما را پیدا کند.
>
-> Pushing to the stack is faster than allocating on the heap because the
-> allocator never has to search for a place to store new data; that location is
-> always at the top of the stack. Comparatively, allocating space on the heap
-> requires more work because the allocator must first find a big enough space
-> to hold the data and then perform bookkeeping to prepare for the next
-> allocation.
+> افزودن داده به استک سریعتر از تخصیص در هیپ است،
+> زیرا تخصیصدهنده نیازی به جستجوی جای خالی برای داده
+> جدید ندارد؛ این مکان همیشه بالای استک است. در مقایسه،
+> تخصیص فضای هیپ نیازمند کار بیشتری است، چون ابتدا
+> باید فضای کافی پیدا شود و سپس اقدامات لازم برای
+> مدیریت تخصیص بعدی انجام شود.
>
-> Accessing data in the heap is generally slower than accessing data on the
-> stack because you have to follow a pointer to get there. Contemporary
-> processors are faster if they jump around less in memory. Continuing the
-> analogy, consider a server at a restaurant taking orders from many tables.
-> It’s most efficient to get all the orders at one table before moving on to
-> the next table. Taking an order from table A, then an order from table B,
-> then one from A again, and then one from B again would be a much slower
-> process. By the same token, a processor can usually do its job better if it
-> works on data that’s close to other data (as it is on the stack) rather than
-> farther away (as it can be on the heap).
+> دسترسی به دادههای روی هیپ معمولاً کندتر از دادههای
+> روی استک است، چون باید اشارهگر را دنبال کنید.
+> پردازندههای امروزی زمانی سریعتر کار میکنند که پرش
+> کمتری در حافظه داشته باشند. با ادامهی تشبیه، فرض کنید
+> یک پیشخدمت در رستوران سفارشهای میزهای مختلف را میگیرد.
+> کارآمدترین روش این است که همه سفارشهای یک میز را
+> کامل دریافت کند و سپس به میز بعدی برود. گرفتن سفارش از
+> میز A، سپس میز B، دوباره میز A و سپس میز B، روندی
+> بسیار کندتر خواهد بود. به همین ترتیب، پردازنده معمولاً
+> بهتر کار میکند اگر روی دادههایی کار کند که به دادههای
+> دیگر نزدیک باشند (مثل دادههای روی استک) نه دادههایی
+> که دورتر هستند (مثل دادههای روی هیپ).
>
-> When your code calls a function, the values passed into the function
-> (including, potentially, pointers to data on the heap) and the function’s
-> local variables get pushed onto the stack. When the function is over, those
-> values get popped off the stack.
+> وقتی کد شما تابعی را فراخوانی میکند، مقادیری که به
+> تابع داده میشوند (شامل اشارهگرهایی به دادههای روی هیپ)
+> و متغیرهای محلی تابع روی استک قرار میگیرند. وقتی تابع
+> به پایان رسید، این دادهها از استک حذف میشوند.
>
-> Keeping track of what parts of code are using what data on the heap,
-> minimizing the amount of duplicate data on the heap, and cleaning up unused
-> data on the heap so you don’t run out of space are all problems that ownership
-> addresses. Once you understand ownership, you won’t need to think about the
-> stack and the heap very often, but knowing that the main purpose of ownership
-> is to manage heap data can help explain why it works the way it does.
+> پیگیری اینکه کدام بخشهای کد از کدام دادهها روی هیپ
+> استفاده میکنند، کمینهسازی دادههای تکراری روی هیپ،
+> و پاکسازی دادههای استفادهنشده روی هیپ تا فضای کافی
+> باقی بماند، همه مسائلی هستند که مالکیت (ownership) به آنها
+> میپردازد. وقتی مالکیت را درک کنید، نیاز نیست زیاد دربارهی
+> استک و هیپ فکر کنید، اما دانستن اینکه هدف اصلی مالکیت
+> مدیریت دادههای روی هیپ است، میتواند توضیح دهد چرا
+> مالکیت اینگونه عمل میکند.
-### Ownership Rules
+### قوانین مالکیت
-First, let’s take a look at the ownership rules. Keep these rules in mind as we
-work through the examples that illustrate them:
+ابتدا، بیایید نگاهی به قوانین مالکیت بیندازیم. این قوانین را در ذهن داشته باشید زیرا با مثالهایی که آنها را نشان میدهند کار میکنیم:
-- Each value in Rust has an _owner_.
-- There can only be one owner at a time.
-- When the owner goes out of scope, the value will be dropped.
+- هر مقدار در Rust یک _مالک_ دارد.
+- در یک زمان فقط میتواند یک مالک وجود داشته باشد.
+- زمانی که مالک از دامنه خارج شود، مقدار _حذف_ خواهد شد.
-### Variable Scope
+### دامنه متغیر
-Now that we’re past basic Rust syntax, we won’t include all the `fn main() {`
-code in examples, so if you’re following along, make sure to put the following
-examples inside a `main` function manually. As a result, our examples will be a
-bit more concise, letting us focus on the actual details rather than
-boilerplate code.
+حال که از سینتکس پایه Rust گذشتهایم، در مثالها کد کامل `fn main() {` را نخواهیم آورد. بنابراین، اگر دنبال میکنید، مطمئن شوید که مثالهای زیر را به صورت دستی داخل یک تابع `main` قرار دهید. در نتیجه، مثالهای ما کمی مختصرتر خواهند بود و میتوانیم بر روی جزئیات واقعی به جای کد ابتدایی تمرکز کنیم.
-As a first example of ownership, we’ll look at the _scope_ of some variables. A
-scope is the range within a program for which an item is valid. Take the
-following variable:
+به عنوان اولین مثال از مالکیت، به _دامنه_ برخی متغیرها نگاه میکنیم. دامنه محدودهای است که در آن یک آیتم در یک برنامه معتبر است. به متغیر زیر توجه کنید:
```rust
let s = "hello";
```
-The variable `s` refers to a string literal, where the value of the string is
-hardcoded into the text of our program. The variable is valid from the point at
-which it’s declared until the end of the current _scope_. Listing 4-1 shows a
-program with comments annotating where the variable `s` would be valid.
+متغیر `s` به یک رشتهی ثابت اشاره دارد، جایی که مقدار رشته به صورت ثابت در متن برنامه ما کدنویسی شده است. این متغیر از نقطهای که اعلام شده معتبر است تا انتهای _دامنه_ جاری. لیست 4-1 برنامهای را با توضیحاتی که نشان میدهند متغیر `s` در کجا معتبر است، نمایش میدهد.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-01/src/main.rs:here}}
@@ -121,127 +112,67 @@ program with comments annotating where the variable `s` would be valid.
-In other words, there are two important points in time here:
-
-- When `s` comes _into_ scope, it is valid.
-- It remains valid until it goes _out of_ scope.
-
-At this point, the relationship between scopes and when variables are valid is
-similar to that in other programming languages. Now we’ll build on top of this
-understanding by introducing the `String` type.
-
-### The `String` Type
-
-To illustrate the rules of ownership, we need a data type that is more complex
-than those we covered in the [“Data Types”][data-types] section
-of Chapter 3. The types covered previously are of a known size, can be stored
-on the stack and popped off the stack when their scope is over, and can be
-quickly and trivially copied to make a new, independent instance if another
-part of code needs to use the same value in a different scope. But we want to
-look at data that is stored on the heap and explore how Rust knows when to
-clean up that data, and the `String` type is a great example.
-
-We’ll concentrate on the parts of `String` that relate to ownership. These
-aspects also apply to other complex data types, whether they are provided by
-the standard library or created by you. We’ll discuss `String` in more depth in
-[Chapter 8][ch8].
-
-We’ve already seen string literals, where a string value is hardcoded into our
-program. String literals are convenient, but they aren’t suitable for every
-situation in which we may want to use text. One reason is that they’re
-immutable. Another is that not every string value can be known when we write
-our code: for example, what if we want to take user input and store it? For
-these situations, Rust has a second string type, `String`. This type manages
-data allocated on the heap and as such is able to store an amount of text that
-is unknown to us at compile time. You can create a `String` from a string
-literal using the `from` function, like so:
+به عبارت دیگر، در اینجا دو نقطهی مهم زمانی وجود دارد:
+
+- وقتی `s` وارد دامنه میشود، معتبر است.
+- تا زمانی که از دامنه خارج شود معتبر باقی میماند.
+
+در این نقطه، رابطه بین دامنهها و زمانهایی که متغیرها معتبر هستند مشابه با زبانهای برنامهنویسی دیگر است. حالا بر اساس این درک، نوع `String` را معرفی میکنیم.
+
+### نوع `String`
+
+برای نشان دادن قوانین مالکیت، به نوع دادهای نیاز داریم که پیچیدهتر از آنهایی باشد که در بخش [“انواع داده”][data-types] فصل ۳ بررسی کردیم. انواعی که قبلاً پوشش داده شد، اندازهی مشخصی دارند، میتوانند در استک ذخیره شوند و وقتی دامنهشان تمام شد از استک برداشته شوند و میتوانند به سرعت و به سادگی برای ساختن یک نمونهی جدید و مستقل کپی شوند اگر قسمت دیگری از کد بخواهد همان مقدار را در دامنهی دیگری استفاده کند. اما ما میخواهیم به دادههایی نگاه کنیم که در هیپ ذخیره شدهاند و بررسی کنیم چگونه Rust میداند چه زمانی باید این دادهها را پاکسازی کند، و نوع `String` یک مثال عالی است.
+
+ما روی بخشهایی از `String` تمرکز خواهیم کرد که به مالکیت مربوط میشوند. این جنبهها همچنین به سایر انواع دادههای پیچیده اعمال میشوند، چه آنهایی که توسط کتابخانه استاندارد ارائه شدهاند و چه آنهایی که خودتان ایجاد کردهاید. ما `String` را در فصل [۸][ch8] با جزئیات بیشتری بررسی خواهیم کرد.
+
+قبلاً رشتههای ثابت را دیدهایم، جایی که مقدار رشته در کد ما به صورت ثابت قرار گرفته است. رشتههای ثابت راحت هستند، اما برای هر موقعیتی که ممکن است بخواهیم از متن استفاده کنیم مناسب نیستند. یکی از دلایل این است که آنها تغییرناپذیر هستند. دلیل دیگر این است که نمیتوان هر مقدار رشته را هنگام نوشتن کد خود دانست: به عنوان مثال، اگر بخواهیم ورودی کاربر را بگیریم و ذخیره کنیم چه؟ برای این شرایط، Rust یک نوع رشتهی دیگر به نام `String` دارد. این نوع دادههای تخصیصیافته در هیپ را مدیریت میکند و به همین دلیل میتواند مقدار متنی را ذخیره کند که اندازهی آن در زمان کامپایل برای ما ناشناخته است. شما میتوانید یک `String` را از یک رشتهی ثابت با استفاده از تابع `from` ایجاد کنید، به این صورت:
```rust
let s = String::from("hello");
```
-The double colon `::` operator allows us to namespace this particular `from`
-function under the `String` type rather than using some sort of name like
-`string_from`. We’ll discuss this syntax more in the [“Method
-Syntax”][method-syntax] section of Chapter 5, and when we talk
-about namespacing with modules in [“Paths for Referring to an Item in the
-Module Tree”][paths-module-tree] in Chapter 7.
+عملگر `::` به ما اجازه میدهد این تابع `from` خاص را تحت نوع `String` نامگذاری کنیم به جای استفاده از نوعی نام مانند `string_from`. این سینتکس را بیشتر در بخش [“سینتکس متد”][method-syntax] فصل ۵ و هنگامی که در مورد نامگذاری با ماژولها صحبت میکنیم در بخش [“مسیرها برای ارجاع به یک آیتم در درخت ماژول”][paths-module-tree] فصل ۷ بررسی خواهیم کرد.
-This kind of string _can_ be mutated:
+این نوع رشته _میتواند_ تغییر کند:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs:here}}
```
-So, what’s the difference here? Why can `String` be mutated but literals
-cannot? The difference is in how these two types deal with memory.
+پس، تفاوت اینجا چیست؟ چرا `String` میتواند تغییر کند اما رشتههای ثابت نمیتوانند؟ تفاوت در نحوهی مدیریت حافظه توسط این دو نوع است.
-### Memory and Allocation
+### حافظه و تخصیص
-In the case of a string literal, we know the contents at compile time, so the
-text is hardcoded directly into the final executable. This is why string
-literals are fast and efficient. But these properties only come from the string
-literal’s immutability. Unfortunately, we can’t put a blob of memory into the
-binary for each piece of text whose size is unknown at compile time and whose
-size might change while running the program.
+در مورد یک رشتهی ثابت، ما محتوا را در زمان کامپایل میدانیم، بنابراین متن به طور مستقیم در فایل اجرایی نهایی کدنویسی شده است. به همین دلیل رشتههای ثابت سریع و کارآمد هستند. اما این ویژگیها فقط از تغییرناپذیری رشتهی ثابت ناشی میشوند. متأسفانه، نمیتوانیم یک تکه حافظه را برای هر قطعه متنی که اندازهی آن در زمان کامپایل ناشناخته است و ممکن است در حین اجرای برنامه تغییر کند، در فایل باینری قرار دهیم.
-With the `String` type, in order to support a mutable, growable piece of text,
-we need to allocate an amount of memory on the heap, unknown at compile time,
-to hold the contents. This means:
+با نوع `String`، برای پشتیبانی از یک متن قابل تغییر و قابل رشد، ما نیاز داریم مقداری حافظه را در هیپ تخصیص دهیم که در زمان کامپایل ناشناخته است تا محتوا را نگه داریم. این به این معناست که:
-- The memory must be requested from the memory allocator at runtime.
-- We need a way of returning this memory to the allocator when we’re done with
- our `String`.
+- حافظه باید در زمان اجرا از تخصیصدهنده حافظه درخواست شود.
+- ما نیاز داریم راهی برای بازگرداندن این حافظه به تخصیصدهنده زمانی که کارمان با `String` تمام شد، داشته باشیم.
-That first part is done by us: when we call `String::from`, its implementation
-requests the memory it needs. This is pretty much universal in programming
-languages.
+قسمت اول توسط ما انجام میشود: وقتی که `String::from` را فراخوانی میکنیم، پیادهسازی آن حافظهای را که نیاز دارد درخواست میکند. این تقریباً در تمام زبانهای برنامهنویسی رایج است.
-However, the second part is different. In languages with a _garbage collector
-(GC)_, the GC keeps track of and cleans up memory that isn’t being used
-anymore, and we don’t need to think about it. In most languages without a GC,
-it’s our responsibility to identify when memory is no longer being used and to
-call code to explicitly free it, just as we did to request it. Doing this
-correctly has historically been a difficult programming problem. If we forget,
-we’ll waste memory. If we do it too early, we’ll have an invalid variable. If
-we do it twice, that’s a bug too. We need to pair exactly one `allocate` with
-exactly one `free`.
+اما قسمت دوم متفاوت است. در زبانهایی که دارای _جمعکننده زباله (GC)_ هستند، GC حافظهای را که دیگر استفاده نمیشود پیگیری و پاکسازی میکند و ما نیازی به فکر کردن در مورد آن نداریم. در بیشتر زبانهایی که GC ندارند، این مسئولیت بر عهده ماست که مشخص کنیم چه زمانی حافظه دیگر استفاده نمیشود و کدی را برای آزادسازی صریح آن فراخوانی کنیم، دقیقاً همانطور که آن را درخواست کردهایم. انجام درست این کار در تاریخ برنامهنویسی یک مشکل دشوار بوده است. اگر فراموش کنیم، حافظه هدر میرود. اگر خیلی زود این کار را انجام دهیم، یک متغیر نامعتبر خواهیم داشت. اگر دو بار این کار را انجام دهیم، این هم یک باگ است. ما نیاز داریم دقیقاً یک `allocate` را با دقیقاً یک `free` جفت کنیم.
-Rust takes a different path: the memory is automatically returned once the
-variable that owns it goes out of scope. Here’s a version of our scope example
-from Listing 4-1 using a `String` instead of a string literal:
+Rust مسیر متفاوتی را طی میکند: حافظه به طور خودکار وقتی که متغیری که مالک آن است از دامنه خارج میشود بازگردانده میشود. در اینجا نسخهای از مثال دامنه ما از فهرست ۴-۱ وجود دارد که از یک `String` به جای یک رشتهی ثابت استفاده میکند:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-02-string-scope/src/main.rs:here}}
```
-There is a natural point at which we can return the memory our `String` needs
-to the allocator: when `s` goes out of scope. When a variable goes out of
-scope, Rust calls a special function for us. This function is called
-[`drop`][drop], and it’s where the author of `String` can put
-the code to return the memory. Rust calls `drop` automatically at the closing
-curly bracket.
-
-> Note: In C++, this pattern of deallocating resources at the end of an item’s
-> lifetime is sometimes called _Resource Acquisition Is Initialization (RAII)_.
-> The `drop` function in Rust will be familiar to you if you’ve used RAII
-> patterns.
+یک نقطه طبیعی وجود دارد که میتوانیم حافظهای را که `String` ما نیاز دارد به تخصیصدهنده بازگردانیم: وقتی `s` از دامنه خارج میشود. وقتی یک متغیر از دامنه خارج میشود، Rust یک تابع خاص را برای ما فراخوانی میکند. این تابع [`drop`][drop] نامیده میشود، و اینجا جایی است که نویسنده `String` میتواند کدی برای بازگرداندن حافظه قرار دهد. Rust به طور خودکار `drop` را در زمان بستن آکولاد فراخوانی میکند.
-This pattern has a profound impact on the way Rust code is written. It may seem
-simple right now, but the behavior of code can be unexpected in more
-complicated situations when we want to have multiple variables use the data
-we’ve allocated on the heap. Let’s explore some of those situations now.
+> نکته: در C++، این الگو که منابع در انتهای دوره عمر یک آیتم آزاد میشوند گاهی اوقات _Resource Acquisition Is Initialization (RAII)_ نامیده میشود. تابع `drop` در Rust برای کسانی که از الگوهای RAII استفاده کردهاند آشنا خواهد بود.
-
+این الگو تأثیر عمیقی بر نحوه نوشتن کد در Rust دارد. ممکن است اکنون ساده به نظر برسد، اما رفتار کد میتواند در موقعیتهای پیچیدهتر که میخواهیم متغیرهای متعددی از دادههایی که در هیپ تخصیص دادهایم استفاده کنند، غیرمنتظره باشد. اکنون به بررسی برخی از این موقعیتها میپردازیم.
-#### Variables and Data Interacting with Move
+#### تعامل متغیرها و دادهها با انتقال (Move)
-Multiple variables can interact with the same data in different ways in Rust.
-Let’s look at an example using an integer in Listing 4-2.
+متغیرهای مختلف میتوانند در Rust به روشهای مختلفی با دادهها تعامل داشته باشند. بیایید به مثالی با استفاده از یک عدد صحیح در فهرست ۴-۲ نگاه کنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-02/src/main.rs:here}}
@@ -249,222 +180,123 @@ Let’s look at an example using an integer in Listing 4-2.
-We can probably guess what this is doing: “bind the value `5` to `x`; then make
-a copy of the value in `x` and bind it to `y`.” We now have two variables, `x`
-and `y`, and both equal `5`. This is indeed what is happening, because integers
-are simple values with a known, fixed size, and these two `5` values are pushed
-onto the stack.
+ما احتمالاً میتوانیم حدس بزنیم این کد چه میکند: "مقدار `5` را به `x` اختصاص بده؛ سپس یک کپی از مقدار `x` بگیر و آن را به `y` اختصاص بده." اکنون دو متغیر داریم، `x` و `y`، و هر دو برابر `5` هستند. این دقیقاً همان چیزی است که اتفاق میافتد، زیرا اعداد صحیح مقادیر سادهای با اندازهی مشخص هستند، و این دو مقدار `5` به استک اضافه میشوند.
-Now let’s look at the `String` version:
+اکنون بیایید به نسخه `String` نگاه کنیم:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-03-string-move/src/main.rs:here}}
```
-This looks very similar, so we might assume that the way it works would be the
-same: that is, the second line would make a copy of the value in `s1` and bind
-it to `s2`. But this isn’t quite what happens.
-
-Take a look at Figure 4-1 to see what is happening to `String` under the
-covers. A `String` is made up of three parts, shown on the left: a pointer to
-the memory that holds the contents of the string, a length, and a capacity.
-This group of data is stored on the stack. On the right is the memory on the
-heap that holds the contents.
-
-
-
-Figure 4-1: Representation in memory of a `String`
-holding the value `"hello"` bound to `s1`
-
-The length is how much memory, in bytes, the contents of the `String` are
-currently using. The capacity is the total amount of memory, in bytes, that the
-`String` has received from the allocator. The difference between length and
-capacity matters, but not in this context, so for now, it’s fine to ignore the
-capacity.
-
-When we assign `s1` to `s2`, the `String` data is copied, meaning we copy the
-pointer, the length, and the capacity that are on the stack. We do not copy the
-data on the heap that the pointer refers to. In other words, the data
-representation in memory looks like Figure 4-2.
-
-
-
-Figure 4-2: Representation in memory of the variable `s2`
-that has a copy of the pointer, length, and capacity of `s1`
-
-The representation does _not_ look like Figure 4-3, which is what memory would
-look like if Rust instead copied the heap data as well. If Rust did this, the
-operation `s2 = s1` could be very expensive in terms of runtime performance if
-the data on the heap were large.
-
-
-
-Figure 4-3: Another possibility for what `s2 = s1` might
-do if Rust copied the heap data as well
-
-Earlier, we said that when a variable goes out of scope, Rust automatically
-calls the `drop` function and cleans up the heap memory for that variable. But
-Figure 4-2 shows both data pointers pointing to the same location. This is a
-problem: when `s2` and `s1` go out of scope, they will both try to free the
-same memory. This is known as a _double free_ error and is one of the memory
-safety bugs we mentioned previously. Freeing memory twice can lead to memory
-corruption, which can potentially lead to security vulnerabilities.
-
-To ensure memory safety, after the line `let s2 = s1;`, Rust considers `s1` as
-no longer valid. Therefore, Rust doesn’t need to free anything when `s1` goes
-out of scope. Check out what happens when you try to use `s1` after `s2` is
-created; it won’t work:
+این بسیار مشابه به نظر میرسد، بنابراین ممکن است فرض کنیم که نحوه عملکرد آن نیز مشابه است: یعنی، خط دوم یک کپی از مقدار موجود در `s1` میگیرد و آن را به `s2` اختصاص میدهد. اما این دقیقاً چیزی نیست که اتفاق میافتد.
+
+به شکل ۴-۱ نگاه کنید تا ببینید که در پشت صحنه با `String` چه اتفاقی میافتد. یک `String` از سه بخش تشکیل شده است که در سمت چپ نشان داده شدهاند: یک اشارهگر (Pointer) به حافظهای که محتوای رشته را نگه میدارد، یک طول، و یک ظرفیت. این گروه دادهها روی استک ذخیره میشوند. در سمت راست، حافظه روی هیپ قرار دارد که محتوای رشته را نگه میدارد.
+
+
+
+شکل ۴-۱: نمایش در حافظه یک `String` که مقدار `"hello"` به `s1` متصل است
+
+طول مشخص میکند که محتوای `String` در حال حاضر چقدر حافظه به بایت استفاده میکند. ظرفیت مقدار کل حافظهای است که `String` از تخصیصدهنده دریافت کرده است. تفاوت بین طول و ظرفیت اهمیت دارد، اما نه در این زمینه، بنابراین در حال حاضر میتوان ظرفیت را نادیده گرفت.
+
+وقتی `s1` را به `s2` اختصاص میدهیم، دادههای `String` کپی میشوند، به این معنی که اشارهگر (Pointer)، طول، و ظرفیت موجود روی استک را کپی میکنیم. ما دادههای روی هیپ را که اشارهگر (Pointer) به آن اشاره میکند، کپی نمیکنیم. به عبارت دیگر، نمایش دادهها در حافظه به شکل ۴-۲ به نظر میرسد.
+
+
+
+شکل ۴-۲: نمایش در حافظه متغیر `s2` که یک کپی از اشارهگر (Pointer)، طول، و ظرفیت `s1` دارد
+
+نمایش دادهها _به این شکل_ نیست که در شکل ۴-۳ آمده است، که نشان میدهد حافظه به گونهای باشد که Rust همچنین دادههای هیپ را کپی کند. اگر Rust این کار را انجام میداد، عملیات `s2 = s1` میتوانست از نظر عملکرد زمان اجرا بسیار گران باشد اگر دادههای روی هیپ بزرگ بودند.
+
+
+
+شکل ۴-۳: یک امکان دیگر برای آنچه که `s2 = s1` ممکن است انجام دهد اگر Rust دادههای هیپ را نیز کپی کند
+
+قبلاً گفتیم که وقتی یک متغیر از دامنه خارج میشود، Rust به طور خودکار تابع `drop` را فراخوانی میکند و حافظه هیپ را برای آن متغیر پاکسازی میکند. اما شکل ۴-۲ نشان میدهد که هر دو اشارهگر (Pointer) دادهها به یک مکان اشاره میکنند. این یک مشکل است: وقتی `s2` و `s1` از دامنه خارج میشوند، هر دو سعی میکنند همان حافظه را آزاد کنند. این به عنوان یک خطای _آزادسازی دوباره_ شناخته میشود و یکی از مشکلات ایمنی حافظه است که قبلاً ذکر کردیم. آزادسازی حافظه دو بار میتواند منجر به خراب شدن حافظه شود، که به طور بالقوه میتواند منجر به آسیبپذیریهای امنیتی شود.
+
+برای اطمینان از ایمنی حافظه، پس از خط `let s2 = s1;`، Rust متغیر `s1` را دیگر معتبر نمیداند. بنابراین، Rust نیازی به آزادسازی هیچ چیزی ندارد وقتی `s1` از دامنه خارج میشود. بررسی کنید که وقتی سعی میکنید `s1` را پس از ایجاد `s2` استفاده کنید، چه اتفاقی میافتد؛ این کار جواب نمیدهد:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/src/main.rs:here}}
```
-You’ll get an error like this because Rust prevents you from using the
-invalidated reference:
+شما خطایی مشابه این دریافت خواهید کرد زیرا Rust از استفاده از مرجع نامعتبر جلوگیری میکند:
```console
{{#include ../listings/ch04-understanding-ownership/no-listing-04-cant-use-after-move/output.txt}}
```
-If you’ve heard the terms _shallow copy_ and _deep copy_ while working with
-other languages, the concept of copying the pointer, length, and capacity
-without copying the data probably sounds like making a shallow copy. But
-because Rust also invalidates the first variable, instead of being called a
-shallow copy, it’s known as a _move_. In this example, we would say that `s1`
-was _moved_ into `s2`. So, what actually happens is shown in Figure 4-4.
+اگر اصطلاحات _کپی سطحی_ و _کپی عمیق_ را هنگام کار با زبانهای دیگر شنیدهاید، مفهوم کپی کردن اشارهگر (Pointer)، طول، و ظرفیت بدون کپی کردن داده احتمالاً شبیه به انجام یک کپی سطحی است. اما به دلیل اینکه Rust همچنین متغیر اول را نامعتبر میکند، به جای اینکه آن را کپی سطحی بنامند، به عنوان یک _انتقال_ شناخته میشود. در این مثال، میتوانیم بگوییم که `s1` به `s2` _منتقل_ شده است. بنابراین، آنچه در واقع اتفاق میافتد در شکل ۴-۴ نشان داده شده است.
-
+
-Figure 4-4: Representation in memory after `s1` has been
-invalidated
+شکل ۴-۴: نمایش در حافظه پس از اینکه `s1` نامعتبر شده است
-That solves our problem! With only `s2` valid, when it goes out of scope it
-alone will free the memory, and we’re done.
+این مشکل ما را حل میکند! با تنها `s2` که معتبر است، وقتی از دامنه خارج میشود، تنها آن حافظه را آزاد خواهد کرد و کار ما تمام است.
-In addition, there’s a design choice that’s implied by this: Rust will never
-automatically create “deep” copies of your data. Therefore, any _automatic_
-copying can be assumed to be inexpensive in terms of runtime performance.
+علاوه بر این، یک انتخاب طراحی وجود دارد که از این نتیجهگیری میشود: Rust هرگز به طور خودکار "کپی عمیق" دادههای شما را ایجاد نمیکند. بنابراین، هر گونه کپی _خودکار_ میتواند بهعنوان عملی ارزان از نظر عملکرد زمان اجرا در نظر گرفته شود.
-#### Scope and Assignment
+#### دامنه و انتساب
-The inverse of this is true for the relationship between scoping, ownership, and
-memory being freed via the `drop` function as well. When you assign a completely
-new value to an existing variable, Rust will call `drop` and free the original
-value’s memory immediately. Consider this code, for example:
+عکس این رابطه بین دامنهبندی، مالکیت، و آزادسازی حافظه از طریق تابع `drop` نیز صحیح است. وقتی یک مقدار کاملاً جدید به یک متغیر موجود اختصاص میدهید، Rust تابع `drop` را فراخوانی میکند و حافظه مقدار اصلی را بلافاصله آزاد میکند. به این کد توجه کنید:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-04b-replacement-drop/src/main.rs:here}}
```
-We initially declare a variable `s` and bind it to a `String` with the value
-`"hello"`. Then we immediately create a new `String` with the value `"ahoy"` and
-assign it to `s`. At this point, nothing is referring to the original value on
-the heap at all.
+ابتدا یک متغیر `s` را اعلان میکنیم و آن را به یک `String` با مقدار `"hello"` اختصاص میدهیم. سپس بلافاصله یک `String` جدید با مقدار `"ahoy"` ایجاد میکنیم و آن را به `s` اختصاص میدهیم. در این نقطه، هیچ چیزی به مقدار اصلی روی هیپ اشاره نمیکند.
-
+
-Figure 4-5: Representation in memory after the initial
-value has been replaced in its entirety.
+شکل ۴-۵: نمایش در حافظه پس از اینکه مقدار اولیه به طور کامل جایگزین شده است.
-The original string thus immediately goes out of scope. Rust will run the `drop`
-function on it and its memory will be freed right away. When we print the value
-at the end, it will be `"ahoy, world!"`.
+رشته اصلی بلافاصله از دامنه خارج میشود. Rust تابع `drop` را روی آن اجرا میکند و حافظه آن بلافاصله آزاد میشود. وقتی مقدار را در انتها چاپ میکنیم، مقدار `"ahoy, world!"` خواهد بود.
-#### Variables and Data Interacting with Clone
+#### تعامل متغیرها و دادهها با Clone
-If we _do_ want to deeply copy the heap data of the `String`, not just the
-stack data, we can use a common method called `clone`. We’ll discuss method
-syntax in Chapter 5, but because methods are a common feature in many
-programming languages, you’ve probably seen them before.
+اگر بخواهیم دادههای هیپ `String` را عمیقاً کپی کنیم، نه فقط دادههای استک، میتوانیم از یک متد معمول به نام `clone` استفاده کنیم. ما نحو متدها را در فصل ۵ بررسی خواهیم کرد، اما از آنجا که متدها یک ویژگی رایج در بسیاری از زبانهای برنامهنویسی هستند، احتمالاً قبلاً آنها را دیدهاید.
-Here’s an example of the `clone` method in action:
+در اینجا یک مثال از روش `clone` در عمل آورده شده است:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-05-clone/src/main.rs:here}}
```
-This works just fine and explicitly produces the behavior shown in Figure 4-3,
-where the heap data _does_ get copied.
+این کد به خوبی کار میکند و به وضوح رفتار نشان داده شده در شکل ۴-۳ را تولید میکند، جایی که دادههای هیپ _کپی_ میشوند.
-When you see a call to `clone`, you know that some arbitrary code is being
-executed and that code may be expensive. It’s a visual indicator that something
-different is going on.
+وقتی یک فراخوانی به `clone` میبینید، میدانید که کدی دلخواه اجرا میشود و ممکن است این کد هزینهبر باشد. این یک شاخص بصری است که نشان میدهد چیزی متفاوت در حال رخ دادن است.
-#### Stack-Only Data: Copy
+#### دادههای فقط استک: Copy
-There’s another wrinkle we haven’t talked about yet. This code using
-integers—part of which was shown in Listing 4-2—works and is valid:
+یک نکته دیگر وجود دارد که هنوز درباره آن صحبت نکردهایم. این کد که از اعداد صحیح استفاده میکند - بخشی از آن در لیستینگ ۴-۲ نشان داده شده است - کار میکند و معتبر است:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-06-copy/src/main.rs:here}}
```
-But this code seems to contradict what we just learned: we don’t have a call to
-`clone`, but `x` is still valid and wasn’t moved into `y`.
-
-The reason is that types such as integers that have a known size at compile
-time are stored entirely on the stack, so copies of the actual values are quick
-to make. That means there’s no reason we would want to prevent `x` from being
-valid after we create the variable `y`. In other words, there’s no difference
-between deep and shallow copying here, so calling `clone` wouldn’t do anything
-different from the usual shallow copying, and we can leave it out.
-
-Rust has a special annotation called the `Copy` trait that we can place on
-types that are stored on the stack, as integers are (we’ll talk more about
-traits in [Chapter 10][traits]). If a type implements the `Copy`
-trait, variables that use it do not move, but rather are trivially copied,
-making them still valid after assignment to another variable.
-
-Rust won’t let us annotate a type with `Copy` if the type, or any of its parts,
-has implemented the `Drop` trait. If the type needs something special to happen
-when the value goes out of scope and we add the `Copy` annotation to that type,
-we’ll get a compile-time error. To learn about how to add the `Copy` annotation
-to your type to implement the trait, see [“Derivable
-Traits”][derivable-traits] in Appendix C.
-
-So, what types implement the `Copy` trait? You can check the documentation for
-the given type to be sure, but as a general rule, any group of simple scalar
-values can implement `Copy`, and nothing that requires allocation or is some
-form of resource can implement `Copy`. Here are some of the types that
-implement `Copy`:
-
-- All the integer types, such as `u32`.
-- The Boolean type, `bool`, with values `true` and `false`.
-- All the floating-point types, such as `f64`.
-- The character type, `char`.
-- Tuples, if they only contain types that also implement `Copy`. For example,
- `(i32, i32)` implements `Copy`, but `(i32, String)` does not.
-
-### Ownership and Functions
-
-The mechanics of passing a value to a function are similar to those when
-assigning a value to a variable. Passing a variable to a function will move or
-copy, just as assignment does. Listing 4-3 has an example with some annotations
-showing where variables go into and out of scope.
-
-
+اما این کد به نظر میرسد با آنچه که به تازگی یاد گرفتیم تناقض دارد: ما یک فراخوانی به `clone` نداریم، اما `x` همچنان معتبر است و به `y` منتقل نشده است.
+
+دلیل این است که انواعی مانند اعداد صحیح که اندازه مشخصی در زمان کامپایل دارند، به طور کامل روی استک ذخیره میشوند، بنابراین کپی کردن مقادیر واقعی سریع است. این به این معناست که هیچ دلیلی وجود ندارد که بخواهیم `x` پس از ایجاد متغیر `y` نامعتبر شود. به عبارت دیگر، در اینجا تفاوتی بین کپی عمیق و کپی سطحی وجود ندارد، بنابراین فراخوانی `clone` کاری متفاوت از کپی سطحی معمول انجام نمیدهد و میتوانیم آن را حذف کنیم.
+
+Rust دارای یک نشانهگذاری ویژه به نام ویژگی `Copy` است که میتوانیم روی انواعی که روی استک ذخیره میشوند (مانند اعداد صحیح) اعمال کنیم (ما در [فصل ۱۰][traits] بیشتر درباره ویژگیها صحبت خواهیم کرد). اگر یک نوع ویژگی `Copy` را پیادهسازی کند، متغیرهایی که از آن استفاده میکنند جابهجا نمیشوند، بلکه به سادگی کپی میشوند و پس از اختصاص به متغیر دیگری همچنان معتبر باقی میمانند.
+
+Rust به ما اجازه نمیدهد یک نوع را با `Copy` نشانهگذاری کنیم اگر نوع یا هر یک از اجزای آن ویژگی `Drop` را پیادهسازی کرده باشند. اگر نوع به چیزی خاص نیاز داشته باشد تا زمانی که مقدار از دامنه خارج شود و ما ویژگی `Copy` را به آن نوع اضافه کنیم، یک خطای زمان کامپایل دریافت خواهیم کرد. برای یادگیری نحوه افزودن ویژگی `Copy` به نوع خود برای پیادهسازی این ویژگی، به [“ویژگیهای قابل اشتقاق”][derivable-traits] در ضمیمه ج مراجعه کنید.
+
+پس، چه نوعهایی ویژگی `Copy` را پیادهسازی میکنند؟ میتوانید برای اطمینان، مستندات نوع داده شده را بررسی کنید، اما به عنوان یک قانون کلی، هر گروه از مقادیر ساده و اسکالر میتوانند ویژگی `Copy` را پیادهسازی کنند و هیچ چیزی که نیاز به تخصیص یا نوعی منبع داشته باشد نمیتواند ویژگی `Copy` را پیادهسازی کند. در اینجا تعدادی از انواعی که ویژگی `Copy` را پیادهسازی میکنند آورده شده است:
+
+- تمام انواع اعداد صحیح، مانند `u32`.
+- نوع بولی، `bool`، با مقادیر `true` و `false`.
+- تمام انواع اعشاری، مانند `f64`.
+- نوع کاراکتر، `char`.
+- تاپلها، اگر تنها شامل انواعی باشند که ویژگی `Copy` را نیز پیادهسازی میکنند. برای مثال، `(i32, i32)` ویژگی `Copy` را پیادهسازی میکند، اما `(i32, String)` این کار را نمیکند.
+
+### مالکیت و توابع
+
+مکانیزمهای انتقال یک مقدار به یک تابع مشابه زمانی است که مقداری را به یک متغیر اختصاص میدهیم. انتقال یک متغیر به یک تابع به همان صورت که تخصیص انجام میشود، جابهجا یا کپی میشود. لیستینگ ۴-۳ مثالی با برخی حاشیهنویسیها دارد که نشان میدهد متغیرها کجا وارد و از دامنه خارج میشوند.
+
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-03/src/main.rs}}
@@ -472,18 +304,13 @@ showing where variables go into and out of scope.
-If we tried to use `s` after the call to `takes_ownership`, Rust would throw a
-compile-time error. These static checks protect us from mistakes. Try adding
-code to `main` that uses `s` and `x` to see where you can use them and where
-the ownership rules prevent you from doing so.
+اگر بخواهیم از `s` پس از فراخوانی به `takes_ownership` استفاده کنیم، Rust یک خطای زمان کامپایل صادر میکند. این بررسیهای استاتیک ما را از اشتباهات محافظت میکنند. سعی کنید کدی به `main` اضافه کنید که از `s` و `x` استفاده کند تا ببینید کجا میتوانید از آنها استفاده کنید و کجا قوانین مالکیت مانع شما میشوند.
-### Return Values and Scope
+### مقادیر بازگشتی و دامنه
-Returning values can also transfer ownership. Listing 4-4 shows an example of a
-function that returns some value, with similar annotations as those in Listing
-4-3.
+بازگرداندن مقادیر نیز میتواند مالکیت را منتقل کند. لیستینگ ۴-۴ مثالی از یک تابع که مقداری را بازمیگرداند نشان میدهد، با حاشیهنویسیهایی مشابه آنچه در لیستینگ ۴-۳ وجود داشت.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-04/src/main.rs}}
@@ -491,20 +318,13 @@ function that returns some value, with similar annotations as those in Listing
-The ownership of a variable follows the same pattern every time: assigning a
-value to another variable moves it. When a variable that includes data on the
-heap goes out of scope, the value will be cleaned up by `drop` unless ownership
-of the data has been moved to another variable.
+مالکیت یک متغیر همیشه از یک الگوی یکسان پیروی میکند: تخصیص یک مقدار به متغیر دیگر آن را جابهجا میکند. زمانی که یک متغیر شامل دادههایی در هیپ از دامنه خارج میشود، مقدار با استفاده از `drop` پاکسازی میشود مگر اینکه مالکیت دادهها به متغیر دیگری منتقل شده باشد.
-While this works, taking ownership and then returning ownership with every
-function is a bit tedious. What if we want to let a function use a value but
-not take ownership? It’s quite annoying that anything we pass in also needs to
-be passed back if we want to use it again, in addition to any data resulting
-from the body of the function that we might want to return as well.
+در حالی که این روش کار میکند، گرفتن مالکیت و سپس بازگرداندن آن با هر تابع کمی خستهکننده است. اگر بخواهیم اجازه دهیم یک تابع از یک مقدار استفاده کند اما مالکیت آن را نگیرد، چه میشود؟ این که هر چیزی که به تابع ارسال میکنیم باید بازگردانده شود تا بتوانیم دوباره از آن استفاده کنیم، علاوه بر هر دادهای که از بدنه تابع ممکن است بخواهیم بازگردانیم، کمی آزاردهنده است.
-Rust does let us return multiple values using a tuple, as shown in Listing 4-5.
+Rust به ما اجازه میدهد مقادیر متعددی را با استفاده از یک tuple بازگردانیم، همانطور که در لیستینگ ۴-۵ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-05/src/main.rs}}
@@ -512,9 +332,7 @@ Rust does let us return multiple values using a tuple, as shown in Listing 4-5.
-But this is too much ceremony and a lot of work for a concept that should be
-common. Luckily for us, Rust has a feature for using a value without
-transferring ownership, called _references_.
+اما این کار بسیار رسمی و زمانبر است برای مفهومی که باید رایج باشد. خوشبختانه، Rust ویژگیای برای استفاده از یک مقدار بدون انتقال مالکیت دارد که _ارجاعات_ نامیده میشود.
[data-types]: ch03-02-data-types.html#data-types
[ch8]: ch08-02-strings.html
@@ -522,4 +340,5 @@ transferring ownership, called _references_.
[derivable-traits]: appendix-03-derivable-traits.html
[method-syntax]: ch05-03-method-syntax.html#method-syntax
[paths-module-tree]: ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html
-[drop]: ../std/ops/trait.Drop.html#tymethod.drop
+[drop]: https://doc.rust-lang.org/std/ops/trait.Drop.html#tymethod.drop
+
diff --git a/src/ch04-02-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md
index 6b92e53a8a..2df9160f4e 100644
--- a/src/ch04-02-references-and-borrowing.md
+++ b/src/ch04-02-references-and-borrowing.md
@@ -1,75 +1,56 @@
-## References and Borrowing
+## ارجاعات و قرض گرفتن (References and Borrowing)
-The issue with the tuple code in Listing 4-5 is that we have to return the
-`String` to the calling function so we can still use the `String` after the
-call to `calculate_length`, because the `String` was moved into
-`calculate_length`. Instead, we can provide a reference to the `String` value.
-A _reference_ is like a pointer in that it’s an address we can follow to access
-the data stored at that address; that data is owned by some other variable.
-Unlike a pointer, a reference is guaranteed to point to a valid value of a
-particular type for the life of that reference.
+مشکل کدی که در لیستینگ 4-5 با استفاده از تاپل وجود دارد این است که باید
+`String` را به تابع فراخوانیکننده بازگردانیم تا بعد از فراخوانی
+`calculate_length` بتوانیم همچنان از
+`String` استفاده کنیم، زیرا
+`String` به
+`calculate_length` منتقل شده است. در عوض، میتوانیم یک ارجاع به مقدار
+`String` ارائه دهیم. یک _ارجاع_ مشابه یک اشارهگر (Pointer) است، به این معنا که یک آدرس است که میتوانیم از آن پیروی کنیم تا به دادههایی که در آن آدرس ذخیره شدهاند دسترسی پیدا کنیم؛ این دادهها متعلق به متغیر دیگری هستند. برخلاف اشارهگر (Pointer)، یک ارجاع تضمین میکند که به یک مقدار معتبر از نوع خاصی در طول عمر آن ارجاع اشاره میکند.
-Here is how you would define and use a `calculate_length` function that has a
-reference to an object as a parameter instead of taking ownership of the value:
+در اینجا نحوه تعریف و استفاده از یک تابع
+`calculate_length` آورده شده است که به جای گرفتن مالکیت مقدار، یک ارجاع به یک شی به عنوان پارامتر دارد:
-
+Filename: src/main.rs
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs:all}}
```
-
-
-First, notice that all the tuple code in the variable declaration and the
-function return value is gone. Second, note that we pass `&s1` into
-`calculate_length` and, in its definition, we take `&String` rather than
-`String`. These ampersands represent _references_, and they allow you to refer
-to some value without taking ownership of it. Figure 4-6 depicts this concept.
+اول، توجه کنید که تمام کد مربوط به تاپل در اعلام متغیر و مقدار بازگشتی تابع حذف شده است. دوم، دقت کنید که ما
+`&s1` را به
+`calculate_length` میدهیم و در تعریف آن،
+`&String` میگیریم به جای
+`String`. این علامتهای & نمایندهی _ارجاعات_ هستند و به شما اجازه میدهند تا به مقداری اشاره کنید بدون اینکه مالکیت آن را بگیرید. شکل 4-6 این مفهوم را نشان میدهد.
-
+
-Figure 4-6: A diagram of `&String s` pointing at `String
-s1`
+شکل 4-6: نمودار `&String s` که به `String s1` اشاره میکند
-> Note: The opposite of referencing by using `&` is _dereferencing_, which is
-> accomplished with the dereference operator, `*`. We’ll see some uses of the
-> dereference operator in Chapter 8 and discuss details of dereferencing in
-> Chapter 15.
+> توجه: متضاد ارجاع دادن با استفاده از `&`، _عدم ارجاع_ است که با عملگر عدم ارجاع، یعنی `*`، انجام میشود. برخی از موارد استفاده از عملگر عدم ارجاع را در فصل 8 خواهیم دید و جزئیات مربوط به عدم ارجاع را در فصل 15 بحث خواهیم کرد.
-Let’s take a closer look at the function call here:
+بیایید نگاهی دقیقتر به فراخوانی تابع بیندازیم:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs:here}}
```
-The `&s1` syntax lets us create a reference that _refers_ to the value of `s1`
-but does not own it. Because the reference does not own it, the value it points
-to will not be dropped when the reference stops being used.
+سینتکس `&s1` به ما اجازه میدهد یک ارجاع ایجاد کنیم که به مقدار
+`s1` _اشاره میکند_ اما مالک آن نیست. از آنجایی که ارجاع مالک آن نیست، مقداری که به آن اشاره میکند زمانی که ارجاع استفاده نمیشود حذف نخواهد شد.
-Likewise, the signature of the function uses `&` to indicate that the type of
-the parameter `s` is a reference. Let’s add some explanatory annotations:
+به همین ترتیب، امضای تابع از `&` استفاده میکند تا نشان دهد که نوع پارامتر `s` یک ارجاع است. بیایید برخی توضیحات اضافه کنیم:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs:here}}
```
-The scope in which the variable `s` is valid is the same as any function
-parameter’s scope, but the value pointed to by the reference is not dropped
-when `s` stops being used, because `s` doesn’t have ownership. When functions
-have references as parameters instead of the actual values, we won’t need to
-return the values in order to give back ownership, because we never had
-ownership.
+دامنهای که متغیر `s` در آن معتبر است، مشابه دامنهی هر پارامتر تابع است، اما مقدار اشارهشده توسط ارجاع زمانی که `s` استفاده نمیشود حذف نمیشود، زیرا `s` مالکیت ندارد. وقتی توابع ارجاعات را به جای مقادیر واقعی به عنوان پارامتر دارند، نیازی نخواهیم داشت مقادیر را بازگردانیم تا مالکیت را بازگردانیم، زیرا هرگز مالکیتی نداشتهایم.
-We call the action of creating a reference _borrowing_. As in real life, if a
-person owns something, you can borrow it from them. When you’re done, you have
-to give it back. You don’t own it.
+ما عمل ایجاد یک ارجاع را _قرض گرفتن_ مینامیم. همانند زندگی واقعی، اگر شخصی چیزی را مالک باشد، شما میتوانید آن را از او قرض بگیرید. وقتی کارتان تمام شد، باید آن را بازگردانید. شما مالک آن نیستید.
-So, what happens if we try to modify something we’re borrowing? Try the code in
-Listing 4-6. Spoiler alert: it doesn’t work!
+پس چه اتفاقی میافتد اگر بخواهیم چیزی که قرض گرفتهایم را تغییر دهیم؟ کد موجود در لیستینگ 4-6 را امتحان کنید. هشدار: این کار نمیکند!
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-06/src/main.rs}}
@@ -77,19 +58,17 @@ Listing 4-6. Spoiler alert: it doesn’t work!
-Here’s the error:
+در اینجا خطا آورده شده است:
```console
{{#include ../listings/ch04-understanding-ownership/listing-04-06/output.txt}}
```
-Just as variables are immutable by default, so are references. We’re not
-allowed to modify something we have a reference to.
+همانطور که متغیرها به صورت پیشفرض غیرقابل تغییر هستند، ارجاعات نیز به همین صورت هستند. ما اجازه نداریم چیزی که به آن ارجاع داریم را تغییر دهیم.
-### Mutable References
+### ارجاعات متغیر
-We can fix the code from Listing 4-6 to allow us to modify a borrowed value
-with just a few small tweaks that use, instead, a _mutable reference_:
+ما میتوانیم کد موجود در لیستینگ 4-6 را طوری اصلاح کنیم که به ما اجازه دهد یک مقدار قرض گرفته شده را تغییر دهیم، با چند تغییر کوچک که به جای آن از _ارجاع متغیر_ استفاده کنیم:
@@ -99,14 +78,9 @@ with just a few small tweaks that use, instead, a _mutable reference_:
-First we change `s` to be `mut`. Then we create a mutable reference with `&mut
-s` where we call the `change` function, and update the function signature to
-accept a mutable reference with `some_string: &mut String`. This makes it very
-clear that the `change` function will mutate the value it borrows.
+ابتدا `s` را به `mut` تغییر میدهیم. سپس یک ارجاع متغیر با `&mut s` ایجاد میکنیم، جایی که تابع `change` را فراخوانی میکنیم، و امضای تابع را بهروزرسانی میکنیم تا یک ارجاع متغیر با `some_string: &mut String` بپذیرد. این بسیار واضح میکند که تابع `change` مقدار قرض گرفته شده را تغییر خواهد داد.
-Mutable references have one big restriction: if you have a mutable reference to
-a value, you can have no other references to that value. This code that
-attempts to create two mutable references to `s` will fail:
+ارجاعات متغیر یک محدودیت بزرگ دارند: اگر یک ارجاع متغیر به یک مقدار داشته باشید، نمیتوانید هیچ ارجاع دیگری به آن مقدار داشته باشید. این کد که تلاش میکند دو ارجاع متغیر به `s` ایجاد کند، ناموفق خواهد بود:
@@ -116,148 +90,102 @@ attempts to create two mutable references to `s` will fail:
-Here’s the error:
+در اینجا خطا آورده شده است:
```console
{{#include ../listings/ch04-understanding-ownership/no-listing-10-multiple-mut-not-allowed/output.txt}}
```
-This error says that this code is invalid because we cannot borrow `s` as
-mutable more than once at a time. The first mutable borrow is in `r1` and must
-last until it’s used in the `println!`, but between the creation of that
-mutable reference and its usage, we tried to create another mutable reference
-in `r2` that borrows the same data as `r1`.
+این خطا میگوید که این کد نامعتبر است زیرا نمیتوانیم `s` را به طور همزمان بیش از یک بار به صورت متغیر قرض بگیریم. اولین قرض متغیر در `r1` است و باید تا زمانی که در `println!` استفاده شود باقی بماند، اما بین ایجاد آن ارجاع متغیر و استفاده از آن، ما سعی کردیم یک ارجاع متغیر دیگر در `r2` ایجاد کنیم که همان دادهای را قرض میگیرد که `r1` نیز قرض گرفته است.
-The restriction preventing multiple mutable references to the same data at the
-same time allows for mutation but in a very controlled fashion. It’s something
-that new Rustaceans struggle with because most languages let you mutate
-whenever you’d like. The benefit of having this restriction is that Rust can
-prevent data races at compile time. A _data race_ is similar to a race
-condition and happens when these three behaviors occur:
+محدودیتی که از ایجاد چند ارجاع متغیر به دادههای یکسان به طور همزمان جلوگیری میکند، امکان تغییر دادهها را فراهم میکند اما به صورت بسیار کنترل شده. این چیزی است که تازهکاران زبان Rust ممکن است با آن مشکل داشته باشند زیرا اکثر زبانها به شما اجازه میدهند هر زمان که بخواهید دادهها را تغییر دهید. مزیت این محدودیت این است که Rust میتواند از مسابقات داده (_data race_) در زمان کامپایل جلوگیری کند. یک _مسابقه داده_ مشابه یک شرایط مسابقه (_race condition_) است و زمانی رخ میدهد که این سه رفتار اتفاق بیفتند:
-- Two or more pointers access the same data at the same time.
-- At least one of the pointers is being used to write to the data.
-- There’s no mechanism being used to synchronize access to the data.
+- دو یا چند اشارهگر (Pointer) به طور همزمان به دادههای یکسان دسترسی پیدا میکنند.
+- حداقل یکی از اشارهگر (Pointer)ها برای نوشتن در دادهها استفاده میشود.
+- هیچ مکانیزمی برای هماهنگ کردن دسترسی به دادهها استفاده نمیشود.
-Data races cause undefined behavior and can be difficult to diagnose and fix
-when you’re trying to track them down at runtime; Rust prevents this problem by
-refusing to compile code with data races!
+مسابقات داده باعث رفتار نامشخص میشوند و در زمان اجرای برنامه ممکن است یافتن و رفع آنها دشوار باشد؛ Rust با عدم کامپایل کدهای دارای مسابقات داده از این مشکل جلوگیری میکند!
-As always, we can use curly brackets to create a new scope, allowing for
-multiple mutable references, just not _simultaneous_ ones:
+همانطور که همیشه، میتوانیم از آکولادها برای ایجاد یک اسکوپ جدید استفاده کنیم که امکان وجود ارجاعات متغیر متعدد را فراهم میکند، اما نه به صورت _همزمان_:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-11-muts-in-separate-scopes/src/main.rs:here}}
```
-Rust enforces a similar rule for combining mutable and immutable references.
-This code results in an error:
+Rust یک قانون مشابه برای ترکیب ارجاعات متغیر و غیرمتغیر اعمال میکند. این کد منجر به خطا میشود:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/src/main.rs:here}}
```
-Here’s the error:
+در اینجا خطا آورده شده است:
```console
{{#include ../listings/ch04-understanding-ownership/no-listing-12-immutable-and-mutable-not-allowed/output.txt}}
```
-Whew! We _also_ cannot have a mutable reference while we have an immutable one
-to the same value.
+ای وای! ما _همچنین_ نمیتوانیم یک ارجاع متغیر داشته باشیم در حالی که یک ارجاع غیرمتغیر به همان مقدار داریم.
-Users of an immutable reference don’t expect the value to suddenly change out
-from under them! However, multiple immutable references are allowed because no
-one who is just reading the data has the ability to affect anyone else’s
-reading of the data.
+کاربرانی که از یک ارجاع غیرمتغیر استفاده میکنند، انتظار ندارند که مقدار به طور ناگهانی تغییر کند! با این حال، چندین ارجاع غیرمتغیر مجاز هستند زیرا هیچکسی که فقط دادهها را میخواند، نمیتواند خواندن دیگران را تحت تأثیر قرار دهد.
-Note that a reference’s scope starts from where it is introduced and continues
-through the last time that reference is used. For instance, this code will
-compile because the last usage of the immutable references is in the `println!`,
-before the mutable reference is introduced:
+توجه داشته باشید که اسکوپ یک ارجاع از جایی که معرفی میشود شروع شده و تا آخرین باری که از آن استفاده میشود ادامه دارد. به عنوان مثال، این کد کامپایل میشود زیرا آخرین استفاده از ارجاعات غیرمتغیر در `println!` است، قبل از اینکه ارجاع متغیر معرفی شود:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-13-reference-scope-ends/src/main.rs:here}}
```
-The scopes of the immutable references `r1` and `r2` end after the `println!`
-where they are last used, which is before the mutable reference `r3` is
-created. These scopes don’t overlap, so this code is allowed: the compiler can
-tell that the reference is no longer being used at a point before the end of
-the scope.
+اسکوپهای ارجاعات غیرمتغیر `r1` و `r2` بعد از `println!` که در آنجا آخرین بار استفاده شدهاند به پایان میرسند، که این قبل از ایجاد ارجاع متغیر `r3` است. این اسکوپها همپوشانی ندارند، بنابراین این کد مجاز است: کامپایلر میتواند تشخیص دهد که ارجاع دیگر در نقطهای قبل از پایان اسکوپ استفاده نمیشود.
-Even though borrowing errors may be frustrating at times, remember that it’s
-the Rust compiler pointing out a potential bug early (at compile time rather
-than at runtime) and showing you exactly where the problem is. Then you don’t
-have to track down why your data isn’t what you thought it was.
+حتی اگر خطاهای قرض گرفتن ممکن است گاهی اوقات ناامیدکننده باشند، به یاد داشته باشید که این کامپایلر Rust است که به شما نشان میدهد یک باگ بالقوه در اوایل (در زمان کامپایل به جای زمان اجرا) وجود دارد و دقیقا به شما میگوید مشکل کجاست. سپس نیازی نیست که پیگیری کنید چرا دادههای شما آن چیزی نیست که فکر میکردید.
-### Dangling References
+### ارجاعات آویزان
-In languages with pointers, it’s easy to erroneously create a _dangling
-pointer_—a pointer that references a location in memory that may have been
-given to someone else—by freeing some memory while preserving a pointer to that
-memory. In Rust, by contrast, the compiler guarantees that references will
-never be dangling references: if you have a reference to some data, the
-compiler will ensure that the data will not go out of scope before the
-reference to the data does.
+در زبانهایی که از اشارهگر (Pointer)ها استفاده میکنند، ایجاد اشتباه یک _اشارهگر (Pointer) آویزان_ آسان است—اشارهگر (Pointer)ی که به مکانی در حافظه اشاره میکند که ممکن است به شخص دیگری داده شده باشد—با آزاد کردن مقداری حافظه در حالی که اشارهگر (Pointer) به آن حافظه را حفظ میکنید. در Rust، برعکس، کامپایلر تضمین میکند که ارجاعات هرگز ارجاعات آویزان نخواهند بود: اگر به دادههایی ارجاع دارید، کامپایلر اطمینان میدهد که دادهها قبل از ارجاع به دادهها از محدوده خارج نمیشوند.
-Let’s try to create a dangling reference to see how Rust prevents them with a
-compile-time error:
+بیایید سعی کنیم یک ارجاع آویزان ایجاد کنیم تا ببینیم چگونه Rust با یک خطای زمان کامپایل از این اتفاق جلوگیری میکند:
-
+Filename: src/main.rs
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-14-dangling-reference/src/main.rs}}
```
-
-
-Here’s the error:
+در اینجا خطا آورده شده است:
```console
{{#include ../listings/ch04-understanding-ownership/no-listing-14-dangling-reference/output.txt}}
```
-This error message refers to a feature we haven’t covered yet: lifetimes. We’ll
-discuss lifetimes in detail in Chapter 10. But, if you disregard the parts
-about lifetimes, the message does contain the key to why this code is a problem:
+این پیام خطا به ویژگیای اشاره دارد که هنوز پوشش ندادهایم: طول عمرها (_lifetimes_). ما طول عمرها را به طور مفصل در فصل 10 مورد بحث قرار خواهیم داد. اما، اگر بخشهای مربوط به طول عمرها را نادیده بگیرید، پیام کلید مشکل این کد را بیان میکند:
```text
this function's return type contains a borrowed value, but there is no value
for it to be borrowed from
```
-Let’s take a closer look at exactly what’s happening at each stage of our
-`dangle` code:
+بیایید نگاهی دقیقتر به آنچه که در هر مرحله از کد `dangle` اتفاق میافتد بیندازیم:
-
+Filename: src/main.rs
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-15-dangling-reference-annotated/src/main.rs:here}}
```
-
-
-Because `s` is created inside `dangle`, when the code of `dangle` is finished,
-`s` will be deallocated. But we tried to return a reference to it. That means
-this reference would be pointing to an invalid `String`. That’s no good! Rust
-won’t let us do this.
+از آنجا که `s` داخل `dangle` ایجاد میشود، زمانی که کد `dangle` تمام میشود، `s` از محدوده خارج میشود و آزاد میگردد. اما ما سعی کردیم یک ارجاع به آن برگردانیم. این بدان معناست که این ارجاع به یک `String` نامعتبر اشاره میکند. این خوب نیست! Rust اجازه نمیدهد این کار را انجام دهیم.
-The solution here is to return the `String` directly:
+راهحل در اینجا این است که به جای آن `String` را به طور مستقیم برگردانید:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-16-no-dangle/src/main.rs:here}}
```
-This works without any problems. Ownership is moved out, and nothing is
-deallocated.
+این بدون هیچ مشکلی کار میکند. مالکیت به بیرون منتقل میشود و هیچ چیزی آزاد نمیشود.
-### The Rules of References
+### قوانین ارجاعات
-Let’s recap what we’ve discussed about references:
+بیایید آنچه درباره ارجاعات بحث کردیم را مرور کنیم:
-- At any given time, you can have _either_ one mutable reference _or_ any
- number of immutable references.
-- References must always be valid.
+- در هر زمان مشخص، میتوانید _یا_ یک ارجاع متغیر داشته باشید _یا_ هر تعداد ارجاع غیرمتغیر.
+- ارجاعات باید همیشه معتبر باشند.
-Next, we’ll look at a different kind of reference: slices.
+در مرحله بعد، به نوع دیگری از ارجاع خواهیم پرداخت: بخشها (_slices_).
diff --git a/src/ch04-03-slices.md b/src/ch04-03-slices.md
index b0e27697b0..15822c8100 100644
--- a/src/ch04-03-slices.md
+++ b/src/ch04-03-slices.md
@@ -1,34 +1,42 @@
-## The Slice Type
+## نوع Slice
-_Slices_ let you reference a contiguous sequence of elements in a
-[collection](ch08-00-common-collections.md). A slice is a kind
-of reference, so it does not have ownership.
+_Slices_ به شما اجازه میدهند که به یک دنبالهی متوالی
+از عناصر در یک [مجموعه](ch08-00-common-collections.md)
+ارجاع دهید. اسلایس نوعی ارجاع است، بنابراین مالکیت ندارد.
-Here’s a small programming problem: write a function that takes a string of
-words separated by spaces and returns the first word it finds in that string.
-If the function doesn’t find a space in the string, the whole string must be
-one word, so the entire string should be returned.
+یک مسئلهی کوچک برنامهنویسی داریم:
+تابعی بنویسید که یک رشته شامل کلمات جداشده
+با فاصله دریافت کند و اولین کلمهای را که در
+آن رشته پیدا میکند بازگرداند. اگر تابع
+فاصلهای در رشته نیابد، کل رشته یک کلمه محسوب
+میشود و باید کل رشته بازگردانده شود.
-> Note: For the purposes of introducing string slices, we are assuming ASCII
-> only in this section; a more thorough discussion of UTF-8 handling is in the
-> [“Storing UTF-8 Encoded Text with Strings”][strings] section
-> of Chapter 8.
+> نکته: برای معرفی اسلایسهای رشتهای در این بخش،
+> فرض بر این است که تنها با ASCII سروکار داریم؛
+> بحث جامعتر دربارهی مدیریت UTF-8 در بخش
+> [«ذخیره متن کدگذاریشده UTF-8 با رشتهها»][strings]
+> در فصل ۸ ارائه شده است.
-Let’s work through how we’d write the signature of this function without using
-slices, to understand the problem that slices will solve:
+بیایید بررسی کنیم چگونه امضای این تابع را بدون استفاده
+از اسلایسها مینویسیم تا مشکلهایی که اسلایسها حل میکنند را
+درک کنیم:
```rust,ignore
fn first_word(s: &String) -> ?
```
-The `first_word` function has a parameter of type `&String`. We don’t need
-ownership, so this is fine. (In idiomatic Rust, functions do not take ownership
-of their arguments unless they need to, and the reasons for that will become
-clear as we keep going.) But what should we return? We don’t really have a way
-to talk about *part* of a string. However, we could return the index of the end
-of the word, indicated by a space. Let’s try that, as shown in Listing 4-7.
+تابع `first_word` یک پارامتر از نوع `&String` دریافت میکند.
+ما نیازی به مالکیت نداریم، پس این کار درست است.
+(در Rust ایدئومیک، توابع معمولاً مالکیت آرگومانهای
+خود را نمیگیرند مگر اینکه واقعاً لازم باشد، و دلایل
+این موضوع با پیش رفتن توضیح داده خواهد شد.) اما
+چه چیزی باید بازگردانیم؟ در واقع راهی برای اشاره به
+*بخشی* از رشته نداریم. با این حال، میتوانیم اندیس
+پایان کلمه را که با یک فاصله مشخص میشود، بازگردانیم.
+بیایید این روش را امتحان کنیم، همانطور که در فهرست 4-7
+نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:here}}
@@ -36,50 +44,31 @@ of the word, indicated by a space. Let’s try that, as shown in Listing 4-7.
-Because we need to go through the `String` element by element and check whether
-a value is a space, we’ll convert our `String` to an array of bytes using the
-`as_bytes` method.
+زیرا ما نیاز داریم عنصر به عنصر از `String` عبور کنیم و بررسی کنیم که آیا یک مقدار فاصله است یا خیر، رشته خود را به یک آرایه از بایتها با استفاده از متد `as_bytes` تبدیل میکنیم.
```rust,ignore
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:as_bytes}}
```
-Next, we create an iterator over the array of bytes using the `iter` method:
+در مرحله بعد، یک iterator روی آرایه بایتها با استفاده از متد `iter` ایجاد میکنیم:
```rust,ignore
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:iter}}
```
-We’ll discuss iterators in more detail in [Chapter 13][ch13].
-For now, know that `iter` is a method that returns each element in a collection
-and that `enumerate` wraps the result of `iter` and returns each element as
-part of a tuple instead. The first element of the tuple returned from
-`enumerate` is the index, and the second element is a reference to the element.
-This is a bit more convenient than calculating the index ourselves.
+ما در [فصل 13][ch13] بیشتر درباره iterators بحث خواهیم کرد. فعلاً بدانید که `iter` یک متد است که هر عنصر در یک مجموعه را برمیگرداند و `enumerate` نتیجه `iter` را میپیچد و هر عنصر را به عنوان بخشی از یک tuple برمیگرداند. اولین عنصر tuple برگردانده شده از `enumerate` شاخص است و دومین عنصر ارجاع به عنصر است. این کار کمی راحتتر از محاسبه شاخص به صورت دستی است.
-Because the `enumerate` method returns a tuple, we can use patterns to
-destructure that tuple. We’ll be discussing patterns more in [Chapter
-6][ch6]. In the `for` loop, we specify a pattern that has `i`
-for the index in the tuple and `&item` for the single byte in the tuple.
-Because we get a reference to the element from `.iter().enumerate()`, we use
-`&` in the pattern.
+زیرا متد `enumerate` یک tuple برمیگرداند، میتوانیم از الگوها برای جدا کردن این tuple استفاده کنیم. ما در [فصل 6][ch6] بیشتر درباره الگوها صحبت خواهیم کرد. در حلقه `for`، الگویی مشخص میکنیم که `i` برای شاخص در tuple و `&item` برای بایت منفرد در tuple باشد. زیرا ما یک ارجاع به عنصر از `.iter().enumerate()` دریافت میکنیم، از `&` در الگو استفاده میکنیم.
-Inside the `for` loop, we search for the byte that represents the space by
-using the byte literal syntax. If we find a space, we return the position.
-Otherwise, we return the length of the string by using `s.len()`.
+داخل حلقه `for`، به دنبال بایتی که نماینده فاصله باشد میگردیم با استفاده از نحوه نوشتن بایت به صورت literale. اگر یک فاصله پیدا کردیم، موقعیت را برمیگردانیم. در غیر این صورت، طول رشته را با استفاده از `s.len()` برمیگردانیم.
```rust,ignore
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-07/src/main.rs:inside_for}}
```
-We now have a way to find out the index of the end of the first word in the
-string, but there’s a problem. We’re returning a `usize` on its own, but it’s
-only a meaningful number in the context of the `&String`. In other words,
-because it’s a separate value from the `String`, there’s no guarantee that it
-will still be valid in the future. Consider the program in Listing 4-8 that
-uses the `first_word` function from Listing 4-7.
+اکنون راهی برای یافتن شاخص انتهای اولین کلمه در رشته داریم، اما مشکلی وجود دارد. ما یک `usize` به تنهایی برمیگردانیم، اما این تنها یک عدد معنادار در زمینه `&String` است. به عبارت دیگر، زیرا این مقدار از `String` جدا است، هیچ تضمینی وجود ندارد که در آینده همچنان معتبر باشد. برنامهای که در لیستینگ 4-8 استفاده میشود و از تابع `first_word` از لیستینگ 4-7 استفاده میکند را در نظر بگیرید.
-
+
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-08/src/main.rs:here}}
@@ -87,59 +76,49 @@ uses the `first_word` function from Listing 4-7.
-This program compiles without any errors and would also do so if we used `word`
-after calling `s.clear()`. Because `word` isn’t connected to the state of `s`
-at all, `word` still contains the value `5`. We could use that value `5` with
-the variable `s` to try to extract the first word out, but this would be a bug
-because the contents of `s` have changed since we saved `5` in `word`.
+این برنامه بدون هیچ خطایی کامپایل میشود و حتی اگر `word` را بعد از فراخوانی `s.clear()` استفاده کنیم، همچنان درست کار خواهد کرد. زیرا `word` اصلاً به حالت `s` متصل نیست، `word` همچنان مقدار `5` را دارد. ما میتوانیم از مقدار `5` همراه با متغیر `s` استفاده کنیم تا تلاش کنیم اولین کلمه را استخراج کنیم، اما این یک باگ خواهد بود زیرا محتوای `s` از زمانی که `5` را در `word` ذخیره کردیم، تغییر کرده است.
-Having to worry about the index in `word` getting out of sync with the data in
-`s` is tedious and error prone! Managing these indices is even more brittle if
-we write a `second_word` function. Its signature would have to look like this:
+نگران هماهنگ نگه داشتن شاخص در `word` با دادههای موجود در `s` بودن، خستهکننده و مستعد خطاست! مدیریت این شاخصها حتی شکنندهتر میشود اگر بخواهیم یک تابع `second_word` بنویسیم. امضای آن باید به این صورت باشد:
```rust,ignore
fn second_word(s: &String) -> (usize, usize) {
```
-Now we’re tracking a starting _and_ an ending index, and we have even more
-values that were calculated from data in a particular state but aren’t tied to
-that state at all. We have three unrelated variables floating around that need
-to be kept in sync.
+حالا ما یک شاخص شروع و یک شاخص پایان را دنبال میکنیم و مقادیر بیشتری داریم که از دادهها در یک وضعیت خاص محاسبه شدهاند اما اصلاً به آن وضعیت مرتبط نیستند. ما سه متغیر نامرتبط داریم که باید همگام نگه داشته شوند.
-Luckily, Rust has a solution to this problem: string slices.
+خوشبختانه، Rust یک راهحل برای این مشکل دارد: برشهای رشتهای.
-### String Slices
+### برشهای رشتهای
+
+_string slice_ یک ارجاع به دنبالهای متوالی از عناصر
+یک `String` است و به این صورت نمایش داده میشود:
-A _string slice_ is a reference to a contiguous sequence of the elements of a
-`String`, and it looks like this:
```rust
{{#rustdoc_include ../listings/ch04-understanding-ownership/no-listing-17-slice/src/main.rs:here}}
```
-Rather than a reference to the entire `String`, `hello` is a reference to a
-portion of the `String`, specified in the extra `[0..5]` bit. We create slices
-using a range within brackets by specifying `[starting_index..ending_index]`,
-where _`starting_index`_ is the first position in the slice and _`ending_index`_
-is one more than the last position in the slice. Internally, the slice data
-structure stores the starting position and the length of the slice, which
-corresponds to _`ending_index`_ minus _`starting_index`_. So, in the case of `let
-world = &s[6..11];`, `world` would be a slice that contains a pointer to the
-byte at index 6 of `s` with a length value of `5`.
+بهجای یک رفرنس به کل `String`، مقدار `hello` یک رفرنس به بخشی از `String` است
+که در بخش اضافی `[0..5]` مشخص شده است.
+
+برای ساختن slice، از یک بازه در داخل براکتها استفاده میکنیم
+و آن را به صورت `[starting_index..ending_index]` مینویسیم؛
+که در آن، *`starting_index`* اولین موقعیت در slice است
+و *`ending_index`* یکی بیشتر از آخرین موقعیت در slice است.
+
+درونیسازی ساختار دادهی slice، موقعیت شروع و طول slice را ذخیره میکند
+که این طول برابر است با *`ending_index`* منهای *`starting_index`*.
+
+پس در مورد دستور `let world = &s[6..11];`، متغیر `world` یک slice خواهد بود
+که اشارهگری به بایت در اندیس ۶ از `s` دارد، به همراه یک مقدار طول برابر با `5`.
-Figure 4-7 shows this in a diagram.
+شکل 4-7 این موضوع را در یک نمودار نشان میدهد.
-
+
-Figure 4-7: String slice referring to part of a
-`String`
+شکل 4-7: برش رشتهای اشاره به بخشی از یک `String`
-With Rust’s `..` range syntax, if you want to start at index 0, you can drop
-the value before the two periods. In other words, these are equal:
+با استفاده از نحوی محدوده `..` در Rust، اگر میخواهید از شاخص 0 شروع کنید، میتوانید مقدار قبل از دو نقطه را حذف کنید. به عبارت دیگر، این دو معادل هستند:
```rust
let s = String::from("hello");
@@ -148,8 +127,7 @@ let slice = &s[0..2];
let slice = &s[..2];
```
-By the same token, if your slice includes the last byte of the `String`, you
-can drop the trailing number. That means these are equal:
+به همین ترتیب، اگر برش شما شامل آخرین بایت `String` باشد، میتوانید عدد پایانی را حذف کنید. این به این معناست که این دو معادل هستند:
```rust
let s = String::from("hello");
@@ -160,8 +138,7 @@ let slice = &s[3..len];
let slice = &s[3..];
```
-You can also drop both values to take a slice of the entire string. So these
-are equal:
+شما همچنین میتوانید هر دو مقدار را حذف کنید تا یک برش از کل رشته بگیرید. بنابراین این دو معادل هستند:
```rust
let s = String::from("hello");
@@ -172,12 +149,11 @@ let slice = &s[0..len];
let slice = &s[..];
```
-> Note: String slice range indices must occur at valid UTF-8 character
-> boundaries. If you attempt to create a string slice in the middle of a
-> multibyte character, your program will exit with an error.
+> توجه: اندیسهای بازهی slice برای `String` باید در مرزهای معتبر کاراکترهای UTF-8 قرار داشته باشند.
+> اگر سعی کنید یک slice از رشته را در میانهی یک کاراکتر چندبایتی ایجاد کنید،
+> برنامهی شما با خطا متوقف خواهد شد.
-With all this information in mind, let’s rewrite `first_word` to return a
-slice. The type that signifies “string slice” is written as `&str`:
+با در نظر گرفتن این اطلاعات، بیایید `first_word` را بازنویسی کنیم تا یک برش برگرداند. نوعی که نشاندهنده "برش رشتهای" است به صورت `&str` نوشته میشود:
@@ -187,30 +163,17 @@ slice. The type that signifies “string slice” is written as `&str`:
-We get the index for the end of the word the same way we did in Listing 4-7, by
-looking for the first occurrence of a space. When we find a space, we return a
-string slice using the start of the string and the index of the space as the
-starting and ending indices.
+ما شاخص پایان کلمه را به همان روشی که در لیستینگ 4-7 انجام دادیم، پیدا میکنیم، یعنی با جستجوی اولین فضای خالی. وقتی یک فضای خالی پیدا میکنیم، یک برش رشتهای با استفاده از شروع رشته و شاخص فضای خالی بهعنوان شاخصهای شروع و پایان برمیگردانیم.
-Now when we call `first_word`, we get back a single value that is tied to the
-underlying data. The value is made up of a reference to the starting point of
-the slice and the number of elements in the slice.
+اکنون وقتی `first_word` را فراخوانی میکنیم، یک مقدار واحد دریافت میکنیم که به دادههای پایه متصل است. این مقدار شامل یک ارجاع به نقطه شروع برش و تعداد عناصر موجود در برش است.
-Returning a slice would also work for a `second_word` function:
+بازگرداندن یک برش برای یک تابع `second_word` نیز کار میکند:
```rust,ignore
fn second_word(s: &String) -> &str {
```
-We now have a straightforward API that’s much harder to mess up because the
-compiler will ensure the references into the `String` remain valid. Remember
-the bug in the program in Listing 4-8, when we got the index to the end of the
-first word but then cleared the string so our index was invalid? That code was
-logically incorrect but didn’t show any immediate errors. The problems would
-show up later if we kept trying to use the first word index with an emptied
-string. Slices make this bug impossible and let us know we have a problem with
-our code much sooner. Using the slice version of `first_word` will throw a
-compile-time error:
+اکنون یک API ساده داریم که بسیار سختتر است اشتباه شود زیرا کامپایلر اطمینان حاصل میکند که ارجاعها به داخل `String` معتبر باقی میمانند. به یاد دارید خطای منطقی برنامه در لیستینگ 4-8، وقتی شاخص انتهای اولین کلمه را به دست آوردیم اما سپس رشته را پاک کردیم، بنابراین شاخص ما نامعتبر شد؟ آن کد منطقی نادرست بود اما هیچ خطای فوری نشان نمیداد. مشکلات بعداً وقتی تلاش میکردیم از شاخص اولین کلمه با یک رشته خالی استفاده کنیم، ظاهر میشد. برشها این خطا را غیرممکن میکنند و به ما اطلاع میدهند که مشکلی در کد ما وجود دارد خیلی زودتر. استفاده از نسخه برش `first_word` یک خطای زمان کامپایل ایجاد میکند:
@@ -220,52 +183,39 @@ compile-time error:
-Here’s the compiler error:
+این هم خطای کامپایلر:
```console
{{#include ../listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt}}
```
-Recall from the borrowing rules that if we have an immutable reference to
-something, we cannot also take a mutable reference. Because `clear` needs to
-truncate the `String`, it needs to get a mutable reference. The `println!`
-after the call to `clear` uses the reference in `word`, so the immutable
-reference must still be active at that point. Rust disallows the mutable
-reference in `clear` and the immutable reference in `word` from existing at the
-same time, and compilation fails. Not only has Rust made our API easier to use,
-but it has also eliminated an entire class of errors at compile time!
+به یاد بیاورید از قوانین وام گرفتن که اگر ما یک ارجاع غیرقابل تغییر به چیزی داشته باشیم، نمیتوانیم یک ارجاع قابل تغییر نیز بگیریم. از آنجایی که `clear` نیاز دارد که `String` را کوتاه کند، نیاز دارد یک ارجاع قابل تغییر بگیرد. `println!` بعد از فراخوانی به `clear` از ارجاع در `word` استفاده میکند، بنابراین ارجاع غیرقابل تغییر باید هنوز در آن نقطه فعال باشد. Rust ارجاع قابل تغییر در `clear` و ارجاع غیرقابل تغییر در `word` را از همزمان وجود داشتن ممنوع میکند و کامپایل شکست میخورد. نه تنها Rust API ما را آسانتر کرده، بلکه یک دسته کامل از خطاها را در زمان کامپایل حذف کرده است!
-
+
-#### String Literals as Slices
+#### رشتههای متنی به عنوان برش
-Recall that we talked about string literals being stored inside the binary. Now
-that we know about slices, we can properly understand string literals:
+به یاد بیاورید که ما درباره ذخیره رشتههای متنی در داخل باینری صحبت کردیم. اکنون که درباره برشها میدانیم، میتوانیم رشتههای متنی را به درستی درک کنیم:
```rust
let s = "Hello, world!";
```
-The type of `s` here is `&str`: it’s a slice pointing to that specific point of
-the binary. This is also why string literals are immutable; `&str` is an
-immutable reference.
+نوع `s` در اینجا `&str` است: این یک برش است که به یک نقطه خاص از باینری اشاره میکند. این همچنین دلیل غیرقابل تغییر بودن رشتههای متنی است؛ `&str` یک ارجاع غیرقابل تغییر است.
-#### String Slices as Parameters
+#### برشهای رشتهای به عنوان پارامترها
-Knowing that you can take slices of literals and `String` values leads us to
-one more improvement on `first_word`, and that’s its signature:
+دانستن اینکه میتوانید برشهایی از رشتههای متنی و مقادیر `String` بگیرید ما را به یک بهبود دیگر در `first_word` میرساند، و آن امضای آن است:
```rust,ignore
fn first_word(s: &String) -> &str {
```
-A more experienced Rustacean would write the signature shown in Listing 4-9
-instead because it allows us to use the same function on both `&String` values
-and `&str` values.
+یک برنامهنویس باتجربهتر Rust امضای نشان داده شده در لیستینگ 4-9 را مینویسد زیرا این اجازه را میدهد که از همان تابع برای مقادیر `&String` و `&str` استفاده کنیم.
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch04-understanding-ownership/listing-04-09/src/main.rs:here}}
@@ -273,14 +223,9 @@ and `&str` values.
-If we have a string slice, we can pass that directly. If we have a `String`, we
-can pass a slice of the `String` or a reference to the `String`. This
-flexibility takes advantage of _deref coercions_, a feature we will cover in the
-[“Implicit Deref Coercions with Functions and
-Methods”][deref-coercions] section of Chapter 15.
+اگر ما یک برش رشتهای داشته باشیم، میتوانیم آن را مستقیماً ارسال کنیم. اگر یک `String` داشته باشیم، میتوانیم یک برش از `String` یا یک ارجاع به `String` ارسال کنیم. این انعطافپذیری از ویژگی _دریف کوئرسین_ استفاده میکند، که در بخش [“Implicit Deref Coercions with Functions and Methods”][deref-coercions] در فصل 15 به آن خواهیم پرداخت.
-Defining a function to take a string slice instead of a reference to a `String`
-makes our API more general and useful without losing any functionality:
+تعریف یک تابع برای گرفتن یک برش رشتهای به جای یک ارجاع به `String`، API ما را عمومیتر و مفیدتر میکند بدون اینکه هیچ کاربردی از دست برود:
@@ -290,17 +235,15 @@ makes our API more general and useful without losing any functionality:
-### Other Slices
+### برشهای دیگر
-String slices, as you might imagine, are specific to strings. But there’s a
-more general slice type too. Consider this array:
+برشهای رشتهای، همانطور که تصور میکنید، مختص رشتهها هستند. اما یک نوع برش عمومیتر نیز وجود دارد. این آرایه را در نظر بگیرید:
```rust
let a = [1, 2, 3, 4, 5];
```
-Just as we might want to refer to part of a string, we might want to refer to
-part of an array. We’d do so like this:
+همانطور که ممکن است بخواهیم به بخشی از یک رشته ارجاع دهیم، ممکن است بخواهیم به بخشی از یک آرایه نیز ارجاع دهیم. این کار را میتوانیم به این شکل انجام دهیم:
```rust
let a = [1, 2, 3, 4, 5];
@@ -310,22 +253,13 @@ let slice = &a[1..3];
assert_eq!(slice, &[2, 3]);
```
-This slice has the type `&[i32]`. It works the same way as string slices do, by
-storing a reference to the first element and a length. You’ll use this kind of
-slice for all sorts of other collections. We’ll discuss these collections in
-detail when we talk about vectors in Chapter 8.
+این برش دارای نوع `&[i32]` است. این دقیقاً همانطور که برشهای رشتهای کار میکنند، با ذخیره یک ارجاع به اولین عنصر و یک طول عمل میکند. شما از این نوع برش برای انواع دیگر مجموعهها نیز استفاده خواهید کرد. ما این مجموعهها را به تفصیل وقتی درباره وکتورها در فصل 8 صحبت کنیم، بررسی خواهیم کرد.
-## Summary
+## خلاصه
-The concepts of ownership, borrowing, and slices ensure memory safety in Rust
-programs at compile time. The Rust language gives you control over your memory
-usage in the same way as other systems programming languages, but having the
-owner of data automatically clean up that data when the owner goes out of scope
-means you don’t have to write and debug extra code to get this control.
+مفاهیم مالکیت، وام گرفتن، و برشها، ایمنی حافظه را در برنامههای Rust در زمان کامپایل تضمین میکنند. زبان Rust به شما همان کنترلی بر استفاده از حافظه میدهد که سایر زبانهای برنامهنویسی سیستم ارائه میدهند، اما این واقعیت که مالک داده به طور خودکار آن داده را هنگامی که مالک از حوزه خارج میشود، پاکسازی میکند، به این معنی است که نیازی به نوشتن و اشکالزدایی کد اضافی برای دستیابی به این کنترل ندارید.
-Ownership affects how lots of other parts of Rust work, so we’ll talk about
-these concepts further throughout the rest of the book. Let’s move on to
-Chapter 5 and look at grouping pieces of data together in a `struct`.
+مالکیت بر نحوه عملکرد بسیاری از بخشهای دیگر Rust تأثیر میگذارد، بنابراین در طول بقیه کتاب این مفاهیم را بیشتر بررسی خواهیم کرد. بیایید به فصل 5 برویم و نگاهی به گروهبندی قطعات داده در یک `struct` بیندازیم.
[ch13]: ch13-02-iterators.html
[ch6]: ch06-02-match.html#patterns-that-bind-to-values
diff --git a/src/ch05-00-structs.md b/src/ch05-00-structs.md
index ee064224d9..85f46dde65 100644
--- a/src/ch05-00-structs.md
+++ b/src/ch05-00-structs.md
@@ -1,14 +1,5 @@
-# Using Structs to Structure Related Data
+# استفاده از Structها برای سازماندهی دادههای مرتبط
-A _struct_, or _structure_, is a custom data type that lets you package
-together and name multiple related values that make up a meaningful group. If
-you’re familiar with an object-oriented language, a _struct_ is like an
-object’s data attributes. In this chapter, we’ll compare and contrast tuples
-with structs to build on what you already know and demonstrate when structs are
-a better way to group data.
+یک _struct_ یا _ساختار_، نوع دادهای سفارشی است که به شما اجازه میدهد چندین مقدار مرتبط را به صورت گروهی در کنار هم بستهبندی و نامگذاری کنید. اگر با یک زبان برنامهنویسی شیءگرا آشنا باشید، یک _struct_ شبیه به ویژگیهای دادهای یک شیء است. در این فصل، ما ساختارها را با تاپلها مقایسه و مقایسه خواهیم کرد تا نشان دهیم چه زمانی ساختارها روش بهتری برای گروهبندی دادهها هستند.
-We’ll demonstrate how to define and instantiate structs. We’ll discuss how to
-define associated functions, especially the kind of associated functions called
-_methods_, to specify behavior associated with a struct type. Structs and enums
-(discussed in Chapter 6) are the building blocks for creating new types in your
-program’s domain to take full advantage of Rust’s compile-time type checking.
+ما نحوه تعریف و نمونهسازی ساختارها را نشان خواهیم داد. همچنین بحث خواهیم کرد که چگونه توابع مرتبط، بهویژه نوعی از توابع مرتبط به نام _متدها_ را تعریف کنیم تا رفتار مرتبط با یک نوع ساختار را مشخص کنیم. ساختارها و Enumها (که در فصل ۶ مورد بحث قرار گرفتهاند) بلوکهای سازندهای برای ایجاد انواع جدید در حوزه برنامه شما هستند که از بررسی نوع در زمان کامپایل در Rust به طور کامل استفاده میکنند.
diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md
index 52f3118fa3..861c543885 100644
--- a/src/ch05-01-defining-structs.md
+++ b/src/ch05-01-defining-structs.md
@@ -1,19 +1,10 @@
-## Defining and Instantiating Structs
+## تعریف و نمونهسازی Structها
-Structs are similar to tuples, discussed in [“The Tuple Type”][tuples] section, in that both hold multiple related values. Like tuples, the
-pieces of a struct can be different types. Unlike with tuples, in a struct
-you’ll name each piece of data so it’s clear what the values mean. Adding these
-names means that structs are more flexible than tuples: you don’t have to rely
-on the order of the data to specify or access the values of an instance.
+ساختارها مشابه تاپلها هستند که در بخش [«نوع Tuple»][tuples] مورد بحث قرار گرفتند، به این معنا که هر دو شامل مقادیر مرتبط متعددی هستند. مانند تاپلها، اجزای یک ساختار میتوانند از انواع مختلفی باشند. اما برخلاف تاپلها، در یک ساختار شما برای هر جزء داده نام تعیین میکنید تا معنای مقادیر روشنتر شود. افزودن این نامها باعث میشود که ساختارها از تاپلها انعطافپذیرتر باشند: شما مجبور نیستید برای مشخص کردن یا دسترسی به مقادیر یک نمونه به ترتیب دادهها تکیه کنید.
-To define a struct, we enter the keyword `struct` and name the entire struct. A
-struct’s name should describe the significance of the pieces of data being
-grouped together. Then, inside curly brackets, we define the names and types of
-the pieces of data, which we call _fields_. For example, Listing 5-1 shows a
-struct that stores information about a user account.
+برای تعریف یک ساختار، کلمه کلیدی `struct` را وارد کرده و نام کل ساختار را تعیین میکنیم. نام یک ساختار باید توصیفکننده اهمیت اجزای دادهای باشد که با هم گروهبندی میشوند. سپس، داخل آکولادها، نامها و انواع اجزای دادهای را که به آنها _فیلد_ میگوییم، تعریف میکنیم. برای مثال، لیست ۵-۱ یک ساختار را نشان میدهد که اطلاعات مربوط به یک حساب کاربری را ذخیره میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-01/src/main.rs:here}}
@@ -21,17 +12,21 @@ struct that stores information about a user account.
-To use a struct after we’ve defined it, we create an _instance_ of that struct
-by specifying concrete values for each of the fields. We create an instance by
-stating the name of the struct and then add curly brackets containing _`key:
-value`_ pairs, where the keys are the names of the fields and the values are the
-data we want to store in those fields. We don’t have to specify the fields in
-the same order in which we declared them in the struct. In other words, the
-struct definition is like a general template for the type, and instances fill
-in that template with particular data to create values of the type. For
-example, we can declare a particular user as shown in Listing 5-2.
+برای استفاده از یک `struct` پس از تعریف آن، باید یک *instance* از آن ایجاد کنیم
+با مشخص کردن مقادیر مشخص برای هر یک از فیلدها.
-
+برای ساختن یک instance، نام `struct` را مینویسیم
+و سپس داخل کروشهها جفتهای *`کلید: مقدار`* قرار میدهیم؛
+که در آنها، کلیدها نام فیلدها هستند و مقادیر، دادههایی هستند که میخواهیم در آن فیلدها ذخیره کنیم.
+
+لازم نیست فیلدها را به همان ترتیبی بنویسیم که در تعریف struct آمدهاند.
+
+به عبارت دیگر، تعریف `struct` مانند یک الگوی کلی برای نوع داده است
+و instanceها آن الگو را با دادههای مشخص پر میکنند تا مقادیر آن نوع را بسازند.
+
+برای نمونه، میتوانیم یک کاربر خاص را همانطور که در لیست ۵-۲ نشان داده شده تعریف کنیم.
+
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-02/src/main.rs:here}}
@@ -39,13 +34,9 @@ example, we can declare a particular user as shown in Listing 5-2.
-To get a specific value from a struct, we use dot notation. For example, to
-access this user’s email address, we use `user1.email`. If the instance is
-mutable, we can change a value by using the dot notation and assigning into a
-particular field. Listing 5-3 shows how to change the value in the `email`
-field of a mutable `User` instance.
+برای بهدستآوردن مقدار خاصی از یک ساختار، از نشانهگذاری نقطه استفاده میکنیم. به عنوان مثال، برای دسترسی به آدرس ایمیل این کاربر، از `user1.email` استفاده میکنیم. اگر نمونه قابل تغییر باشد، میتوانیم مقدار را با استفاده از نشانهگذاری نقطه تغییر داده و در یک فیلد خاص مقداردهی کنیم. لیست ۵-۳ نشان میدهد که چگونه مقدار در فیلد `email` یک نمونه قابل تغییر `User` را تغییر دهیم.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-03/src/main.rs:here}}
@@ -53,16 +44,11 @@ field of a mutable `User` instance.
-Note that the entire instance must be mutable; Rust doesn’t allow us to mark
-only certain fields as mutable. As with any expression, we can construct a new
-instance of the struct as the last expression in the function body to
-implicitly return that new instance.
+توجه داشته باشید که کل نمونه باید قابل تغییر باشد؛ Rust به ما اجازه نمیدهد که فقط برخی از فیلدها را به صورت قابل تغییر علامتگذاری کنیم. مانند هر عبارت دیگری، میتوانیم یک نمونه جدید از ساختار را به عنوان آخرین عبارت در بدنه یک تابع بسازیم تا به طور ضمنی آن نمونه جدید را بازگردانیم.
-Listing 5-4 shows a `build_user` function that returns a `User` instance with
-the given email and username. The `active` field gets the value of `true`, and
-the `sign_in_count` gets a value of `1`.
+لیست ۵-۴ یک تابع `build_user` را نشان میدهد که یک نمونه از `User` را با ایمیل و نام کاربری مشخص برمیگرداند. فیلد `active` مقدار `true` میگیرد و `sign_in_count` مقدار `1` دریافت میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-04/src/main.rs:here}}
@@ -70,23 +56,15 @@ the `sign_in_count` gets a value of `1`.
-It makes sense to name the function parameters with the same name as the struct
-fields, but having to repeat the `email` and `username` field names and
-variables is a bit tedious. If the struct had more fields, repeating each name
-would get even more annoying. Luckily, there’s a convenient shorthand!
-
-
+نوشتن نام پارامترهای تابع با همان نام فیلدهای ساختار منطقی است، اما تکرار نامهای `email` و `username` برای هر دو فیلد و متغیرها کمی خستهکننده است. اگر ساختار فیلدهای بیشتری داشت، تکرار هر نام حتی آزاردهندهتر میشد. خوشبختانه، یک راه میانبر راحت وجود دارد!
-### Using the Field Init Shorthand
+### استفاده از میانبر مقداردهی فیلد
-Because the parameter names and the struct field names are exactly the same in
-Listing 5-4, we can use the _field init shorthand_ syntax to rewrite
-`build_user` so it behaves exactly the same but doesn’t have the repetition of
-`username` and `email`, as shown in Listing 5-5.
+از آنجا که نام پارامترها و نام فیلدهای ساختار دقیقاً یکسان هستند، میتوانیم از نحو _میانبر مقداردهی فیلد_ برای بازنویسی `build_user` استفاده کنیم تا همان رفتار را داشته باشد اما تکرار `username` و `email` را نداشته باشد، همانطور که در لیست ۵-۵ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-05/src/main.rs:here}}
@@ -94,23 +72,19 @@ Listing 5-4, we can use the _field init shorthand_ syntax to rewrite
-Here, we’re creating a new instance of the `User` struct, which has a field
-named `email`. We want to set the `email` field’s value to the value in the
-`email` parameter of the `build_user` function. Because the `email` field and
-the `email` parameter have the same name, we only need to write `email` rather
-than `email: email`.
+اینجا، ما یک نمونه جدید از ساختار `User` میسازیم که فیلدی به نام `email` دارد. ما میخواهیم مقدار فیلد `email` را به مقداری که در پارامتر `email` تابع `build_user` وجود دارد تنظیم کنیم. از آنجا که فیلد `email` و پارامتر `email` نام یکسانی دارند، فقط نیاز داریم `email` بنویسیم، نه `email: email`.
+
+### ایجاد نمونهها از نمونههای دیگر با استفاده از نحو بهروزرسانی Struct
-### Creating Instances from Other Instances with Struct Update Syntax
+اغلب مفید است که یک instance جدید از یک `struct` ایجاد کنیم
+که بیشتر مقادیر آن از یک instance دیگر با همان نوع گرفته شده باشد،
+اما برخی از مقادیر آن تغییر کرده باشند.
+برای انجام این کار میتوانید از *syntax بهروزرسانی struct* استفاده کنید.
-It’s often useful to create a new instance of a struct that includes most of
-the values from another instance of the same type, but changes some. You can do
-this using _struct update syntax_.
-First, in Listing 5-6 we show how to create a new `User` instance in `user2`
-regularly, without the update syntax. We set a new value for `email` but
-otherwise use the same values from `user1` that we created in Listing 5-2.
+ابتدا، در لیست ۵-۶ نشان داده شده است که چگونه میتوان یک نمونه جدید `User` در `user2` ایجاد کرد، بدون استفاده از نحو بهروزرسانی. ما یک مقدار جدید برای `email` تنظیم میکنیم اما در غیر این صورت از همان مقادیر در `user1` که قبلاً در لیست ۵-۲ ایجاد شده است، استفاده میکنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs:here}}
@@ -118,11 +92,9 @@ otherwise use the same values from `user1` that we created in Listing 5-2.
-Using struct update syntax, we can achieve the same effect with less code, as
-shown in Listing 5-7. The syntax `..` specifies that the remaining fields not
-explicitly set should have the same value as the fields in the given instance.
+با استفاده از نحو بهروزرسانی Struct، میتوانیم همان نتیجه را با کد کمتری به دست آوریم، همانطور که در لیست ۵-۷ نشان داده شده است. نحو `..` مشخص میکند که فیلدهای باقیماندهای که به صورت صریح تنظیم نشدهاند باید همان مقادیری را داشته باشند که در نمونه داده شده هستند.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs:here}}
@@ -130,38 +102,26 @@ explicitly set should have the same value as the fields in the given instance.
-The code in Listing 5-7 also creates an instance in `user2` that has a
-different value for `email` but has the same values for the `username`,
-`active`, and `sign_in_count` fields from `user1`. The `..user1` must come last
-to specify that any remaining fields should get their values from the
-corresponding fields in `user1`, but we can choose to specify values for as
-many fields as we want in any order, regardless of the order of the fields in
-the struct’s definition.
-
-Note that the struct update syntax uses `=` like an assignment; this is because
-it moves the data, just as we saw in the [“Variables and Data Interacting with
-Move”][move] section. In this example, we can no longer use
-`user1` after creating `user2` because the `String` in the `username` field of
-`user1` was moved into `user2`. If we had given `user2` new `String` values for
-both `email` and `username`, and thus only used the `active` and `sign_in_count`
-values from `user1`, then `user1` would still be valid after creating `user2`.
-Both `active` and `sign_in_count` are types that implement the `Copy` trait, so
-the behavior we discussed in the [“Stack-Only Data: Copy”][copy]
-section would apply. We can also still use `user1.email` in this example,
-because its value was not moved out of `user1`.
-
-### Using Tuple Structs Without Named Fields to Create Different Types
-
-Rust also supports structs that look similar to tuples, called _tuple structs_.
-Tuple structs have the added meaning the struct name provides but don’t have
-names associated with their fields; rather, they just have the types of the
-fields. Tuple structs are useful when you want to give the whole tuple a name
-and make the tuple a different type from other tuples, and when naming each
-field as in a regular struct would be verbose or redundant.
-
-To define a tuple struct, start with the `struct` keyword and the struct name
-followed by the types in the tuple. For example, here we define and use two
-tuple structs named `Color` and `Point`:
+کد در لیست ۵-۷ همچنین نمونهای در `user2` ایجاد میکند که مقدار متفاوتی برای `email` دارد اما دارای مقادیر مشابهی برای فیلدهای `username`، `active` و `sign_in_count` از `user1` است. `..user1` باید در انتها بیاید تا مشخص کند که فیلدهای باقیمانده باید مقادیر خود را از فیلدهای مربوطه در `user1` دریافت کنند، اما میتوانیم مقادیر را برای هر تعداد فیلدی که میخواهیم به هر ترتیبی مشخص کنیم، بدون توجه به ترتیب فیلدها در تعریف ساختار.
+
+
+توجه داشته باشید که نحو بهروزرسانی struct از `=` مانند عمل انتساب استفاده میکند؛
+زیرا داده را *منتقل* میکند، همانطور که در بخش [«تعامل متغیرها و دادهها با Move»][move] دیدیم.
+در این مثال، پس از ایجاد `user2` دیگر نمیتوانیم از `user1` استفاده کنیم
+چون `String` موجود در فیلد `username` از `user1` به `user2` منتقل شده است.
+اگر برای `user2` مقادیر جدیدی از نوع `String` برای هر دو فیلد `email` و `username` مشخص کرده بودیم
+و تنها از مقادیر `active` و `sign_in_count` از `user1` استفاده کرده بودیم،
+آنگاه `user1` پس از ساختن `user2` همچنان معتبر باقی میماند.
+زیرا `active` و `sign_in_count` از نوعهایی هستند که `Copy` trait را پیادهسازی میکنند،
+و بنابراین رفتاری که در بخش [«دادههای فقط-پشته: Copy»][copy] توضیح دادیم، اعمال میشود.
+در این مثال، همچنان میتوانیم از `user1.email` استفاده کنیم،
+چون مقدار آن از `user1` خارج نشده است.
+
+### استفاده از ساختارهای Tuple بدون فیلدهای نامگذاریشده برای ایجاد انواع مختلف
+
+Rust همچنین از ساختارهایی که شبیه تاپلها هستند پشتیبانی میکند که به آنها _ساختارهای Tuple_ میگویند. ساختارهای Tuple به دلیل نام ساختار معنای بیشتری دارند اما نامهایی برای فیلدهای خود ندارند؛ بلکه فقط نوع فیلدها را دارند. ساختارهای Tuple زمانی مفید هستند که بخواهید به کل تاپل یک نام بدهید و آن را به عنوان نوعی متفاوت از تاپلهای دیگر مشخص کنید، و وقتی نامگذاری هر فیلد مانند یک ساختار معمولی طولانی یا زائد باشد.
+
+برای تعریف یک ساختار Tuple، با کلمه کلیدی `struct` و نام ساختار شروع کنید و سپس نوعهای موجود در تاپل را مشخص کنید. به عنوان مثال، در اینجا ما دو ساختار Tuple به نامهای `Color` و `Point` تعریف و استفاده کردهایم:
@@ -171,27 +131,24 @@ tuple structs named `Color` and `Point`:
-Note that the `black` and `origin` values are different types because they’re
-instances of different tuple structs. Each struct you define is its own type,
-even though the fields within the struct might have the same types. For
-example, a function that takes a parameter of type `Color` cannot take a
-`Point` as an argument, even though both types are made up of three `i32`
-values. Otherwise, tuple struct instances are similar to tuples in that you can
-destructure them into their individual pieces, and you can use a `.` followed
-by the index to access an individual value. Unlike tuples, tuple structs
-require you to name the type of the struct when you destructure them. For
-example, we would write `let Point(x, y, z) = origin;` to destructure the
-values in the `origin` point into variables named `x`, `y`, and `z`.
-
-### Unit-Like Structs Without Any Fields
-
-You can also define structs that don’t have any fields! These are called
-_unit-like structs_ because they behave similarly to `()`, the unit type that
-we mentioned in [“The Tuple Type”][tuples] section. Unit-like
-structs can be useful when you need to implement a trait on some type but don’t
-have any data that you want to store in the type itself. We’ll discuss traits
-in Chapter 10. Here’s an example of declaring and instantiating a unit struct
-named `AlwaysEqual`:
+توجه داشته باشید که مقادیر `black` و `origin` از انواع متفاوتی هستند
+چون آنها instanceهای دو `tuple struct` مختلفاند.
+هر `struct`ای که تعریف میکنید، نوع خاص خود را دارد،
+حتی اگر فیلدهای داخل آن `struct` نوعهای یکسانی داشته باشند.
+برای مثال، یک تابع که پارامتری از نوع `Color` میگیرد،
+نمیتواند یک `Point` را به عنوان آرگومان دریافت کند،
+حتی اگر هر دو نوع از سه مقدار `i32` تشکیل شده باشند.
+به جز این مورد، `tuple struct`ها شبیه به `tuple`ها هستند
+از این جهت که میتوانید آنها را به اجزای منفردشان destructure کنید،
+و با استفاده از `.` و اندیس، به مقدار خاصی دسترسی پیدا کنید.
+برخلاف `tuple`ها، `tuple struct`ها نیاز دارند که هنگام destructure کردن،
+نام نوع struct را مشخص کنید.
+برای مثال، برای destructure کردن مقادیر موجود در `origin` به متغیرهای `x`، `y` و `z`،
+باید بنویسیم: `let Point(x, y, z) = origin;`
+
+### ساختارهای شبیه به Unit بدون هیچ فیلدی
+
+شما همچنین میتوانید ساختارهایی تعریف کنید که هیچ فیلدی ندارند! اینها به عنوان _ساختارهای شبیه Unit_ شناخته میشوند زیرا شبیه به نوع `()`، نوع Unit، رفتار میکنند که در بخش [«نوع Tuple»][tuples] مورد اشاره قرار گرفت. ساختارهای شبیه Unit زمانی مفید هستند که نیاز به پیادهسازی یک ویژگی بر روی یک نوع داشته باشید اما هیچ دادهای برای ذخیره در خود نوع نداشته باشید. ما ویژگیها را در فصل ۱۰ بحث خواهیم کرد. در اینجا مثالی از اعلام و نمونهسازی یک ساختار شبیه Unit به نام `AlwaysEqual` آورده شده است:
@@ -201,92 +158,73 @@ named `AlwaysEqual`:
-To define `AlwaysEqual`, we use the `struct` keyword, the name we want, and
-then a semicolon. No need for curly brackets or parentheses! Then we can get an
-instance of `AlwaysEqual` in the `subject` variable in a similar way: using the
-name we defined, without any curly brackets or parentheses. Imagine that later
-we’ll implement behavior for this type such that every instance of
-`AlwaysEqual` is always equal to every instance of any other type, perhaps to
-have a known result for testing purposes. We wouldn’t need any data to
-implement that behavior! You’ll see in Chapter 10 how to define traits and
-implement them on any type, including unit-like structs.
-
-> ### Ownership of Struct Data
->
-> In the `User` struct definition in Listing 5-1, we used the owned `String`
-> type rather than the `&str` string slice type. This is a deliberate choice
-> because we want each instance of this struct to own all of its data and for
-> that data to be valid for as long as the entire struct is valid.
->
-> It’s also possible for structs to store references to data owned by something
-> else, but to do so requires the use of _lifetimes_, a Rust feature that we’ll
-> discuss in Chapter 10. Lifetimes ensure that the data referenced by a struct
-> is valid for as long as the struct is. Let’s say you try to store a reference
-> in a struct without specifying lifetimes, like the following; this won’t work:
->
->
->
->
->
-> ```rust,ignore,does_not_compile
-> struct User {
-> active: bool,
-> username: &str,
-> email: &str,
-> sign_in_count: u64,
-> }
->
-> fn main() {
-> let user1 = User {
-> active: true,
-> username: "someusername123",
-> email: "someone@example.com",
-> sign_in_count: 1,
-> };
-> }
-> ```
->
->
->
-> The compiler will complain that it needs lifetime specifiers:
->
-> ```console
-> $ cargo run
-> Compiling structs v0.1.0 (file:///projects/structs)
-> error[E0106]: missing lifetime specifier
-> --> src/main.rs:3:15
-> |
-> 3 | username: &str,
-> | ^ expected named lifetime parameter
-> |
-> help: consider introducing a named lifetime parameter
-> |
-> 1 ~ struct User<'a> {
-> 2 | active: bool,
-> 3 ~ username: &'a str,
-> |
->
-> error[E0106]: missing lifetime specifier
-> --> src/main.rs:4:12
-> |
-> 4 | email: &str,
-> | ^ expected named lifetime parameter
-> |
-> help: consider introducing a named lifetime parameter
-> |
-> 1 ~ struct User<'a> {
-> 2 | active: bool,
-> 3 | username: &str,
-> 4 ~ email: &'a str,
-> |
+برای تعریف `AlwaysEqual`، از کلمه کلیدی `struct`، نام دلخواه و سپس یک نقطه ویرگول استفاده میکنیم. نیازی به آکولاد یا پرانتز نیست! سپس میتوانیم یک نمونه از `AlwaysEqual` را در متغیر `subject` با استفاده از همان نامی که تعریف کردهایم، بدون هیچ آکولاد یا پرانتزی دریافت کنیم. تصور کنید که در آینده رفتاری را برای این نوع پیادهسازی خواهیم کرد که همه نمونههای `AlwaysEqual` همیشه با تمام نمونههای دیگر برابر باشند، شاید برای داشتن نتیجهای مشخص برای اهداف آزمایشی. برای پیادهسازی آن رفتار نیازی به هیچ دادهای نداریم! شما در فصل ۱۰ خواهید دید که چگونه میتوانید ویژگیها را تعریف و آنها را بر روی هر نوعی، از جمله ساختارهای شبیه به Unit، پیادهسازی کنید.
+
+> ### مالکیت دادههای Struct
>
-> For more information about this error, try `rustc --explain E0106`.
-> error: could not compile `structs` (bin "structs") due to 2 previous errors
-> ```
+> در تعریف ساختار `User` در لیست ۵-۱، ما از نوع مالک `String` به جای نوع برش رشته `&str` استفاده کردیم. این یک انتخاب عمدی است زیرا ما میخواهیم هر نمونه از این ساختار همه دادههای خود را مالک باشد و این دادهها به مدت زمانی که کل ساختار معتبر است، معتبر باقی بمانند.
>
-> In Chapter 10, we’ll discuss how to fix these errors so you can store
-> references in structs, but for now, we’ll fix errors like these using owned
-> types like `String` instead of references like `&str`.
+> همچنین ممکن است ساختارهایی وجود داشته باشند که به دادههای متعلق به چیز دیگری ارجاع میدهند، اما برای انجام این کار نیاز به استفاده از _طول عمرها_ داریم، یک ویژگی از Rust که ما در فصل ۱۰ مورد بحث قرار خواهیم داد. طول عمرها اطمینان حاصل میکنند که دادههایی که توسط یک ساختار ارجاع داده شدهاند تا زمانی که ساختار معتبر است، معتبر باقی میمانند. بیایید بگوییم شما سعی دارید یک ارجاع را در یک ساختار ذخیره کنید بدون اینکه طول عمرها را مشخص کنید، مانند مثال زیر؛ این کار نخواهد کرد:
+
+
+
+```rust,ignore,does_not_compile
+struct User {
+ active: bool,
+ username: &str,
+ email: &str,
+ sign_in_count: u64,
+}
+
+fn main() {
+ let user1 = User {
+ active: true,
+ username: "someusername123",
+ email: "someone@example.com",
+ sign_in_count: 1,
+ };
+}
+```
+
+
+
+کامپایلر شکایت خواهد کرد که به مشخصکنندههای طول عمر نیاز دارد:
+
+```console
+$ cargo run
+ Compiling structs v0.1.0 (file:///projects/structs)
+error[E0106]: missing lifetime specifier
+ --> src/main.rs:3:15
+ |
+3 | username: &str,
+ | ^ expected named lifetime parameter
+ |
+help: consider introducing a named lifetime parameter
+ |
+1 ~ struct User<'a> {
+2 | active: bool,
+3 ~ username: &'a str,
+ |
+
+error[E0106]: missing lifetime specifier
+ --> src/main.rs:4:12
+ |
+4 | email: &str,
+ | ^ expected named lifetime parameter
+ |
+help: consider introducing a named lifetime parameter
+ |
+1 ~ struct User<'a> {
+2 | active: bool,
+3 | username: &str,
+4 ~ email: &'a str,
+ |
+
+For more information about this error, try `rustc --explain E0106`.
+error: could not compile `structs` (bin "structs") due to 2 previous errors
+```
+
+در فصل ۱۰، ما بحث خواهیم کرد که چگونه این خطاها را برطرف کنید تا بتوانید ارجاعها را در ساختارها ذخیره کنید، اما در حال حاضر، ما این خطاها را با استفاده از انواع مالک مانند `String` به جای ارجاعها مانند `&str` برطرف خواهیم کرد.
section
-of Chapter 3: by using tuples.
+تابع `area` قرار است مساحت یک مستطیل را محاسبه کند، اما تابعی که نوشتیم دو پارامتر دارد و هیچکجا در برنامه مشخص نیست که این پارامترها به هم مرتبط هستند. بهتر است عرض و ارتفاع را به صورت گروهی تعریف کنیم تا خوانایی و مدیریت کد بهتر شود. یکی از روشهایی که قبلاً در بخش [«نوع Tuple»][the-tuple-type] فصل ۳ بحث کردیم این است که از تاپلها استفاده کنیم.
-### Refactoring with Tuples
+### بازنویسی با استفاده از Tupleها
-Listing 5-9 shows another version of our program that uses tuples.
+لیست ۵-۹ نسخه دیگری از برنامه ما را نشان میدهد که از تاپلها استفاده میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-09/src/main.rs}}
@@ -52,25 +40,15 @@ Listing 5-9 shows another version of our program that uses tuples.
-In one way, this program is better. Tuples let us add a bit of structure, and
-we’re now passing just one argument. But in another way, this version is less
-clear: tuples don’t name their elements, so we have to index into the parts of
-the tuple, making our calculation less obvious.
+از یک منظر، این برنامه بهتر است. تاپلها کمی ساختار اضافه میکنند و اکنون ما فقط یک آرگومان ارسال میکنیم. اما از منظر دیگر، این نسخه کمتر واضح است: تاپلها اجزای خود را نامگذاری نمیکنند، بنابراین باید به بخشهای تاپل با استفاده از ایندکسها دسترسی پیدا کنیم که محاسبات ما را کمتر شفاف میکند.
-Mixing up the width and height wouldn’t matter for the area calculation, but if
-we want to draw the rectangle on the screen, it would matter! We would have to
-keep in mind that `width` is the tuple index `0` and `height` is the tuple
-index `1`. This would be even harder for someone else to figure out and keep in
-mind if they were to use our code. Because we haven’t conveyed the meaning of
-our data in our code, it’s now easier to introduce errors.
+اگر بخواهیم مستطیل را روی صفحه نمایش بکشیم، جابهجایی عرض و ارتفاع اهمیتی ندارد، اما برای رسم آن اهمیت پیدا میکند! ما باید به خاطر داشته باشیم که `width` ایندکس `0` تاپل و `height` ایندکس `1` تاپل است. این کار حتی برای کسی که از کد ما استفاده میکند سختتر خواهد بود و به اشتباهات بیشتری منجر میشود. چون معنای دادههای ما در کد مشخص نشده است، احتمال خطا بیشتر میشود.
-### Refactoring with Structs: Adding More Meaning
+### بازنویسی با استفاده از Structها: افزودن معنای بیشتر
-We use structs to add meaning by labeling the data. We can transform the tuple
-we’re using into a struct with a name for the whole as well as names for the
-parts, as shown in Listing 5-10.
+ما از ساختارها استفاده میکنیم تا با نامگذاری دادهها، معنای بیشتری به آنها بدهیم. میتوانیم تاپلی که استفاده میکنیم را به یک ساختار تبدیل کنیم که برای کل دادهها یک نام و همچنین برای بخشهای مختلف آن نامهایی مشخص کنیم، همانطور که در لیست ۵-۱۰ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-10/src/main.rs}}
@@ -78,35 +56,20 @@ parts, as shown in Listing 5-10.
-Here, we’ve defined a struct and named it `Rectangle`. Inside the curly
-brackets, we defined the fields as `width` and `height`, both of which have
-type `u32`. Then, in `main`, we created a particular instance of `Rectangle`
-that has a width of `30` and a height of `50`.
+در اینجا یک `struct` تعریف کردهایم و نام آن را `Rectangle` گذاشتهایم.
+درون آکولادها، فیلدهایی با نامهای `width` و `height` تعریف کردهایم
+که هر دو دارای نوع `u32` هستند. سپس، در تابع `main`، یک نمونه خاص از `Rectangle` ایجاد کردهایم
+که `width` آن برابر با `30` و `height` آن برابر با `50` است.
-Our `area` function is now defined with one parameter, which we’ve named
-`rectangle`, whose type is an immutable borrow of a struct `Rectangle`
-instance. As mentioned in Chapter 4, we want to borrow the struct rather than
-take ownership of it. This way, `main` retains its ownership and can continue
-using `rect1`, which is the reason we use the `&` in the function signature and
-where we call the function.
+تابع `area` ما اکنون با یک پارامتر تعریف شده است که آن را `rectangle` نامیدهایم و نوع آن یک ارجاع غیرقابل تغییر به یک نمونه از ساختار `Rectangle` است. همانطور که در فصل ۴ اشاره شد، ما میخواهیم ساختار را قرض بگیریم نه اینکه مالکیت آن را بگیریم. به این ترتیب، `main` مالکیت خود را حفظ میکند و میتواند همچنان از `rect1` استفاده کند. به همین دلیل است که از `&` در امضای تابع و در جایی که تابع را فراخوانی میکنیم استفاده میکنیم.
-The `area` function accesses the `width` and `height` fields of the `Rectangle`
-instance (note that accessing fields of a borrowed struct instance does not
-move the field values, which is why you often see borrows of structs). Our
-function signature for `area` now says exactly what we mean: calculate the area
-of `Rectangle`, using its `width` and `height` fields. This conveys that the
-width and height are related to each other, and it gives descriptive names to
-the values rather than using the tuple index values of `0` and `1`. This is a
-win for clarity.
+تابع `area` به فیلدهای `width` و `height` در نمونه `Rectangle` دسترسی پیدا میکند (توجه داشته باشید که دسترسی به فیلدهای یک نمونه قرضگرفتهشده باعث انتقال مقادیر فیلدها نمیشود، به همین دلیل است که اغلب قرضگیری ساختارها را مشاهده میکنید). امضای تابع `area` ما اکنون دقیقاً همان چیزی را میگوید که منظور ماست: مساحت `Rectangle` را با استفاده از فیلدهای `width` و `height` آن محاسبه کن. این کار نشان میدهد که عرض و ارتفاع به یکدیگر مرتبط هستند و نامهای توصیفی به مقادیر میدهد، به جای استفاده از مقادیر ایندکس تاپلها مانند `0` و `1`. این یک پیروزی برای شفافیت است.
-### Adding Useful Functionality with Derived Traits
+### افزودن قابلیتهای مفید با Traits مشتقشده
-It’d be useful to be able to print an instance of `Rectangle` while we’re
-debugging our program and see the values for all its fields. Listing 5-11 tries
-using the [`println!` macro][println] as we have used in
-previous chapters. This won’t work, however.
+زمانی که در حال اشکالزدایی برنامه خود هستیم، مفید است که بتوانیم نمونهای از `Rectangle` را چاپ کرده و مقادیر تمام فیلدهای آن را ببینیم. لیست ۵-۱۱ تلاش میکند با استفاده از ماکروی [`println!`][println] که در فصلهای قبلی استفاده کردهایم، این کار را انجام دهد. با این حال، این کار موفق نخواهد بود.
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-11/src/main.rs}}
@@ -114,53 +77,37 @@ previous chapters. This won’t work, however.
-When we compile this code, we get an error with this core message:
+وقتی این کد را کامپایل میکنیم، با خطایی مواجه میشویم که پیام اصلی آن به این صورت است:
```text
{{#include ../listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt:3}}
```
-The `println!` macro can do many kinds of formatting, and by default, the curly
-brackets tell `println!` to use formatting known as `Display`: output intended
-for direct end user consumption. The primitive types we’ve seen so far
-implement `Display` by default because there’s only one way you’d want to show
-a `1` or any other primitive type to a user. But with structs, the way
-`println!` should format the output is less clear because there are more
-display possibilities: Do you want commas or not? Do you want to print the
-curly brackets? Should all the fields be shown? Due to this ambiguity, Rust
-doesn’t try to guess what we want, and structs don’t have a provided
-implementation of `Display` to use with `println!` and the `{}` placeholder.
+ماکروی `println!` میتواند بسیاری از انواع فرمتبندی را انجام دهد، و به صورت پیشفرض، آکولادها به `println!` میگویند که از فرمتبندیای که به نام `Display` شناخته میشود استفاده کند: خروجیای که برای مصرف مستقیم کاربر نهایی در نظر گرفته شده است. انواع ابتدایی که تاکنون دیدهایم به صورت پیشفرض ویژگی `Display` را پیادهسازی میکنند زیرا تنها یک روش برای نمایش یک مقدار مانند `1` یا هر نوع ابتدایی دیگری به کاربر وجود دارد. اما با ساختارها، روش فرمتبندی خروجی کمتر واضح است زیرا امکانات بیشتری برای نمایش وجود دارد: آیا میخواهید از ویرگول استفاده شود یا خیر؟ آیا میخواهید آکولادها چاپ شوند؟ آیا تمام فیلدها باید نشان داده شوند؟ به دلیل این ابهام، Rust سعی نمیکند حدس بزند که ما چه میخواهیم، و ساختارها پیادهسازیای برای `Display` ندارند که بتوان با `println!` و جایگزین `{}` استفاده کرد.
-If we continue reading the errors, we’ll find this helpful note:
+اگر به خواندن خطاها ادامه دهیم، به این یادداشت مفید خواهیم رسید:
```text
{{#include ../listings/ch05-using-structs-to-structure-related-data/listing-05-11/output.txt:9:10}}
```
-Let’s try it! The `println!` macro call will now look like `println!("rect1 is
-{rect1:?}");`. Putting the specifier `:?` inside the curly brackets tells
-`println!` we want to use an output format called `Debug`. The `Debug` trait
-enables us to print our struct in a way that is useful for developers so we can
-see its value while we’re debugging our code.
+بیایید آن را امتحان کنیم! اکنون فراخوانی ماکروی `println!` به صورت `println!("rect1 is {rect1:?}");` خواهد بود. قرار دادن مشخصکننده `:?` داخل آکولادها به `println!` میگوید که میخواهیم از یک فرمت خروجی به نام `Debug` استفاده کنیم. ویژگی `Debug` به ما اجازه میدهد تا ساختار خود را به روشی که برای توسعهدهندگان مفید است چاپ کنیم تا مقدار آن را هنگام اشکالزدایی کد خود ببینیم.
-Compile the code with this change. Drat! We still get an error:
+کد را با این تغییر کامپایل کنید. خب، باز هم یک خطا دریافت میکنیم:
```text
{{#include ../listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt:3}}
```
-But again, the compiler gives us a helpful note:
+اما باز هم کامپایلر یادداشتی مفید به ما میدهد:
```text
{{#include ../listings/ch05-using-structs-to-structure-related-data/output-only-01-debug/output.txt:9:10}}
```
-Rust _does_ include functionality to print out debugging information, but we
-have to explicitly opt in to make that functionality available for our struct.
-To do that, we add the outer attribute `#[derive(Debug)]` just before the
-struct definition, as shown in Listing 5-12.
+Rust _در واقع_ قابلیت چاپ اطلاعات اشکالزدایی را دارد، اما باید به صورت صریح این قابلیت را برای ساختار خود فعال کنیم. برای انجام این کار، ویژگی بیرونی `#[derive(Debug)]` را دقیقاً قبل از تعریف ساختار اضافه میکنیم، همانطور که در لیست ۵-۱۲ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-12/src/main.rs}}
@@ -168,77 +115,43 @@ struct definition, as shown in Listing 5-12.
-Now when we run the program, we won’t get any errors, and we’ll see the
-following output:
+اکنون وقتی برنامه را اجرا میکنیم، هیچ خطایی دریافت نخواهیم کرد و خروجی زیر را خواهیم دید:
```console
{{#include ../listings/ch05-using-structs-to-structure-related-data/listing-05-12/output.txt}}
```
-Nice! It’s not the prettiest output, but it shows the values of all the fields
-for this instance, which would definitely help during debugging. When we have
-larger structs, it’s useful to have output that’s a bit easier to read; in
-those cases, we can use `{:#?}` instead of `{:?}` in the `println!` string. In
-this example, using the `{:#?}` style will output the following:
+عالی! این خروجی ممکن است زیباترین نباشد، اما مقادیر تمام فیلدها را برای این نمونه نشان میدهد که قطعاً در هنگام اشکالزدایی کمک میکند. زمانی که ساختارهای بزرگتری داریم، مفید است که خروجی کمی آسانتر خوانده شود؛ در چنین مواردی میتوانیم به جای `{:?}` از `{:#?}` در رشته `println!` استفاده کنیم. در این مثال، استفاده از سبک `{:#?}` خروجی زیر را ایجاد خواهد کرد:
```console
{{#include ../listings/ch05-using-structs-to-structure-related-data/output-only-02-pretty-debug/output.txt}}
```
-Another way to print out a value using the `Debug` format is to use the [`dbg!`
-macro][dbg], which takes ownership of an expression (as opposed
-to `println!`, which takes a reference), prints the file and line number of
-where that `dbg!` macro call occurs in your code along with the resultant value
-of that expression, and returns ownership of the value.
+روش دیگر برای چاپ مقدار با استفاده از فرمت `Debug`، استفاده از ماکروی [`dbg!`][dbg] است که مالکیت یک عبارت را میگیرد (برخلاف `println!`، که ارجاع میگیرد)، فایل و شماره خطی که فراخوانی `dbg!` در آن اتفاق میافتد همراه با مقدار حاصل از آن عبارت را چاپ میکند و مالکیت مقدار را بازمیگرداند.
-> Note: Calling the `dbg!` macro prints to the standard error console stream
-> (`stderr`), as opposed to `println!`, which prints to the standard output
-> console stream (`stdout`). We’ll talk more about `stderr` and `stdout` in the
-> [“Writing Error Messages to Standard Error Instead of Standard Output”
-> section in Chapter 12][err].
-Here’s an example where we’re interested in the value that gets assigned to the
-`width` field, as well as the value of the whole struct in `rect1`:
+Here is the continuation of the translation for **"ch05-02-example-structs.md"** into Persian:
+
+در اینجا مثالی آورده شده است که در آن ما به مقدار اختصاص داده شده به فیلد `width` و همچنین مقدار کل ساختار در `rect1` علاقهمند هستیم:
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/src/main.rs}}
```
-We can put `dbg!` around the expression `30 * scale` and, because `dbg!`
-returns ownership of the expression’s value, the `width` field will get the
-same value as if we didn’t have the `dbg!` call there. We don’t want `dbg!` to
-take ownership of `rect1`, so we use a reference to `rect1` in the next call.
-Here’s what the output of this example looks like:
+ما میتوانیم `dbg!` را در اطراف عبارت `30 * scale` قرار دهیم و چون `dbg!` مالکیت مقدار عبارت را بازمیگرداند، فیلد `width` همان مقداری را خواهد داشت که اگر فراخوانی `dbg!` در آنجا وجود نداشت. ما نمیخواهیم `dbg!` مالکیت `rect1` را بگیرد، بنابراین از یک ارجاع به `rect1` در فراخوانی بعدی استفاده میکنیم. در اینجا خروجی این مثال آورده شده است:
```console
{{#include ../listings/ch05-using-structs-to-structure-related-data/no-listing-05-dbg-macro/output.txt}}
```
-We can see the first bit of output came from _src/main.rs_ line 10 where we’re
-debugging the expression `30 * scale`, and its resultant value is `60` (the
-`Debug` formatting implemented for integers is to print only their value). The
-`dbg!` call on line 14 of _src/main.rs_ outputs the value of `&rect1`, which is
-the `Rectangle` struct. This output uses the pretty `Debug` formatting of the
-`Rectangle` type. The `dbg!` macro can be really helpful when you’re trying to
-figure out what your code is doing!
-
-In addition to the `Debug` trait, Rust has provided a number of traits for us
-to use with the `derive` attribute that can add useful behavior to our custom
-types. Those traits and their behaviors are listed in [Appendix C][app-c]. We’ll cover how to implement these traits with custom behavior as
-well as how to create your own traits in Chapter 10. There are also many
-attributes other than `derive`; for more information, see [the “Attributes”
-section of the Rust Reference][attributes].
-
-Our `area` function is very specific: it only computes the area of rectangles.
-It would be helpful to tie this behavior more closely to our `Rectangle` struct
-because it won’t work with any other type. Let’s look at how we can continue to
-refactor this code by turning the `area` function into an `area` _method_
-defined on our `Rectangle` type.
+میتوانیم ببینیم که اولین بخش خروجی از خط ۱۰ در _src/main.rs_ آمده است، جایی که ما در حال اشکالزدایی عبارت `30 * scale` هستیم، و مقدار حاصل آن `60` است (فرمتبندی `Debug` که برای اعداد صحیح پیادهسازی شده است فقط مقدار آنها را چاپ میکند). فراخوانی `dbg!` در خط ۱۴ از _src/main.rs_ مقدار `&rect1` را چاپ میکند که ساختار `Rectangle` است. این خروجی از فرمتبندی زیبا و مفید `Debug` برای نوع `Rectangle` استفاده میکند. ماکروی `dbg!` میتواند در هنگام تلاش برای درک رفتار کدتان بسیار مفید باشد!
+
+علاوه بر ویژگی `Debug`، Rust تعدادی ویژگی برای ما فراهم کرده است که میتوانیم با استفاده از ویژگی `derive` آنها را به نوعهای سفارشی خود اضافه کنیم و رفتار مفیدی ارائه دهند. این ویژگیها و رفتار آنها در [ضمیمه ج][app-c] فهرست شدهاند. ما در فصل ۱۰ به نحوه پیادهسازی این ویژگیها با رفتار سفارشی و همچنین نحوه ایجاد ویژگیهای خود میپردازیم. همچنین بسیاری از ویژگیهای دیگر به غیر از `derive` وجود دارند؛ برای اطلاعات بیشتر، به [بخش «ویژگیها» در مرجع Rust][attributes] مراجعه کنید.
+
+تابع `area` ما بسیار خاص است: فقط مساحت مستطیلها را محاسبه میکند. مفید خواهد بود اگر این رفتار را به صورت نزدیکتر با ساختار `Rectangle` مرتبط کنیم، زیرا این تابع با هیچ نوع دیگری کار نخواهد کرد. بیایید ببینیم که چگونه میتوانیم با تبدیل تابع `area` به یک _متد_ که برای نوع `Rectangle` تعریف شده است، این کد را بازنویسی کنیم.
[the-tuple-type]: ch03-02-data-types.html#the-tuple-type
[app-c]: appendix-03-derivable-traits.md
-[println]: ../std/macro.println.html
-[dbg]: ../std/macro.dbg.html
-[err]: ch12-06-writing-to-stderr-instead-of-stdout.html
-[attributes]: ../reference/attributes.html
+[println]: https://doc.rust-lang.org/std/macro.println.html
+[dbg]: https://doc.rust-lang.org/std/macro.dbg.html
+[attributes]: ../reference/attributes.html
\ No newline at end of file
diff --git a/src/ch05-03-method-syntax.md b/src/ch05-03-method-syntax.md
index ab92d19ac0..fcdef0521a 100644
--- a/src/ch05-03-method-syntax.md
+++ b/src/ch05-03-method-syntax.md
@@ -1,21 +1,17 @@
-## Method Syntax
+## متد
-_Methods_ are similar to functions: we declare them with the `fn` keyword and a
-name, they can have parameters and a return value, and they contain some code
-that’s run when the method is called from somewhere else. Unlike functions,
-methods are defined within the context of a struct (or an enum or a trait
-object, which we cover in [Chapter 6][enums] and [Chapter
-18][trait-objects], respectively), and their first parameter is
-always `self`, which represents the instance of the struct the method is being
-called on.
+_متدها_ شبیه به توابع هستند: آنها را با کلیدواژهی `fn` و یک نام تعریف میکنیم،
+میتوانند پارامتر و مقدار بازگشتی داشته باشند، و شامل مقداری کد هستند
+که هنگام فراخوانی متد از جایی دیگر اجرا میشود. برخلاف توابع،
+متدها در بستر یک `struct` (یا یک `enum` یا یک `trait object` که به ترتیب در [فصل ۶][enums] و [فصل ۱۸][trait-objects] بررسی میشوند) تعریف میشوند،
+و اولین پارامتر آنها همیشه `self` است، که نشاندهندهی نمونهای از `struct` است
+که متد روی آن فراخوانی شده است.
-### Defining Methods
+### تعریف متدها
-Let’s change the `area` function that has a `Rectangle` instance as a parameter
-and instead make an `area` method defined on the `Rectangle` struct, as shown
-in Listing 5-13.
+بیایید تابع `area` که یک نمونه از `Rectangle` را به عنوان پارامتر میگیرد، تغییر دهیم و به جای آن، یک متد `area` تعریف کنیم که روی ساختار `Rectangle` تعریف شده است، همانطور که در لیست ۵-۱۳ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-13/src/main.rs}}
@@ -23,46 +19,15 @@ in Listing 5-13.
-To define the function within the context of `Rectangle`, we start an `impl`
-(implementation) block for `Rectangle`. Everything within this `impl` block
-will be associated with the `Rectangle` type. Then we move the `area` function
-within the `impl` curly brackets and change the first (and in this case, only)
-parameter to be `self` in the signature and everywhere within the body. In
-`main`, where we called the `area` function and passed `rect1` as an argument,
-we can instead use _method syntax_ to call the `area` method on our `Rectangle`
-instance. The method syntax goes after an instance: we add a dot followed by
-the method name, parentheses, and any arguments.
-
-In the signature for `area`, we use `&self` instead of `rectangle: &Rectangle`.
-The `&self` is actually short for `self: &Self`. Within an `impl` block, the
-type `Self` is an alias for the type that the `impl` block is for. Methods must
-have a parameter named `self` of type `Self` for their first parameter, so Rust
-lets you abbreviate this with only the name `self` in the first parameter spot.
-Note that we still need to use the `&` in front of the `self` shorthand to
-indicate that this method borrows the `Self` instance, just as we did in
-`rectangle: &Rectangle`. Methods can take ownership of `self`, borrow `self`
-immutably, as we’ve done here, or borrow `self` mutably, just as they can any
-other parameter.
-
-We chose `&self` here for the same reason we used `&Rectangle` in the function
-version: we don’t want to take ownership, and we just want to read the data in
-the struct, not write to it. If we wanted to change the instance that we’ve
-called the method on as part of what the method does, we’d use `&mut self` as
-the first parameter. Having a method that takes ownership of the instance by
-using just `self` as the first parameter is rare; this technique is usually
-used when the method transforms `self` into something else and you want to
-prevent the caller from using the original instance after the transformation.
-
-The main reason for using methods instead of functions, in addition to
-providing method syntax and not having to repeat the type of `self` in every
-method’s signature, is for organization. We’ve put all the things we can do
-with an instance of a type in one `impl` block rather than making future users
-of our code search for capabilities of `Rectangle` in various places in the
-library we provide.
-
-Note that we can choose to give a method the same name as one of the struct’s
-fields. For example, we can define a method on `Rectangle` that is also named
-`width`:
+برای تعریف تابع در زمینه `Rectangle`، یک بلوک `impl` (پیادهسازی) برای `Rectangle` شروع میکنیم. هر چیزی در این بلوک `impl` با نوع `Rectangle` مرتبط خواهد بود. سپس، تابع `area` را به درون آکولادهای `impl` منتقل کرده و اولین (و در اینجا تنها) پارامتر آن را در امضا و در هر جایی در بدنه به `self` تغییر میدهیم. در `main`، جایی که تابع `area` را فراخوانی میکردیم و `rect1` را به عنوان آرگومان ارسال میکردیم، اکنون میتوانیم از _نحو متد_ برای فراخوانی متد `area` روی نمونه `Rectangle` خود استفاده کنیم. نحو متد بعد از یک نمونه قرار میگیرد: نقطهای اضافه میکنیم و به دنبال آن نام متد، پرانتزها و هر آرگومان دیگری قرار میدهیم.
+
+در امضای `area`، از `&self` به جای `rectangle: &Rectangle` استفاده میکنیم. `&self` در واقع معادل کوتاهشدهای از `self: &Self` است. درون یک بلوک `impl`، نوع `Self` نام مستعاری برای نوعی است که بلوک `impl` برای آن تعریف شده است. متدها باید به عنوان پارامتر اول خود یک پارامتری به نام `self` از نوع `Self` داشته باشند، بنابراین Rust به شما اجازه میدهد این عبارت را با فقط نوشتن `self` در محل اولین پارامتر کوتاه کنید. توجه داشته باشید که همچنان باید از `&` در مقابل اختصار `self` استفاده کنیم تا نشان دهیم که این متد نمونه `Self` را قرض میگیرد، دقیقاً همانطور که در `rectangle: &Rectangle` استفاده میکردیم. متدها میتوانند مالکیت `self` را بگیرند، `self` را به صورت غیرقابل تغییر قرض بگیرند، همانطور که در اینجا انجام دادهایم، یا `self` را به صورت قابل تغییر قرض بگیرند، دقیقاً مانند هر پارامتر دیگری.
+
+ما در اینجا `&self` را انتخاب کردهایم به همان دلیلی که در نسخه تابع از `&Rectangle` استفاده کردیم: ما نمیخواهیم مالکیت را بگیریم و فقط میخواهیم دادهها را در ساختار بخوانیم، نه اینکه آنها را تغییر دهیم. اگر بخواهیم نمونهای که متد روی آن فراخوانی شده است را به عنوان بخشی از کاری که متد انجام میدهد تغییر دهیم، به عنوان پارامتر اول از `&mut self` استفاده میکنیم. داشتن متدی که مالکیت نمونه را میگیرد با استفاده از فقط `self` به عنوان پارامتر اول به ندرت اتفاق میافتد؛ این تکنیک معمولاً زمانی استفاده میشود که متد `self` را به چیز دیگری تبدیل کند و شما بخواهید از استفاده از نمونه اصلی پس از تبدیل جلوگیری کنید.
+
+دلیل اصلی استفاده از متدها به جای توابع، علاوه بر ارائه نحو متد و عدم نیاز به تکرار نوع `self` در امضای هر متد، سازماندهی است. ما تمام کارهایی که میتوانیم با یک نمونه از یک نوع انجام دهیم را در یک بلوک `impl` قرار دادهایم، به جای اینکه کاربران آینده کد ما به دنبال قابلیتهای `Rectangle` در مکانهای مختلف در کتابخانهای که ارائه میدهیم بگردند.
+
+توجه داشته باشید که میتوانیم تصمیم بگیریم متدی با همان نام یک فیلد ساختار تعریف کنیم. برای مثال، میتوانیم متدی روی `Rectangle` تعریف کنیم که نام آن نیز `width` باشد:
@@ -72,37 +37,19 @@ fields. For example, we can define a method on `Rectangle` that is also named
-Here, we’re choosing to make the `width` method return `true` if the value in
-the instance’s `width` field is greater than `0` and `false` if the value is
-`0`: we can use a field within a method of the same name for any purpose. In
-`main`, when we follow `rect1.width` with parentheses, Rust knows we mean the
-method `width`. When we don’t use parentheses, Rust knows we mean the field
-`width`.
-
-Often, but not always, when we give a method the same name as a field we want
-it to only return the value in the field and do nothing else. Methods like this
-are called _getters_, and Rust does not implement them automatically for struct
-fields as some other languages do. Getters are useful because you can make the
-field private but the method public, and thus enable read-only access to that
-field as part of the type’s public API. We will discuss what public and private
-are and how to designate a field or method as public or private in [Chapter
-7][public].
-
-> ### Where’s the `->` Operator?
+Here is the continuation of the translation for **"ch05-03-method-syntax.md"** into Persian:
+
+در اینجا ما تصمیم گرفتهایم متد `width` را طوری تعریف کنیم که اگر مقدار در فیلد `width` نمونه بزرگتر از `0` باشد مقدار `true` و در غیر این صورت مقدار `false` برگرداند: ما میتوانیم از یک فیلد درون یک متد با همان نام برای هر منظوری استفاده کنیم. در `main`، وقتی که ما `rect1.width` را با پرانتز دنبال میکنیم، Rust میداند که منظور ما متد `width` است. وقتی از پرانتز استفاده نمیکنیم، Rust میداند که منظور ما فیلد `width` است.
+
+اغلب، اما نه همیشه، زمانی که به یک متد نامی مشابه یک فیلد میدهیم، میخواهیم که این متد تنها مقدار موجود در فیلد را بازگرداند و هیچ کار دیگری انجام ندهد. متدهایی مانند اینها _getter_ نامیده میشوند، و Rust آنها را به صورت خودکار برای فیلدهای ساختار پیادهسازی نمیکند، همانطور که برخی از زبانهای دیگر انجام میدهند. Getterها مفید هستند زیرا میتوانید فیلد را خصوصی کنید اما متد را عمومی کنید و به این ترتیب دسترسی فقط-خواندنی به آن فیلد را به عنوان بخشی از API عمومی نوع فعال کنید. ما در فصل [۷][public] در مورد عمومی و خصوصی بودن و چگونگی تعیین عمومی یا خصوصی بودن یک فیلد یا متد بحث خواهیم کرد.
+
+> ### کجاست عملگر `->`؟
>
-> In C and C++, two different operators are used for calling methods: you use
-> `.` if you’re calling a method on the object directly and `->` if you’re
-> calling the method on a pointer to the object and need to dereference the
-> pointer first. In other words, if `object` is a pointer,
-> `object->something()` is similar to `(*object).something()`.
+> در C و C++، دو عملگر مختلف برای فراخوانی متدها استفاده میشود: شما از `.` استفاده میکنید اگر متد را روی خود شیء فراخوانی میکنید و از `->` اگر متد را روی یک اشارهگر (Pointer) به شیء فراخوانی میکنید و نیاز دارید ابتدا اشارهگر (Pointer) را اشارهبرداری کنید. به عبارت دیگر، اگر `object` یک اشارهگر (Pointer) باشد، `object->something()` شبیه به `(*object).something()` است.
>
-> Rust doesn’t have an equivalent to the `->` operator; instead, Rust has a
-> feature called _automatic referencing and dereferencing_. Calling methods is
-> one of the few places in Rust with this behavior.
+> Rust معادل عملگر `->` را ندارد؛ به جای آن، Rust یک ویژگی به نام _ارجاعدهی و اشارهبرداری خودکار_ دارد. فراخوانی متدها یکی از معدود مکانهایی در Rust است که این رفتار را دارد.
>
-> Here’s how it works: when you call a method with `object.something()`, Rust
-> automatically adds in `&`, `&mut`, or `*` so `object` matches the signature of
-> the method. In other words, the following are the same:
+> اینگونه کار میکند: وقتی یک متد را با `object.something()` فراخوانی میکنید، Rust به طور خودکار `&`، `&mut` یا `*` را اضافه میکند تا `object` با امضای متد مطابقت داشته باشد. به عبارت دیگر، موارد زیر یکسان هستند:
>
>
>
@@ -127,23 +74,13 @@ are and how to designate a field or method as public or private in [Chapter
> (&p1).distance(&p2);
> ```
>
-> The first one looks much cleaner. This automatic referencing behavior works
-> because methods have a clear receiver—the type of `self`. Given the receiver
-> and name of a method, Rust can figure out definitively whether the method is
-> reading (`&self`), mutating (`&mut self`), or consuming (`self`). The fact
-> that Rust makes borrowing implicit for method receivers is a big part of
-> making ownership ergonomic in practice.
+> اولین مورد خیلی تمیزتر به نظر میرسد. این رفتار ارجاعدهی خودکار کار میکند زیرا متدها یک گیرنده واضح دارند—نوع `self`. با توجه به گیرنده و نام یک متد، Rust میتواند به طور قطعی تعیین کند که آیا متد در حال خواندن (`&self`)، تغییر (`&mut self`) یا مصرف (`self`) است. این واقعیت که Rust قرضگیری را برای گیرندههای متد ضمنی میکند، بخش بزرگی از راحتی کار با مالکیت در عمل است.
-### Methods with More Parameters
+### متدهایی با پارامترهای بیشتر
-Let’s practice using methods by implementing a second method on the `Rectangle`
-struct. This time we want an instance of `Rectangle` to take another instance
-of `Rectangle` and return `true` if the second `Rectangle` can fit completely
-within `self` (the first `Rectangle`); otherwise, it should return `false`.
-That is, once we’ve defined the `can_hold` method, we want to be able to write
-the program shown in Listing 5-14.
+بیایید با تعریف یک متد دیگر روی ساختار `Rectangle` تمرین کنیم. این بار میخواهیم یک نمونه از `Rectangle` نمونه دیگری از `Rectangle` را بگیرد و مقدار `true` برگرداند اگر `Rectangle` دوم کاملاً در `self` (اولین `Rectangle`) جای گیرد؛ در غیر این صورت مقدار `false` برگرداند. به عبارت دیگر، پس از تعریف متد `can_hold`، میخواهیم بتوانیم برنامهای بنویسیم که در لیست ۵-۱۴ نشان داده شده است.
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-14/src/main.rs}}
@@ -151,30 +88,16 @@ the program shown in Listing 5-14.
-The expected output would look like the following because both dimensions of
-`rect2` are smaller than the dimensions of `rect1`, but `rect3` is wider than
-`rect1`:
+خروجی مورد انتظار به صورت زیر خواهد بود زیرا هر دو بُعد `rect2` کوچکتر از ابعاد `rect1` هستند، اما `rect3` از `rect1` عریضتر است:
```text
Can rect1 hold rect2? true
Can rect1 hold rect3? false
```
-We know we want to define a method, so it will be within the `impl Rectangle`
-block. The method name will be `can_hold`, and it will take an immutable borrow
-of another `Rectangle` as a parameter. We can tell what the type of the
-parameter will be by looking at the code that calls the method:
-`rect1.can_hold(&rect2)` passes in `&rect2`, which is an immutable borrow to
-`rect2`, an instance of `Rectangle`. This makes sense because we only need to
-read `rect2` (rather than write, which would mean we’d need a mutable borrow),
-and we want `main` to retain ownership of `rect2` so we can use it again after
-calling the `can_hold` method. The return value of `can_hold` will be a
-Boolean, and the implementation will check whether the width and height of
-`self` are greater than the width and height of the other `Rectangle`,
-respectively. Let’s add the new `can_hold` method to the `impl` block from
-Listing 5-13, shown in Listing 5-15.
-
-
+ما میدانیم که میخواهیم یک متد تعریف کنیم، بنابراین این متد در بلوک `impl Rectangle` خواهد بود. نام متد `can_hold` خواهد بود و یک قرض غیرقابل تغییر از یک `Rectangle` دیگر به عنوان پارامتر خواهد گرفت. میتوانیم نوع پارامتر را با نگاه به کدی که متد را فراخوانی میکند تشخیص دهیم: `rect1.can_hold(&rect2)` مقدار `&rect2` را ارسال میکند، که یک قرض غیرقابل تغییر به `rect2`، یک نمونه از `Rectangle` است. این منطقی است زیرا ما فقط نیاز به خواندن `rect2` داریم (نه نوشتن، که به یک قرض قابل تغییر نیاز داشت) و میخواهیم مالکیت `rect2` در `main` باقی بماند تا بتوانیم پس از فراخوانی متد `can_hold` دوباره از آن استفاده کنیم. مقدار بازگشتی `can_hold` یک مقدار بولی خواهد بود و پیادهسازی بررسی میکند که آیا عرض و ارتفاع `self` به ترتیب بزرگتر از عرض و ارتفاع `Rectangle` دیگر هستند. بیایید متد جدید `can_hold` را به بلوک `impl` از لیست ۵-۱۳ اضافه کنیم، همانطور که در لیست ۵-۱۵ نشان داده شده است.
+
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-15/src/main.rs:here}}
@@ -182,27 +105,16 @@ Listing 5-13, shown in Listing 5-15.
-When we run this code with the `main` function in Listing 5-14, we’ll get our
-desired output. Methods can take multiple parameters that we add to the
-signature after the `self` parameter, and those parameters work just like
-parameters in functions.
-### Associated Functions
+Here is the continuation of the translation for **"ch05-03-method-syntax.md"** into Persian:
+
+وقتی این کد را با تابع `main` موجود در لیست ۵-۱۴ اجرا میکنیم، خروجی دلخواه را دریافت خواهیم کرد. متدها میتوانند چندین پارامتر بگیرند که ما آنها را پس از پارامتر `self` به امضا اضافه میکنیم، و این پارامترها همانند پارامترهای توابع عمل میکنند.
+
+### توابع مرتبط
-All functions defined within an `impl` block are called _associated functions_
-because they’re associated with the type named after the `impl`. We can define
-associated functions that don’t have `self` as their first parameter (and thus
-are not methods) because they don’t need an instance of the type to work with.
-We’ve already used one function like this: the `String::from` function that’s
-defined on the `String` type.
+تمام توابعی که در یک بلوک `impl` تعریف شدهاند _توابع مرتبط_ نامیده میشوند، زیرا با نوعی که بعد از `impl` نامگذاری شده است، مرتبط هستند. ما میتوانیم توابع مرتبطی را تعریف کنیم که `self` را به عنوان اولین پارامتر خود ندارند (و بنابراین متد نیستند) زیرا نیازی به کار با یک نمونه از نوع ندارند. ما قبلاً از یک تابع مشابه استفاده کردهایم: تابع `String::from` که روی نوع `String` تعریف شده است.
-Associated functions that aren’t methods are often used for constructors that
-will return a new instance of the struct. These are often called `new`, but
-`new` isn’t a special name and isn’t built into the language. For example, we
-could choose to provide an associated function named `square` that would have
-one dimension parameter and use that as both width and height, thus making it
-easier to create a square `Rectangle` rather than having to specify the same
-value twice:
+توابع مرتبطی که متد نیستند اغلب برای سازندهها استفاده میشوند که نمونه جدیدی از ساختار را بازمیگردانند. این توابع معمولاً `new` نامیده میشوند، اما `new` یک نام خاص نیست و در زبان به صورت داخلی تعریف نشده است. برای مثال، ما میتوانیم تصمیم بگیریم تابع مرتبطی به نام `square` ارائه دهیم که یک پارامتر برای ابعاد بگیرد و از آن به عنوان عرض و ارتفاع استفاده کند، بنابراین ایجاد یک `Rectangle` مربعی را آسانتر میکند به جای اینکه مجبور باشیم مقدار یکسان را دو بار مشخص کنیم:
Filename: src/main.rs
@@ -210,23 +122,15 @@ value twice:
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/no-listing-03-associated-functions/src/main.rs:here}}
```
-The `Self` keywords in the return type and in the body of the function are
-aliases for the type that appears after the `impl` keyword, which in this case
-is `Rectangle`.
+کلمات کلیدی `Self` در نوع بازگشتی و در بدنه تابع، نام مستعاری برای نوعی هستند که بعد از کلمه کلیدی `impl` ظاهر میشود، که در اینجا `Rectangle` است.
-To call this associated function, we use the `::` syntax with the struct name;
-`let sq = Rectangle::square(3);` is an example. This function is namespaced by
-the struct: the `::` syntax is used for both associated functions and
-namespaces created by modules. We’ll discuss modules in [Chapter
-7][modules].
+برای فراخوانی این تابع مرتبط، از نحو `::` همراه با نام ساختار استفاده میکنیم؛ برای مثال: `let sq = Rectangle::square(3);`. این تابع با ساختار فضای نامگذاری شده است: نحو `::` برای توابع مرتبط و فضای نامهای ایجاد شده توسط ماژولها استفاده میشود. ما ماژولها را در [فصل ۷][modules] بررسی خواهیم کرد.
-### Multiple `impl` Blocks
+### بلوکهای متعدد `impl`
-Each struct is allowed to have multiple `impl` blocks. For example, Listing
-5-15 is equivalent to the code shown in Listing 5-16, which has each method in
-its own `impl` block.
+هر ساختار اجازه دارد چندین بلوک `impl` داشته باشد. برای مثال، لیست ۵-۱۵ معادل کدی است که در لیست ۵-۱۶ نشان داده شده است، که هر متد در بلوک `impl` خود قرار دارد.
-
+
```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-16/src/main.rs:here}}
@@ -234,21 +138,13 @@ its own `impl` block.
-There’s no reason to separate these methods into multiple `impl` blocks here,
-but this is valid syntax. We’ll see a case in which multiple `impl` blocks are
-useful in Chapter 10, where we discuss generic types and traits.
+هیچ دلیلی برای جدا کردن این متدها به بلوکهای متعدد `impl` در اینجا وجود ندارد، اما این یک نحو معتبر است. ما در فصل ۱۰ موردی را خواهیم دید که در آن بلوکهای متعدد `impl` مفید هستند، جایی که ما نوعهای عمومی و ویژگیها را بررسی خواهیم کرد.
-## Summary
+## خلاصه
-Structs let you create custom types that are meaningful for your domain. By
-using structs, you can keep associated pieces of data connected to each other
-and name each piece to make your code clear. In `impl` blocks, you can define
-functions that are associated with your type, and methods are a kind of
-associated function that let you specify the behavior that instances of your
-structs have.
+ساختارها به شما اجازه میدهند تا نوعهای سفارشی ایجاد کنید که برای حوزه کاری شما معنادار باشند. با استفاده از ساختارها، میتوانید قطعات دادهای مرتبط را به هم متصل کنید و برای هر قطعه نامی تعیین کنید تا کد شما شفاف شود. در بلوکهای `impl`، شما میتوانید توابعی را تعریف کنید که با نوع شما مرتبط هستند، و متدها نوعی از توابع مرتبط هستند که به شما اجازه میدهند رفتار نمونههای ساختارهایتان را مشخص کنید.
-But structs aren’t the only way you can create custom types: let’s turn to
-Rust’s enum feature to add another tool to your toolbox.
+اما ساختارها تنها راه ایجاد نوعهای سفارشی نیستند: بیایید به ویژگی Enum در Rust بپردازیم تا ابزار دیگری به جعبه ابزار شما اضافه کنیم.
[enums]: ch06-00-enums.html
[trait-objects]: ch18-02-trait-objects.md
diff --git a/src/ch06-00-enums.md b/src/ch06-00-enums.md
index 0b9fc7806c..69cf50ae43 100644
--- a/src/ch06-00-enums.md
+++ b/src/ch06-00-enums.md
@@ -1,11 +1,8 @@
-# Enums and Pattern Matching
+# شمارندهها و تطابق الگو
-In this chapter, we’ll look at _enumerations_, also referred to as _enums_.
-Enums allow you to define a type by enumerating its possible _variants_. First
-we’ll define and use an enum to show how an enum can encode meaning along with
-data. Next, we’ll explore a particularly useful enum, called `Option`, which
-expresses that a value can be either something or nothing. Then we’ll look at
-how pattern matching in the `match` expression makes it easy to run different
-code for different values of an enum. Finally, we’ll cover how the `if let`
-construct is another convenient and concise idiom available to handle enums in
-your code.
+در این فصل، به *شمارندهها* که همچنین به عنوان *enums* شناخته میشوند، میپردازیم. شمارندهها به شما اجازه میدهند
+یک نوع را با شمردن *مقادیر ممکن* آن تعریف کنید. ابتدا، یک شمارنده تعریف کرده و از آن استفاده میکنیم تا نشان دهیم چگونه
+شمارنده میتواند معنی را همراه با داده کدگذاری کند. سپس، به شمارندهای بسیار مفید به نام `Option` خواهیم پرداخت که
+بیان میکند یک مقدار میتواند *چیزی* باشد یا *هیچ چیز*. بعد، بررسی خواهیم کرد که چگونه تطابق الگو در عبارت `match`
+باعث میشود اجرای کد مختلف برای مقادیر مختلف یک شمارنده آسان شود. در نهایت، پوشش خواهیم داد که ساختار `if let` چگونه
+ایدهآل و مختصر برای مدیریت شمارندهها در کد شما است.
diff --git a/src/ch06-01-defining-an-enum.md b/src/ch06-01-defining-an-enum.md
index 4e1443c703..3d1933a11c 100644
--- a/src/ch06-01-defining-an-enum.md
+++ b/src/ch06-01-defining-an-enum.md
@@ -1,65 +1,42 @@
-## Defining an Enum
-
-Where structs give you a way of grouping together related fields and data, like
-a `Rectangle` with its `width` and `height`, enums give you a way of saying a
-value is one of a possible set of values. For example, we may want to say that
-`Rectangle` is one of a set of possible shapes that also includes `Circle` and
-`Triangle`. To do this, Rust allows us to encode these possibilities as an enum.
-
-Let’s look at a situation we might want to express in code and see why enums
-are useful and more appropriate than structs in this case. Say we need to work
-with IP addresses. Currently, two major standards are used for IP addresses:
-version four and version six. Because these are the only possibilities for an
-IP address that our program will come across, we can _enumerate_ all possible
-variants, which is where enumeration gets its name.
-
-Any IP address can be either a version four or a version six address, but not
-both at the same time. That property of IP addresses makes the enum data
-structure appropriate because an enum value can only be one of its variants.
-Both version four and version six addresses are still fundamentally IP
-addresses, so they should be treated as the same type when the code is handling
-situations that apply to any kind of IP address.
-
-We can express this concept in code by defining an `IpAddrKind` enumeration and
-listing the possible kinds an IP address can be, `V4` and `V6`. These are the
-variants of the enum:
+## تعریف یک Enum
+
+در حالی که ساختارها (Structs) روشی برای گروهبندی فیلدها و دادههای مرتبط فراهم میکنند، Enumها به شما امکان میدهند که بگویید یک مقدار یکی از مجموعه مقادیر ممکن است. به عنوان مثال، ممکن است بخواهیم بگوییم که `Rectangle` یکی از مجموعه اشکالی است که همچنین شامل `Circle` و `Triangle` میشود. برای انجام این کار، زبان Rust به ما اجازه میدهد تا این امکانها را بهعنوان یک Enum کدگذاری کنیم.
+
+بیایید نگاهی به یک موقعیت بیندازیم که ممکن است بخواهیم در کد بیان کنیم و ببینیم چرا Enumها مفیدتر و مناسبتر از Structها هستند. فرض کنید باید با آدرسهای IP کار کنیم. در حال حاضر، دو استاندارد اصلی برای آدرسهای IP وجود دارد: نسخه چهار و نسخه شش. از آنجا که این تنها حالتهای ممکن برای آدرسهای IP هستند که برنامه ما با آنها مواجه خواهد شد، میتوانیم تمام حالتهای ممکن را شمارش کنیم، که همین موضوع اساس نامگذاری Enumها است.
+
+هر آدرس IP میتواند یا نسخه چهار یا نسخه شش باشد، اما نمیتواند بهطور همزمان هر دو باشد. این ویژگی آدرسهای IP استفاده از ساختار داده Enum را مناسب میکند زیرا مقدار یک Enum میتواند فقط یکی از حالتهایش باشد. هر دو آدرس نسخه چهار و نسخه شش همچنان اساساً آدرس IP هستند، بنابراین باید هنگام کار با کدی که به هر نوع آدرس IP اعمال میشود، بهعنوان یک نوع یکسان رفتار شوند.
+
+ما میتوانیم این مفهوم را در کد با تعریف یک Enumeration به نام `IpAddrKind` و فهرست کردن انواع ممکن یک آدرس IP، یعنی `V4` و `V6`، بیان کنیم. اینها حالتهای Enum هستند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs:def}}
```
-`IpAddrKind` is now a custom data type that we can use elsewhere in our code.
+اکنون `IpAddrKind` یک نوع داده سفارشی است که میتوانیم در قسمتهای دیگر کد خود استفاده کنیم.
-### Enum Values
+### مقادیر Enum
-We can create instances of each of the two variants of `IpAddrKind` like this:
+میتوانیم نمونههایی از هر یک از دو حالت `IpAddrKind` را به این صورت ایجاد کنیم:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs:instance}}
```
-Note that the variants of the enum are namespaced under its identifier, and we
-use a double colon to separate the two. This is useful because now both values
-`IpAddrKind::V4` and `IpAddrKind::V6` are of the same type: `IpAddrKind`. We
-can then, for instance, define a function that takes any `IpAddrKind`:
+توجه داشته باشید که حالتهای Enum تحت شناسه آن نامگذاری شدهاند و برای جدا کردن دو حالت از یکدیگر از دو نقطه استفاده میکنیم. این ویژگی مفید است زیرا اکنون هر دو مقدار `IpAddrKind::V4` و `IpAddrKind::V6` از نوع یکسان `IpAddrKind` هستند. سپس میتوانیم به عنوان مثال یک تابع تعریف کنیم که هر نوع `IpAddrKind` را به عنوان ورودی بپذیرد:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs:fn}}
```
-And we can call this function with either variant:
+و میتوانیم این تابع را با هر دو حالت فراخوانی کنیم:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-01-defining-enums/src/main.rs:fn_call}}
```
-Using enums has even more advantages. Thinking more about our IP address type,
-at the moment we don’t have a way to store the actual IP address _data_; we
-only know what _kind_ it is. Given that you just learned about structs in
-Chapter 5, you might be tempted to tackle this problem with structs as shown in
-Listing 6-1.
+استفاده از Enumها مزایای بیشتری دارد. اگر بیشتر به نوع آدرس IP خود فکر کنیم، متوجه میشویم که در حال حاضر راهی برای ذخیره دادههای واقعی آدرس IP نداریم؛ فقط میدانیم که چه نوعی است. با توجه به اینکه بهتازگی درباره Structها در فصل 5 یاد گرفتهاید، ممکن است وسوسه شوید این مشکل را با Structها همانطور که در فهرست 6-1 نشان داده شده است، حل کنید.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs:here}}
@@ -67,51 +44,23 @@ Listing 6-1.
-Here, we’ve defined a struct `IpAddr` that has two fields: a `kind` field that
-is of type `IpAddrKind` (the enum we defined previously) and an `address` field
-of type `String`. We have two instances of this struct. The first is `home`,
-and it has the value `IpAddrKind::V4` as its `kind` with associated address
-data of `127.0.0.1`. The second instance is `loopback`. It has the other
-variant of `IpAddrKind` as its `kind` value, `V6`, and has address `::1`
-associated with it. We’ve used a struct to bundle the `kind` and `address`
-values together, so now the variant is associated with the value.
+در اینجا ما یک Struct به نام `IpAddr` تعریف کردهایم که دو فیلد دارد: یک فیلد `kind` که از نوع `IpAddrKind` است (همان Enum که قبلاً تعریف کردیم) و یک فیلد `address` از نوع `String`. ما دو نمونه از این Struct داریم. اولین مورد `home` نام دارد و مقدار `IpAddrKind::V4` بهعنوان `kind` با دادههای آدرس مرتبط `127.0.0.1` دارد. نمونه دوم `loopback` نام دارد. این نمونه حالت دیگر Enum یعنی `V6` را بهعنوان مقدار `kind` دارد و آدرس مرتبط `::1` است. ما از یک Struct برای بستهبندی مقادیر `kind` و `address` با هم استفاده کردهایم، بنابراین اکنون حالت با مقدار مرتبط شده است.
-However, representing the same concept using just an enum is more concise:
-rather than an enum inside a struct, we can put data directly into each enum
-variant. This new definition of the `IpAddr` enum says that both `V4` and `V6`
-variants will have associated `String` values:
+با این حال، نمایش همان مفهوم با استفاده از فقط یک Enum مختصرتر است: بهجای استفاده از Enum داخل یک Struct، میتوانیم دادهها را مستقیماً به هر حالت Enum متصل کنیم. این تعریف جدید Enum `IpAddr` نشان میدهد که هر دو حالت `V4` و `V6` مقادیر `String` مرتبط دارند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-02-enum-with-data/src/main.rs:here}}
```
-We attach data to each variant of the enum directly, so there is no need for an
-extra struct. Here, it’s also easier to see another detail of how enums work:
-the name of each enum variant that we define also becomes a function that
-constructs an instance of the enum. That is, `IpAddr::V4()` is a function call
-that takes a `String` argument and returns an instance of the `IpAddr` type. We
-automatically get this constructor function defined as a result of defining the
-enum.
-
-There’s another advantage to using an enum rather than a struct: each variant
-can have different types and amounts of associated data. Version four IP
-addresses will always have four numeric components that will have values
-between 0 and 255. If we wanted to store `V4` addresses as four `u8` values but
-still express `V6` addresses as one `String` value, we wouldn’t be able to with
-a struct. Enums handle this case with ease:
+ما دادهها را مستقیماً به هر حالت Enum متصل کردهایم، بنابراین نیازی به یک Struct اضافی نیست. در اینجا همچنین میتوان جزئیات دیگری از نحوه عملکرد Enumها را مشاهده کرد: نام هر حالت Enum که تعریف میکنیم بهصورت یک تابع تبدیل میشود که نمونهای از Enum ایجاد میکند. یعنی `IpAddr::V4()` یک فراخوانی تابع است که یک آرگومان از نوع `String` میگیرد و نمونهای از نوع `IpAddr` برمیگرداند. این تابع سازنده بهطور خودکار بهعنوان نتیجه تعریف Enum تعریف میشود.
+
+یک مزیت دیگر استفاده از Enum بهجای Struct این است که هر حالت میتواند انواع و مقادیر داده مرتبط متفاوتی داشته باشد. آدرسهای IP نسخه چهار همیشه چهار مؤلفه عددی خواهند داشت که مقادیرشان بین 0 و 255 است. اگر بخواهیم آدرسهای `V4` را بهصورت چهار مقدار `u8` ذخیره کنیم اما همچنان آدرسهای `V6` را بهصورت یک مقدار `String` بیان کنیم، با یک Struct نمیتوانیم این کار را انجام دهیم. Enumها بهراحتی این حالت را مدیریت میکنند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-03-variants-with-different-data/src/main.rs:here}}
```
-We’ve shown several different ways to define data structures to store version
-four and version six IP addresses. However, as it turns out, wanting to store
-IP addresses and encode which kind they are is so common that [the standard
-library has a definition we can use!][IpAddr] Let’s look at how
-the standard library defines `IpAddr`: it has the exact enum and variants that
-we’ve defined and used, but it embeds the address data inside the variants in
-the form of two different structs, which are defined differently for each
-variant:
+ما چندین روش مختلف برای تعریف ساختارهای داده برای ذخیره آدرسهای IP نسخه چهار و نسخه شش نشان دادهایم. با این حال، همانطور که مشخص است، ذخیره آدرسهای IP و کدگذاری نوع آنها بهقدری رایج است که [کتابخانه استاندارد تعریفی برای این کار ارائه میدهد!][IpAddr] بیایید نگاهی به نحوه تعریف `IpAddr` در کتابخانه استاندارد بیندازیم: این کتابخانه دارای همان Enum و حالتهایی است که ما تعریف کرده و استفاده کردهایم، اما دادههای آدرس را بهصورت داخلی در حالتها در قالب دو Struct مختلف تعبیه کرده است، که بهطور متفاوت برای هر حالت تعریف شدهاند:
```rust
struct Ipv4Addr {
@@ -128,20 +77,13 @@ enum IpAddr {
}
```
-This code illustrates that you can put any kind of data inside an enum variant:
-strings, numeric types, or structs, for example. You can even include another
-enum! Also, standard library types are often not much more complicated than
-what you might come up with.
+این کد نشان میدهد که شما میتوانید هر نوع دادهای مانند رشتهها، انواع عددی، یا Structها را داخل حالتهای Enum قرار دهید. حتی میتوانید یک Enum دیگر را نیز شامل کنید! همچنین، انواع استاندارد کتابخانه معمولاً خیلی پیچیدهتر از چیزی نیستند که ممکن است خودتان ارائه دهید.
-Note that even though the standard library contains a definition for `IpAddr`,
-we can still create and use our own definition without conflict because we
-haven’t brought the standard library’s definition into our scope. We’ll talk
-more about bringing types into scope in Chapter 7.
+توجه داشته باشید که با وجود اینکه کتابخانه استاندارد تعریفی برای `IpAddr` دارد، ما همچنان میتوانیم تعریف خودمان را ایجاد و استفاده کنیم بدون اینکه تضادی پیش بیاید زیرا تعریف کتابخانه استاندارد را به محدوده خود وارد نکردهایم. ما در فصل 7 درباره وارد کردن انواع به محدوده بیشتر صحبت خواهیم کرد.
-Let’s look at another example of an enum in Listing 6-2: this one has a wide
-variety of types embedded in its variants.
+بیایید به مثال دیگری از یک Enum در فهرست 6-2 نگاه کنیم: این مورد دارای انواع متنوعی از دادههای جاسازیشده در حالتهای خود است.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs:here}}
@@ -149,86 +91,50 @@ variety of types embedded in its variants.
-This enum has four variants with different types:
+این Enum دارای چهار حالت با انواع مختلف است:
-- `Quit`: Has no data associated with it at all
-- `Move`: Has named fields, like a struct does
-- `Write`: Includes a single `String`
-- `ChangeColor`: Includes three `i32` values
+* `Quit`: هیچ دادهای همراه خود ندارد
+* `Move`: دارای فیلدهای نامگذاریشده است، مانند یک `struct`
+* `Write`: شامل یک `String` واحد است
+* `ChangeColor`: شامل سه مقدار از نوع `i32` است
-Defining an enum with variants such as the ones in Listing 6-2 is similar to
-defining different kinds of struct definitions, except the enum doesn’t use the
-`struct` keyword and all the variants are grouped together under the `Message`
-type. The following structs could hold the same data that the preceding enum
-variants hold:
+تعریف یک Enum با حالتهایی مانند حالتهای فهرست 6-2 مشابه تعریف انواع مختلف ساختارها است، با این تفاوت که Enum از کلمه کلیدی `struct` استفاده نمیکند و تمام حالتها تحت نوع `Message` گروهبندی شدهاند. ساختارهای زیر میتوانند همان دادههایی را نگه دارند که حالتهای Enum قبلی نگه میدارند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-04-structs-similar-to-message-enum/src/main.rs:here}}
```
-But if we used the different structs, each of which has its own type, we
-couldn’t as easily define a function to take any of these kinds of messages as
-we could with the `Message` enum defined in Listing 6-2, which is a single type.
+اما اگر از ساختارهای مختلفی استفاده کنیم که هر یک نوع خاص خود را دارند، نمیتوانیم بهراحتی یک تابع تعریف کنیم که بتواند هر یک از این انواع پیامها را مانند چیزی که با Enum `Message` تعریفشده در فهرست 6-2 امکانپذیر است، دریافت کند.
-There is one more similarity between enums and structs: just as we’re able to
-define methods on structs using `impl`, we’re also able to define methods on
-enums. Here’s a method named `call` that we could define on our `Message` enum:
+یک شباهت دیگر بین Enumها و ساختارها این است: همانطور که میتوانیم متدها را با استفاده از `impl` برای ساختارها تعریف کنیم، میتوانیم متدها را برای Enumها نیز تعریف کنیم. اینجا یک متد به نام `call` است که میتوانیم برای Enum `Message` خود تعریف کنیم:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-05-methods-on-enums/src/main.rs:here}}
```
-The body of the method would use `self` to get the value that we called the
-method on. In this example, we’ve created a variable `m` that has the value
-`Message::Write(String::from("hello"))`, and that is what `self` will be in the
-body of the `call` method when `m.call()` runs.
-
-Let’s look at another enum in the standard library that is very common and
-useful: `Option`.
-
-### The `Option` Enum and Its Advantages Over Null Values
-
-This section explores a case study of `Option`, which is another enum defined
-by the standard library. The `Option` type encodes the very common scenario in
-which a value could be something or it could be nothing.
-
-For example, if you request the first item in a non-empty list, you would get
-a value. If you request the first item in an empty list, you would get nothing.
-Expressing this concept in terms of the type system means the compiler can
-check whether you’ve handled all the cases you should be handling; this
-functionality can prevent bugs that are extremely common in other programming
-languages.
-
-Programming language design is often thought of in terms of which features you
-include, but the features you exclude are important too. Rust doesn’t have the
-null feature that many other languages have. _Null_ is a value that means there
-is no value there. In languages with null, variables can always be in one of
-two states: null or not-null.
-
-In his 2009 presentation “Null References: The Billion Dollar Mistake,” Tony
-Hoare, the inventor of null, had this to say:
-
-> I call it my billion-dollar mistake. At that time, I was designing the first
-> comprehensive type system for references in an object-oriented language. My
-> goal was to ensure that all use of references should be absolutely safe, with
-> checking performed automatically by the compiler. But I couldn’t resist the
-> temptation to put in a null reference, simply because it was so easy to
-> implement. This has led to innumerable errors, vulnerabilities, and system
-> crashes, which have probably caused a billion dollars of pain and damage in
-> the last forty years.
-
-The problem with null values is that if you try to use a null value as a
-not-null value, you’ll get an error of some kind. Because this null or not-null
-property is pervasive, it’s extremely easy to make this kind of error.
-
-However, the concept that null is trying to express is still a useful one: a
-null is a value that is currently invalid or absent for some reason.
-
-The problem isn’t really with the concept but with the particular
-implementation. As such, Rust does not have nulls, but it does have an enum
-that can encode the concept of a value being present or absent. This enum is
-`Option`, and it is [defined by the standard library][option]
-as follows:
+بدنه این متد از `self` برای دسترسی به مقداری که متد روی آن فراخوانی شده است استفاده میکند. در این مثال، ما یک متغیر به نام `m` ایجاد کردهایم که مقدار `Message::Write(String::from("hello"))` را دارد و این همان چیزی است که `self` در بدن متد `call` هنگام اجرای `m.call()` خواهد بود.
+
+بیایید به یک Enum دیگر در کتابخانه استاندارد که بسیار متداول و مفید است نگاهی بیندازیم: `Option`.
+
+
+### Enum `Option` و مزایای آن نسبت به مقادیر Null
+
+این بخش به مطالعه موردی `Option` میپردازد که یکی دیگر از Enumهای تعریف شده در کتابخانه استاندارد است. نوع `Option` سناریوی بسیار رایجی را نشان میدهد که در آن یک مقدار میتواند وجود داشته باشد یا هیچ مقداری وجود نداشته باشد.
+
+به عنوان مثال، اگر اولین مورد را در یک لیست غیر خالی درخواست کنید، مقداری دریافت خواهید کرد. اگر اولین مورد را در یک لیست خالی درخواست کنید، هیچ مقداری دریافت نخواهید کرد. بیان این مفهوم در قالب سیستم نوع به کامپایلر امکان میدهد تا بررسی کند آیا تمام مواردی که باید مدیریت شوند را در نظر گرفتهاید؛ این ویژگی میتواند از بروز باگهایی که در دیگر زبانهای برنامهنویسی بسیار رایج هستند جلوگیری کند.
+
+طراحی زبانهای برنامهنویسی اغلب از نظر ویژگیهایی که شامل میشوند بررسی میشود، اما ویژگیهایی که کنار گذاشته میشوند نیز مهم هستند. Rust ویژگی null را که بسیاری از زبانهای دیگر دارند، ندارد. _Null_ یک مقدار است که به معنای وجود نداشتن مقدار میباشد. در زبانهایی که دارای null هستند، متغیرها میتوانند همیشه در یکی از دو حالت باشند: null یا not-null.
+
+در ارائهی سال ۲۰۰۹ خود با عنوان «ارجاعات تهی: اشتباه میلیارد دلاری»(Null References: The Billion Dollar Mistake,”)، تونی هوار،
+مخترع `null`، این سخن را بیان کرد:
+
+> من آن را اشتباه میلیارد دلاری خود مینامم. در آن زمان، من در حال طراحی اولین سیستم نوع جامع برای مراجع در یک زبان شیءگرا بودم. هدف من اطمینان از این بود که تمام استفادههای از مراجع کاملاً امن باشند، با بررسیهایی که بهطور خودکار توسط کامپایلر انجام میشوند. اما نتوانستم در برابر وسوسه قرار دادن یک مرجع null مقاومت کنم، فقط به این دلیل که پیادهسازی آن بسیار آسان بود. این منجر به خطاها، آسیبپذیریها، و خرابیهای سیستمهای بیشماری شده است که احتمالاً باعث یک میلیارد دلار درد و ضرر در چهل سال گذشته شدهاند.
+
+مشکل مقادیر null این است که اگر بخواهید از یک مقدار null بهعنوان یک مقدار not-null استفاده کنید، نوعی خطا دریافت خواهید کرد. از آنجا که خاصیت null یا not-null فراگیر است، بسیار آسان است که این نوع خطا را مرتکب شوید.
+
+با این حال، مفهومی که null سعی در بیان آن دارد همچنان مفید است: null یک مقدار است که در حال حاضر به دلایلی نامعتبر یا غایب است.
+
+مشکل واقعاً با مفهوم نیست، بلکه با پیادهسازی خاص است. به این ترتیب، Rust مقادیر null ندارد، اما یک Enum دارد که میتواند مفهوم وجود داشتن یا نداشتن یک مقدار را کدگذاری کند. این Enum `Option` است که به صورت زیر توسط کتابخانه استاندارد تعریف شده است:
```rust
enum Option {
@@ -237,90 +143,40 @@ enum Option {
}
```
-The `Option` enum is so useful that it’s even included in the prelude; you
-don’t need to bring it into scope explicitly. Its variants are also included in
-the prelude: you can use `Some` and `None` directly without the `Option::`
-prefix. The `Option` enum is still just a regular enum, and `Some(T)` and
-`None` are still variants of type `Option`.
+Enum `Option` آنقدر مفید است که حتی در بخش پیشفرض (Prelude) گنجانده شده است؛ نیازی نیست که بهطور صریح آن را به محدوده بیاورید. حالتهای آن نیز در بخش پیشفرض هستند: میتوانید مستقیماً از `Some` و `None` بدون پیشوند `Option::` استفاده کنید. Enum `Option` همچنان یک Enum معمولی است، و `Some(T)` و `None` همچنان حالتهایی از نوع `Option` هستند.
-The `` syntax is a feature of Rust we haven’t talked about yet. It’s a
-generic type parameter, and we’ll cover generics in more detail in Chapter 10.
-For now, all you need to know is that `` means that the `Some` variant of
-the `Option` enum can hold one piece of data of any type, and that each
-concrete type that gets used in place of `T` makes the overall `Option` type
-a different type. Here are some examples of using `Option` values to hold
-number types and char types:
+سینتکس `` یک ویژگی از Rust است که هنوز درباره آن صحبت نکردهایم. این یک پارامتر نوع عمومی (Generic) است و ما در فصل 10 به جزئیات بیشتری درباره آن خواهیم پرداخت. برای حالا، تنها چیزی که باید بدانید این است که `` به این معنا است که حالت `Some` از Enum `Option` میتواند یک قطعه داده از هر نوعی را نگه دارد، و هر نوع مشخصی که به جای `T` استفاده شود، کل نوع `Option` را به یک نوع متفاوت تبدیل میکند. در اینجا چند مثال از استفاده از مقادیر `Option` برای نگهداری انواع عددی و کاراکتری آورده شده است:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs:here}}
```
-The type of `some_number` is `Option`. The type of `some_char` is
-`Option`, which is a different type. Rust can infer these types because
-we’ve specified a value inside the `Some` variant. For `absent_number`, Rust
-requires us to annotate the overall `Option` type: the compiler can’t infer the
-type that the corresponding `Some` variant will hold by looking only at a
-`None` value. Here, we tell Rust that we mean for `absent_number` to be of type
-`Option`.
+نوع `some_number` برابر با `Option` است. نوع `some_char` برابر با `Option` است که یک نوع متفاوت است. Rust میتواند این انواع را تشخیص دهد زیرا ما مقداری را در حالت `Some` مشخص کردهایم. برای `absent_number`، Rust از ما میخواهد که نوع کلی `Option` را مشخص کنیم: کامپایلر نمیتواند نوعی را که حالت `Some` مرتبط نگه خواهد داشت فقط با نگاه کردن به یک مقدار `None` تشخیص دهد. در اینجا، ما به Rust میگوییم که منظور ما این است که `absent_number` از نوع `Option` باشد.
-When we have a `Some` value, we know that a value is present and the value is
-held within the `Some`. When we have a `None` value, in some sense it means the
-same thing as null: we don’t have a valid value. So why is having `Option`
-any better than having null?
+هنگامی که ما یک مقدار `Some` داریم، میدانیم که یک مقدار وجود دارد و این مقدار درون `Some` نگهداری میشود. هنگامی که ما یک مقدار `None` داریم، از یک نظر، این همان معنای null را دارد: ما یک مقدار معتبر نداریم. پس چرا داشتن `Option` بهتر از داشتن null است؟
-In short, because `Option` and `T` (where `T` can be any type) are different
-types, the compiler won’t let us use an `Option` value as if it were
-definitely a valid value. For example, this code won’t compile, because it’s
-trying to add an `i8` to an `Option`:
+به طور خلاصه، به این دلیل که `Option` و `T` (جایی که `T` میتواند هر نوعی باشد) انواع متفاوتی هستند، کامپایلر به ما اجازه نمیدهد که یک مقدار `Option` را بهعنوان یک مقدار قطعاً معتبر استفاده کنیم. به عنوان مثال، این کد کامپایل نخواهد شد، زیرا سعی در جمع یک `i8` با یک `Option` دارد:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/src/main.rs:here}}
```
-If we run this code, we get an error message like this one:
+اگر این کد را اجرا کنیم، پیام خطایی شبیه به این دریافت میکنیم:
```console
{{#include ../listings/ch06-enums-and-pattern-matching/no-listing-07-cant-use-option-directly/output.txt}}
```
-Intense! In effect, this error message means that Rust doesn’t understand how
-to add an `i8` and an `Option`, because they’re different types. When we
-have a value of a type like `i8` in Rust, the compiler will ensure that we
-always have a valid value. We can proceed confidently without having to check
-for null before using that value. Only when we have an `Option` (or
-whatever type of value we’re working with) do we have to worry about possibly
-not having a value, and the compiler will make sure we handle that case before
-using the value.
-
-In other words, you have to convert an `Option` to a `T` before you can
-perform `T` operations with it. Generally, this helps catch one of the most
-common issues with null: assuming that something isn’t null when it actually is.
-
-Eliminating the risk of incorrectly assuming a not-null value helps you to be
-more confident in your code. In order to have a value that can possibly be
-null, you must explicitly opt in by making the type of that value `Option`.
-Then, when you use that value, you are required to explicitly handle the case
-when the value is null. Everywhere that a value has a type that isn’t an
-`Option`, you _can_ safely assume that the value isn’t null. This was a
-deliberate design decision for Rust to limit null’s pervasiveness and increase
-the safety of Rust code.
-
-So how do you get the `T` value out of a `Some` variant when you have a value
-of type `Option` so that you can use that value? The `Option` enum has a
-large number of methods that are useful in a variety of situations; you can
-check them out in [its documentation][docs]. Becoming familiar
-with the methods on `Option` will be extremely useful in your journey with
-Rust.
-
-In general, in order to use an `Option` value, you want to have code that
-will handle each variant. You want some code that will run only when you have a
-`Some(T)` value, and this code is allowed to use the inner `T`. You want some
-other code to run only if you have a `None` value, and that code doesn’t have a
-`T` value available. The `match` expression is a control flow construct that
-does just this when used with enums: it will run different code depending on
-which variant of the enum it has, and that code can use the data inside the
-matching value.
-
-[IpAddr]: ../std/net/enum.IpAddr.html
-[option]: ../std/option/enum.Option.html
-[docs]: ../std/option/enum.Option.html
+شدید است! در واقع، این پیام خطا به این معنا است که Rust نمیداند چگونه یک `i8` و یک `Option` را جمع کند، زیرا آنها انواع مختلفی هستند. هنگامی که یک مقدار از نوعی مانند `i8` در Rust داریم، کامپایلر اطمینان میدهد که همیشه یک مقدار معتبر داریم. میتوانیم با اطمینان ادامه دهیم بدون اینکه مجبور باشیم قبل از استفاده از آن مقدار، null را بررسی کنیم. فقط زمانی که یک `Option` (یا هر نوع مقداری که با آن کار میکنیم) داریم باید نگران احتمال عدم وجود مقدار باشیم، و کامپایلر اطمینان میدهد که ما آن حالت را قبل از استفاده از مقدار مدیریت کردهایم.
+
+به عبارت دیگر، شما باید یک مقدار `Option` را به یک مقدار `T` تبدیل کنید قبل از اینکه بتوانید عملیات `T` را با آن انجام دهید. به طور کلی، این به جلوگیری از یکی از شایعترین مشکلات null کمک میکند: فرض غلط که چیزی null نیست در حالی که واقعاً null است.
+
+از بین بردن خطر فرض نادرست درباره یک مقدار not-null به شما کمک میکند تا در کد خود اطمینان بیشتری داشته باشید. برای داشتن مقداری که ممکن است null باشد، باید صریحاً با تعیین نوع آن مقدار بهعنوان `Option` به آن رضایت دهید. سپس، هنگامی که از آن مقدار استفاده میکنید، موظف هستید که بهطور صریح حالتی را که مقدار null است مدیریت کنید. هر جا که مقداری از نوعی است که `Option` نیست، میتوانید با خیال راحت فرض کنید که مقدار null نیست. این تصمیم طراحی برای محدود کردن شیوع null و افزایش ایمنی کدهای Rust بود.
+
+پس چگونه مقدار `T` را از حالت `Some` وقتی که یک مقدار از نوع `Option` دارید استخراج میکنید تا بتوانید از آن مقدار استفاده کنید؟ Enum `Option` تعداد زیادی متد دارد که در موقعیتهای مختلف مفید هستند؛ میتوانید آنها را در [مستندات آن][docs] بررسی کنید. آشنایی با متدهای موجود در `Option` در مسیر یادگیری Rust بسیار مفید خواهد بود.
+
+به طور کلی، برای استفاده از یک مقدار `Option`، میخواهید کدی داشته باشید که هر حالت را مدیریت کند. میخواهید کدی داشته باشید که تنها زمانی اجرا شود که یک مقدار `Some(T)` دارید، و این کد اجازه دارد از مقدار داخلی `T` استفاده کند. همچنین، میخواهید کدی داشته باشید که فقط در صورت وجود مقدار `None` اجرا شود، و این کد به هیچ مقدار `T` دسترسی ندارد. عبارت `match` یک سازه جریان کنترلی است که وقتی با Enumها استفاده میشود دقیقاً این کار را انجام میدهد: این عبارت کد متفاوتی را بسته به اینکه کدام حالت از Enum موجود است اجرا میکند، و آن کد میتواند از داده داخل مقدار منطبق شده استفاده کند.
+
+[IpAddr]: https://doc.rust-lang.org/std/net/enum.IpAddr.html
+[option]: https://doc.rust-lang.org/std/option/enum.Option.html
+[docs]: https://doc.rust-lang.org/std/option/enum.Option.html
diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md
index 6fa9de9e76..fb9e4511ae 100644
--- a/src/ch06-02-match.md
+++ b/src/ch06-02-match.md
@@ -1,30 +1,17 @@
+
-## The `match` Control Flow Construct
+## سازه جریان کنترلی `match`
-Rust has an extremely powerful control flow construct called `match` that
-allows you to compare a value against a series of patterns and then execute
-code based on which pattern matches. Patterns can be made up of literal values,
-variable names, wildcards, and many other things; [Chapter
-19][ch19-00-patterns] covers all the different kinds of patterns
-and what they do. The power of `match` comes from the expressiveness of the
-patterns and the fact that the compiler confirms that all possible cases are
-handled.
+زبان Rust دارای یک سازه جریان کنترلی بسیار قدرتمند به نام `match` است که به شما اجازه میدهد تا یک مقدار را با یک سری الگوها مقایسه کنید و سپس بر اساس الگویی که مطابقت دارد، کد مربوطه را اجرا کنید. الگوها میتوانند شامل مقادیر ثابت، نام متغیرها، wildcardها و چیزهای دیگر باشند. [فصل 19][ch19-00-patterns] انواع مختلف الگوها و عملکرد آنها را پوشش میدهد. قدرت `match` از بیانپذیری الگوها و این واقعیت ناشی میشود که کامپایلر تأیید میکند که همه حالتهای ممکن مدیریت شدهاند.
-Think of a `match` expression as being like a coin-sorting machine: coins slide
-down a track with variously sized holes along it, and each coin falls through
-the first hole it encounters that it fits into. In the same way, values go
-through each pattern in a `match`, and at the first pattern the value “fits,”
-the value falls into the associated code block to be used during execution.
+میتوانید یک عبارت `match` را مانند یک دستگاه مرتبکننده سکه تصور کنید: سکهها در یک مسیر با سوراخهایی با اندازههای مختلف قرار میگیرند و هر سکه از اولین سوراخی که در آن جا میشود عبور میکند. به همین ترتیب، مقادیر از هر الگو در یک `match` عبور میکنند و در اولین الگویی که مقدار "جا میشود"، مقدار به بلوک کد مرتبط میافتد و برای اجرا استفاده میشود.
-Speaking of coins, let’s use them as an example using `match`! We can write a
-function that takes an unknown US coin and, in a similar way as the counting
-machine, determines which coin it is and returns its value in cents, as shown
-in Listing 6-3.
+حال بیایید از یک مثال واقعی با سکهها استفاده کنیم! میتوانیم تابعی بنویسیم که یک سکه ناشناخته از ایالات متحده را بگیرد و به شیوهای مشابه دستگاه شمارنده سکهها، تعیین کند که آن سکه کدام نوع است و ارزش آن را به سنت برگرداند، همانطور که در فهرست 6-3 نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs:here}}
@@ -32,53 +19,29 @@ in Listing 6-3.
-Let’s break down the `match` in the `value_in_cents` function. First we list
-the `match` keyword followed by an expression, which in this case is the value
-`coin`. This seems very similar to a conditional expression used with `if`, but
-there’s a big difference: with `if`, the condition needs to evaluate to a
-Boolean value, but here it can be any type. The type of `coin` in this example
-is the `Coin` enum that we defined on the first line.
-
-Next are the `match` arms. An arm has two parts: a pattern and some code. The
-first arm here has a pattern that is the value `Coin::Penny` and then the `=>`
-operator that separates the pattern and the code to run. The code in this case
-is just the value `1`. Each arm is separated from the next with a comma.
-
-When the `match` expression executes, it compares the resultant value against
-the pattern of each arm, in order. If a pattern matches the value, the code
-associated with that pattern is executed. If that pattern doesn’t match the
-value, execution continues to the next arm, much as in a coin-sorting machine.
-We can have as many arms as we need: in Listing 6-3, our `match` has four arms.
-
-The code associated with each arm is an expression, and the resultant value of
-the expression in the matching arm is the value that gets returned for the
-entire `match` expression.
-
-We don’t typically use curly brackets if the match arm code is short, as it is
-in Listing 6-3 where each arm just returns a value. If you want to run multiple
-lines of code in a match arm, you must use curly brackets, and the comma
-following the arm is then optional. For example, the following code prints
-“Lucky penny!” every time the method is called with a `Coin::Penny`, but still
-returns the last value of the block, `1`:
+### بازبینی تابع `value_in_cents`
+
+ابتدا کلمه کلیدی `match` و سپس یک عبارت را فهرست میکنیم که در این مورد مقدار `coin` است. این کار بسیار مشابه یک عبارت شرطی که با `if` استفاده میشود به نظر میرسد، اما تفاوت بزرگی دارد: با `if`، شرط باید به یک مقدار بولین ارزیابی شود، اما اینجا میتواند هر نوعی باشد. نوع `coin` در این مثال enum `Coin` است که در اولین خط تعریف کردیم.
+
+بازوهای `match` دو قسمت دارند: یک الگو و مقداری کد. اولین بازو در اینجا دارای الگویی است که مقدار `Coin::Penny` است و سپس اپراتور `=>` که الگو و کد اجرایی را از هم جدا میکند. کد در اینجا فقط مقدار `1` است. هر بازو با یک کاما از بازوی بعدی جدا میشود.
+
+هنگامی که عبارت `match` اجرا میشود، مقدار حاصل را با الگوی هر بازو به ترتیب مقایسه میکند. اگر الگویی با مقدار مطابقت داشته باشد، کدی که با آن الگو مرتبط است اجرا میشود. اگر آن الگو با مقدار مطابقت نداشته باشد، اجرا به بازوی بعدی ادامه مییابد، همانطور که در یک دستگاه مرتبکننده سکهها عمل میکند. ما میتوانیم به هر تعداد بازو که نیاز داریم داشته باشیم: در فهرست 6-3، `match` ما چهار بازو دارد.
+
+کد مرتبط با هر بازو یک عبارت است و مقدار حاصل از عبارت در بازوی منطبق شده، مقداری است که برای کل عبارت `match` بازگردانده میشود.
+
+معمولاً اگر کد بازوی `match` کوتاه باشد، از آکولاد استفاده نمیکنیم، همانطور که در فهرست 6-3 که هر بازو فقط یک مقدار را برمیگرداند. اگر بخواهید چندین خط کد را در یک بازو اجرا کنید، باید از آکولاد استفاده کنید، و در این صورت کاما پس از بازو اختیاری است. به عنوان مثال، کد زیر هر بار که متد با یک `Coin::Penny` فراخوانی میشود، “Lucky penny!” را چاپ میکند، اما همچنان آخرین مقدار بلوک یعنی `1` را بازمیگرداند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-08-match-arm-multiple-lines/src/main.rs:here}}
```
-### Patterns That Bind to Values
+### الگوهایی که به مقادیر متصل میشوند
-Another useful feature of match arms is that they can bind to the parts of the
-values that match the pattern. This is how we can extract values out of enum
-variants.
+یکی دیگر از ویژگیهای مفید بازوهای `match` این است که میتوانند به بخشهایی از مقادیر که با الگو مطابقت دارند متصل شوند. این همان روشی است که میتوانیم مقادیر را از حالتهای enum استخراج کنیم.
-As an example, let’s change one of our enum variants to hold data inside it.
-From 1999 through 2008, the United States minted quarters with different
-designs for each of the 50 states on one side. No other coins got state
-designs, so only quarters have this extra value. We can add this information to
-our `enum` by changing the `Quarter` variant to include a `UsState` value
-stored inside it, which we’ve done in Listing 6-4.
+به عنوان مثال، بیایید یکی از حالتهای enum خود را تغییر دهیم تا دادههایی را درون خود نگه دارد. از سال 1999 تا 2008، ایالات متحده ربعهایی با طرحهای مختلف برای هر یک از 50 ایالت در یک طرف ضرب کرد. هیچ سکه دیگری طرح ایالتی نداشت، بنابراین فقط ربعها این مقدار اضافی را دارند. میتوانیم این اطلاعات را به enum خود با تغییر حالت `Quarter` به گونهای که یک مقدار `UsState` درون آن ذخیره شود اضافه کنیم، همانطور که در فهرست 6-4 انجام دادیم.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs:here}}
@@ -86,174 +49,100 @@ stored inside it, which we’ve done in Listing 6-4.
-Let’s imagine that a friend is trying to collect all 50 state quarters. While
-we sort our loose change by coin type, we’ll also call out the name of the
-state associated with each quarter so that if it’s one our friend doesn’t have,
-they can add it to their collection.
+بیایید تصور کنیم که یک دوست ما سعی دارد تمام 50 ربع ایالتی را جمعآوری کند. در حالی که ما پولهای خود را بر اساس نوع سکه مرتب میکنیم، همچنین نام ایالتی که با هر ربع مرتبط است را اعلام میکنیم تا اگر این یکی از آنهایی باشد که دوست ما ندارد، بتوانند آن را به مجموعه خود اضافه کنند.
-In the match expression for this code, we add a variable called `state` to the
-pattern that matches values of the variant `Coin::Quarter`. When a
-`Coin::Quarter` matches, the `state` variable will bind to the value of that
-quarter’s state. Then we can use `state` in the code for that arm, like so:
+در عبارت `match` برای این کد، یک متغیر به نام `state` به الگو اضافه میکنیم که مقادیری از حالت `Coin::Quarter` را تطبیق میدهد. وقتی که یک مقدار `Coin::Quarter` منطبق میشود، متغیر `state` به مقدار ایالت آن ربع متصل خواهد شد. سپس میتوانیم از `state` در کد بازوی آن استفاده کنیم، به این صورت:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-09-variable-in-pattern/src/main.rs:here}}
```
-If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin`
-would be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each
-of the match arms, none of them match until we reach `Coin::Quarter(state)`. At
-that point, the binding for `state` will be the value `UsState::Alaska`. We can
-then use that binding in the `println!` expression, thus getting the inner
-state value out of the `Coin` enum variant for `Quarter`.
+اگر ما `value_in_cents(Coin::Quarter(UsState::Alaska))` را فراخوانی کنیم، مقدار `coin` برابر با `Coin::Quarter(UsState::Alaska)` خواهد بود. هنگامی که آن مقدار را با هر بازوی `match` مقایسه میکنیم، هیچکدام از آنها مطابقت ندارند تا اینکه به `Coin::Quarter(state)` برسیم. در این نقطه، اتصال برای `state` مقدار `UsState::Alaska` خواهد بود. سپس میتوانیم از آن اتصال در عبارت `println!` استفاده کنیم و به این ترتیب مقدار داخلی ایالت را از حالت `Quarter` enum `Coin` استخراج کنیم.
-### Matching with `Option`
+### تطبیق با `Option`
-In the previous section, we wanted to get the inner `T` value out of the `Some`
-case when using `Option`; we can also handle `Option` using `match`, as
-we did with the `Coin` enum! Instead of comparing coins, we’ll compare the
-variants of `Option`, but the way the `match` expression works remains the
-same.
+در بخش قبلی، ما میخواستیم مقدار داخلی `T` را از حالت `Some` استخراج کنیم زمانی که از `Option` استفاده میکردیم؛ همچنین میتوانیم با استفاده از `match` حالتهای `Option` را مدیریت کنیم، همانطور که با enum `Coin` انجام دادیم! به جای مقایسه سکهها، حالتهای `Option` را مقایسه میکنیم، اما روش کار عبارت `match` همان باقی میماند.
-Let’s say we want to write a function that takes an `Option` and, if
-there’s a value inside, adds 1 to that value. If there isn’t a value inside,
-the function should return the `None` value and not attempt to perform any
-operations.
+بیایید فرض کنیم که میخواهیم تابعی بنویسیم که یک `Option` بگیرد و اگر یک مقدار درون آن باشد، مقدار 1 را به آن اضافه کند. اگر هیچ مقداری درون آن نباشد، تابع باید مقدار `None` را بازگرداند و هیچ عملیاتی را انجام ندهد.
-This function is very easy to write, thanks to `match`, and will look like
-Listing 6-5.
-
-
+نوشتن این تابع با استفاده از `match` بسیار آسان است و به این صورت خواهد بود:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:here}}
```
-
-
-Let’s examine the first execution of `plus_one` in more detail. When we call
-`plus_one(five)`, the variable `x` in the body of `plus_one` will have the
-value `Some(5)`. We then compare that against each match arm:
+اجازه دهید اولین اجرای `plus_one` را با جزئیات بیشتری بررسی کنیم. وقتی که `plus_one(five)` را فراخوانی میکنیم، متغیر `x` در بدنه `plus_one` مقدار `Some(5)` خواهد داشت. سپس آن را با هر بازوی `match` مقایسه میکنیم:
```rust,ignore
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:first_arm}}
```
-The `Some(5)` value doesn’t match the pattern `None`, so we continue to the
-next arm:
+مقدار `Some(5)` با الگوی `None` مطابقت ندارد، بنابراین به بازوی بعدی میرویم:
```rust,ignore
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:second_arm}}
```
-Does `Some(5)` match `Some(i)`? It does! We have the same variant. The `i`
-binds to the value contained in `Some`, so `i` takes the value `5`. The code in
-the match arm is then executed, so we add 1 to the value of `i` and create a
-new `Some` value with our total `6` inside.
+آیا `Some(5)` با `Some(i)` مطابقت دارد؟ بله! ما همان حالت را داریم. مقدار `i` به مقدار داخل `Some` متصل میشود، بنابراین `i` مقدار `5` میگیرد. سپس کد موجود در بازوی `match` اجرا میشود، بنابراین مقدار 1 به مقدار `i` اضافه میکنیم و یک مقدار جدید `Some` با مقدار کل `6` ایجاد میکنیم.
-Now let’s consider the second call of `plus_one` in Listing 6-5, where `x` is
-`None`. We enter the `match` and compare to the first arm:
+حالا اجازه دهید اجرای دوم `plus_one` را در فهرست 6-5 در نظر بگیریم، جایی که مقدار `x` برابر با `None` است. ما وارد `match` میشویم و آن را با اولین بازو مقایسه میکنیم:
```rust,ignore
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:first_arm}}
```
-It matches! There’s no value to add to, so the program stops and returns the
-`None` value on the right side of `=>`. Because the first arm matched, no other
-arms are compared.
+این بار مطابقت دارد! هیچ مقداری برای اضافه کردن وجود ندارد، بنابراین برنامه متوقف میشود و مقدار `None` در سمت راست `=>` را بازمیگرداند. از آنجا که اولین بازو مطابقت داشت، بازوهای دیگر بررسی نمیشوند.
-Combining `match` and enums is useful in many situations. You’ll see this
-pattern a lot in Rust code: `match` against an enum, bind a variable to the
-data inside, and then execute code based on it. It’s a bit tricky at first, but
-once you get used to it, you’ll wish you had it in all languages. It’s
-consistently a user favorite.
+ترکیب `match` و enumها در بسیاری از موقعیتها مفید است. این الگو را در کد Rust زیاد خواهید دید: `match` روی یک enum، اتصال یک متغیر به داده داخل، و سپس اجرای کد بر اساس آن. ممکن است در ابتدا کمی سخت باشد، اما وقتی به آن عادت کنید، آرزو خواهید کرد که در همه زبانها وجود داشته باشد. این سازه همواره یکی از ویژگیهای مورد علاقه کاربران است.
-### Matches Are Exhaustive
+### تطابقها Exhaustive هستند
-There’s one other aspect of `match` we need to discuss: the arms’ patterns must
-cover all possibilities. Consider this version of our `plus_one` function,
-which has a bug and won’t compile:
+یکی دیگر از جنبههای عبارت `match` این است که الگوهای بازوها باید تمام حالتهای ممکن را پوشش دهند. به این نسخه از تابع `plus_one` که یک باگ دارد و کامپایل نمیشود توجه کنید:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs:here}}
```
-We didn’t handle the `None` case, so this code will cause a bug. Luckily, it’s
-a bug Rust knows how to catch. If we try to compile this code, we’ll get this
-error:
+ما حالت `None` را مدیریت نکردهایم، بنابراین این کد باعث بروز یک باگ خواهد شد. خوشبختانه، این یک باگ است که Rust میتواند آن را تشخیص دهد. اگر تلاش کنیم این کد را کامپایل کنیم، این خطا را دریافت خواهیم کرد:
```console
{{#include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/output.txt}}
```
-Rust knows that we didn’t cover every possible case, and even knows which
-pattern we forgot! Matches in Rust are _exhaustive_: we must exhaust every last
-possibility in order for the code to be valid. Especially in the case of
-`Option`, when Rust prevents us from forgetting to explicitly handle the
-`None` case, it protects us from assuming that we have a value when we might
-have null, thus making the billion-dollar mistake discussed earlier impossible.
-
-### Catch-All Patterns and the `_` Placeholder
-
-Using enums, we can also take special actions for a few particular values, but
-for all other values take one default action. Imagine we’re implementing a game
-where, if you roll a 3 on a dice roll, your player doesn’t move, but instead
-gets a new fancy hat. If you roll a 7, your player loses a fancy hat. For all
-other values, your player moves that number of spaces on the game board. Here’s
-a `match` that implements that logic, with the result of the dice roll
-hardcoded rather than a random value, and all other logic represented by
-functions without bodies because actually implementing them is out of scope for
-this example:
+Rust میداند که ما هر حالت ممکن را پوشش ندادهایم و حتی میداند که کدام الگو را فراموش کردهایم! تطابقها در Rust _exhaustive_ هستند: ما باید هر حالت ممکن را مدیریت کنیم تا کد معتبر باشد. به ویژه در مورد `Option`، وقتی که Rust از فراموش کردن مدیریت صریح حالت `None` جلوگیری میکند، از فرض نادرست وجود مقدار زمانی که ممکن است null باشد محافظت میکند و به این ترتیب اشتباه میلیارد دلاری که قبلاً بحث شد را غیرممکن میسازد.
+
+### الگوهای Catch-all و Placeholder `_`
+
+با استفاده از Enumها، میتوانیم اقدامات ویژهای برای چند مقدار خاص انجام دهیم، اما برای تمام مقادیر دیگر یک عمل پیشفرض داشته باشیم. تصور کنید که در حال پیادهسازی یک بازی هستید که اگر بازیکن عدد 3 روی تاس بیاورد، حرکت نمیکند اما یک کلاه زیبا جدید میگیرد. اگر عدد 7 بیاورد، بازیکن یک کلاه زیبا از دست میدهد. برای تمام مقادیر دیگر، بازیکن به اندازه عدد روی تخته بازی حرکت میکند. در اینجا یک عبارت `match` آورده شده است که این منطق را پیادهسازی میکند. نتیجهی پرتاب تاس به جای مقدار تصادفی، به صورت هاردکد شده قرار داده شده است، و تمام منطق دیگر با توابعی بدون بدنه نشان داده شدهاند زیرا پیادهسازی آنها خارج از محدوده این مثال است:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-15-binding-catchall/src/main.rs:here}}
```
-For the first two arms, the patterns are the literal values `3` and `7`. For
-the last arm that covers every other possible value, the pattern is the
-variable we’ve chosen to name `other`. The code that runs for the `other` arm
-uses the variable by passing it to the `move_player` function.
+برای دو بازوی اول، الگوها مقادیر ثابت `3` و `7` هستند. برای بازوی آخر که تمام مقادیر ممکن دیگر را پوشش میدهد، الگو یک متغیر است که ما آن را `other` نامیدهایم. کدی که برای بازوی `other` اجرا میشود، متغیر را با استفاده از تابع `move_player` میفرستد.
-This code compiles, even though we haven’t listed all the possible values a
-`u8` can have, because the last pattern will match all values not specifically
-listed. This catch-all pattern meets the requirement that `match` must be
-exhaustive. Note that we have to put the catch-all arm last because the
-patterns are evaluated in order. If we put the catch-all arm earlier, the other
-arms would never run, so Rust will warn us if we add arms after a catch-all!
+این کد کامپایل میشود، حتی اگر تمام مقادیر ممکن یک `u8` را فهرست نکرده باشیم، زیرا بازوی آخر همه مقادیر ذکر نشده را تطبیق میدهد. این الگوی catch-all نیاز تطابق exhaustive را برآورده میکند. توجه داشته باشید که باید بازوی catch-all را در آخر قرار دهیم زیرا الگوها به ترتیب ارزیابی میشوند. اگر بازوی catch-all را زودتر قرار دهیم، بازوهای دیگر هرگز اجرا نخواهند شد، بنابراین Rust به ما هشدار میدهد اگر بعد از یک بازوی catch-all بازوهای دیگری اضافه کنیم!
-Rust also has a pattern we can use when we want a catch-all but don’t want to
-_use_ the value in the catch-all pattern: `_` is a special pattern that matches
-any value and does not bind to that value. This tells Rust we aren’t going to
-use the value, so Rust won’t warn us about an unused variable.
+Rust همچنین یک الگو به نام `_` دارد که میتوانیم از آن استفاده کنیم وقتی که میخواهیم یک catch-all داشته باشیم اما نمیخواهیم مقدار در الگوی catch-all را _استفاده کنیم_. این به Rust میگوید که ما قصد نداریم مقدار را استفاده کنیم، بنابراین Rust درباره یک متغیر استفاده نشده به ما هشدار نمیدهد.
-Let’s change the rules of the game: now, if you roll anything other than a 3 or
-a 7, you must roll again. We no longer need to use the catch-all value, so we
-can change our code to use `_` instead of the variable named `other`:
+بیایید قوانین بازی را تغییر دهیم: حالا اگر بازیکن هر چیزی به غیر از 3 یا 7 بیاورد، باید دوباره تاس بیندازد. دیگر نیازی به استفاده از مقدار catch-all نیست، بنابراین میتوانیم کد خود را بهجای متغیری به نام `other` از `_` استفاده کنیم:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-16-underscore-catchall/src/main.rs:here}}
```
-This example also meets the exhaustiveness requirement because we’re explicitly
-ignoring all other values in the last arm; we haven’t forgotten anything.
+این مثال نیز نیاز تطابق exhaustive را برآورده میکند زیرا ما صریحاً تمام مقادیر دیگر را در بازوی آخر نادیده گرفتهایم و چیزی را فراموش نکردهایم.
-Finally, we’ll change the rules of the game one more time so that nothing else
-happens on your turn if you roll anything other than a 3 or a 7. We can express
-that by using the unit value (the empty tuple type we mentioned in [“The Tuple
-Type”][tuples] section) as the code that goes with the `_` arm:
+در نهایت، قوانین بازی را یک بار دیگر تغییر میدهیم، بنابراین اگر بازیکن هر چیزی غیر از 3 یا 7 بیاورد، هیچ کار دیگری در نوبت او انجام نمیشود. میتوانیم این موضوع را با استفاده از مقدار واحد (نوع `tuple` خالی که قبلاً در بخش [“نوع Tuple”][tuples] ذکر شد) به عنوان کدی که با بازوی `_` همراه است بیان کنیم:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-17-underscore-unit/src/main.rs:here}}
```
-Here, we’re telling Rust explicitly that we aren’t going to use any other value
-that doesn’t match a pattern in an earlier arm, and we don’t want to run any
-code in this case.
+اینجا، ما به Rust صریحاً میگوییم که قصد نداریم هیچ مقدار دیگری را که با هیچ الگویی در بازوهای قبلی مطابقت ندارد استفاده کنیم و نمیخواهیم در این حالت کدی اجرا کنیم.
+
-There’s more about patterns and matching that we’ll cover in [Chapter
-19][ch19-00-patterns]. For now, we’re going to move on to the
-`if let` syntax, which can be useful in situations where the `match` expression
-is a bit wordy.
+درباره الگوها و تطبیق آنها مطالب بیشتری در [فصل 19][ch19-00-patterns] پوشش خواهیم داد. فعلاً به سینتکس `if let` میپردازیم که میتواند در مواقعی که عبارت `match` کمی طولانی به نظر میرسد، مفید باشد.
[tuples]: ch03-02-data-types.html#the-tuple-type
[ch19-00-patterns]: ch19-00-patterns.html
diff --git a/src/ch06-03-if-let.md b/src/ch06-03-if-let.md
index b22d7b894e..1696c10417 100644
--- a/src/ch06-03-if-let.md
+++ b/src/ch06-03-if-let.md
@@ -1,12 +1,8 @@
-## Concise Control Flow with `if let` and `let else`
+## جریان کنترلی مختصر با `if let` و `let else`
-The `if let` syntax lets you combine `if` and `let` into a less verbose way to
-handle values that match one pattern while ignoring the rest. Consider the
-program in Listing 6-6 that matches on an `Option` value in the
-`config_max` variable but only wants to execute code if the value is the `Some`
-variant.
+دستور `if let` به شما اجازه میدهد که `if` و `let` را ترکیب کنید و به شکلی کمتر پرحجم، مقادیر مطابق با یک الگو را مدیریت کنید و سایر مقادیر را نادیده بگیرید. برنامهای که در لیستینگ 6-6 نشان داده شده است، بر روی یک مقدار `Option` در متغیر `config_max` مطابقت دارد، اما تنها زمانی که مقدار `Some` باشد کد را اجرا میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs:here}}
@@ -14,70 +10,50 @@ variant.
-If the value is `Some`, we print out the value in the `Some` variant by binding
-the value to the variable `max` in the pattern. We don’t want to do anything
-with the `None` value. To satisfy the `match` expression, we have to add `_ =>
-()` after processing just one variant, which is annoying boilerplate code to
-add.
+اگر مقدار `Some` باشد، مقدار موجود در متغیر `Some` را با اتصال به متغیر `max` در الگو چاپ میکنیم. ما نمیخواهیم با مقدار `None` کاری انجام دهیم. برای برآورده کردن دستور `match`، باید `_ => ()` را بعد از پردازش تنها یک مورد اضافه کنیم، که کد اضافی آزاردهندهای است.
-Instead, we could write this in a shorter way using `if let`. The following
-code behaves the same as the `match` in Listing 6-6:
+در عوض، میتوانیم این کد را به شکلی کوتاهتر با استفاده از `if let` بنویسیم. کد زیر به همان شکل `match` در لیستینگ 6-6 عمل میکند:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-12-if-let/src/main.rs:here}}
```
-The syntax `if let` takes a pattern and an expression separated by an equal
-sign. It works the same way as a `match`, where the expression is given to the
-`match` and the pattern is its first arm. In this case, the pattern is
-`Some(max)`, and the `max` binds to the value inside the `Some`. We can then
-use `max` in the body of the `if let` block in the same way we used `max` in
-the corresponding `match` arm. The code in the `if let` block only runs if the
-value matches the pattern.
-
-Using `if let` means less typing, less indentation, and less boilerplate code.
-However, you lose the exhaustive checking `match` enforces that ensures you
-aren’t forgetting to handle any cases. Choosing between `match` and `if let`
-depends on what you’re doing in your particular situation and whether gaining
-conciseness is an appropriate trade-off for losing exhaustive checking.
-
-In other words, you can think of `if let` as syntax sugar for a `match` that
-runs code when the value matches one pattern and then ignores all other values.
-
-We can include an `else` with an `if let`. The block of code that goes with the
-`else` is the same as the block of code that would go with the `_` case in the
-`match` expression that is equivalent to the `if let` and `else`. Recall the
-`Coin` enum definition in Listing 6-4, where the `Quarter` variant also held a
-`UsState` value. If we wanted to count all non-quarter coins we see while also
-announcing the state of the quarters, we could do that with a `match`
-expression, like this:
+دستور `if let` یک الگو و یک عبارت را میگیرد که با یک علامت مساوی جدا شدهاند. این دستور همانند `match` عمل میکند، جایی که عبارت به `match` داده میشود و الگو بازوی اول آن است. در این مورد، الگو `Some(max)` است و متغیر `max` مقدار داخل `Some` را میگیرد. سپس میتوانیم از `max` در بدنه بلوک `if let` همانطور که در بازوی متناظر `match` استفاده کردیم، استفاده کنیم. کد در بلوک `if let` تنها در صورتی اجرا میشود که مقدار با الگو مطابقت داشته باشد.
+
+استفاده از `if let` به معنای تایپ کمتر، تورفتگی کمتر، و کدنویسی قالبی (boilerplate) کمتر است.
+با این حال، بررسی جامعای که `match` تحمیل میکند را از دست میدهید؛ این بررسی تضمین میکند
+که هیچ حالتی را فراموش نکرده باشید. انتخاب بین `match` و `if let` بستگی به کاری دارد که
+در موقعیت خاص خود انجام میدهید و اینکه آیا دستیابی به اختصار، مبادلهای مناسب در برابر از دست دادن بررسی جامع است یا خیر.
+
+به عبارت دیگر، میتوانید `if let` را به عنوان یک قند سینتکس برای `match` تصور کنید که کد را زمانی که مقدار با یک الگو مطابقت دارد اجرا میکند و سپس تمام مقادیر دیگر را نادیده میگیرد.
+
+ما میتوانیم یک `else` با یک `if let` اضافه کنیم. بلوک کدی که با `else` همراه میشود همان بلوک کدی است که با مورد `_` در دستور `match` که معادل `if let` و `else` است همراه میشود. دستور `Coin` را در لیستینگ 6-4 به یاد بیاورید، جایی که نوع `Quarter` یک مقدار `UsState` را نیز در خود جای داده بود. اگر میخواستیم تمام سکههای غیر `Quarter` را که میبینیم بشماریم، همزمان ایالتهای سکههای `Quarter` را اعلام کنیم، میتوانستیم این کار را با یک دستور `match` انجام دهیم، مانند این:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-13-count-and-announce-match/src/main.rs:here}}
```
-Or we could use an `if let` and `else` expression, like this:
+یا میتوانستیم از یک عبارت `if let` و `else` استفاده کنیم، مانند این:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-14-count-and-announce-if-let-else/src/main.rs:here}}
```
-## Staying on the “Happy Path” with `let...else`
+## ماندن در «مسیر خوشحال» با `let...else`
-The common pattern is to perform some computation when a value is present and
-return a default value otherwise. Continuing on with our example of coins with a
-`UsState` value, if we wanted to say something funny depending on how old the
-state on the quarter was, we might introduce a method on `UsState` to check the
-age of a state, like so:
+الگوی رایج این است که وقتی یک مقدار موجود است، عملیاتی را انجام دهیم و در غیر این صورت،
+یک مقدار پیشفرض را بازگردانیم. با ادامه دادن مثال سکهها با یک مقدار `UsState`،
+اگر بخواهیم بر اساس قدمت ایالتی که روی سکهی ۲۵ سنتی (quarter) است،
+چیزی خندهدار بگوییم، میتوانیم یک متد روی `UsState` تعریف کنیم تا سن ایالت را بررسی کند،
+مانند مثال زیر:
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-07/src/main.rs:state}}
```
-Then we might use `if let` to match on the type of coin, introducing a `state`
-variable within the body of the condition, as in Listing 6-7.
+سپس ممکن است از `if let` برای مطابقت با نوع سکه استفاده کنیم، متغیری به نام `state` را در بدنه شرط معرفی کنیم، همانطور که در لیستینگ 6-7 نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-07/src/main.rs:describe}}
@@ -85,14 +61,13 @@ variable within the body of the condition, as in Listing 6-7.
-That gets the job done, but it has pushed the work into the body of the `if
-let` statement, and if the work to be done is more complicated, it might be
-hard to follow exactly how the top-level branches relate. We could also take
-advantage of the fact that expressions produce a value either to produce the
-`state` from the `if let` or to return early, as in Listing 6-8. (You could do
-similar with a `match`, too.)
+این کار انجام میشود، اما منطق اجرا را به درون بدنهی عبارت `if let` منتقل کردهایم،
+و اگر کار مورد نظر پیچیدهتر باشد، ممکن است دنبال کردن ارتباط شاخههای سطح بالا دشوار شود.
+همچنین میتوانیم از این واقعیت بهره ببریم که عبارات یک مقدار تولید میکنند—
+یا برای تولید `state` از `if let` استفاده کنیم یا زودتر بازگردیم، همانطور که در لیستینگ 6-8 نشان داده شده است.
+(میتوانید کار مشابهی را با `match` نیز انجام دهید.)
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-08/src/main.rs:describe}}
@@ -100,20 +75,19 @@ similar with a `match`, too.)
-This is a bit annoying to follow in its own way, though! One branch of the `if
-let` produces a value, and the other one returns from the function entirely.
+این تا حدی آزاردهنده است! یک شاخه `if let` یک مقدار تولید میکند و دیگری کاملاً از تابع بازمیگردد.
-To make this common pattern nicer to express, Rust has `let...else`. The
-`let...else` syntax takes a pattern on the left side and an expression on the
-right, very similar to `if let`, but it does not have an `if` branch, only an
-`else` branch. If the pattern matches, it will bind the value from the pattern
-in the outer scope. If the pattern does _not_ match, the program will flow into
-the `else` arm, which must return from the function.
+برای بیان سادهتر این الگوی رایج، Rust ساختار `let...else` را ارائه داده است.
+سینتکس `let...else` یک الگو در سمت چپ و یک عبارت در سمت راست میگیرد،
+که بسیار شبیه به `if let` است، اما شاخهی `if` ندارد و تنها یک شاخهی `else` دارد.
+اگر الگو با مقدار مطابقت داشته باشد، مقدار از درون الگو در حوزهی بیرونی (outer scope) بایند خواهد شد.
+اگر الگو *مطابقت نداشته باشد*، برنامه وارد شاخهی `else` خواهد شد،
+که باید از تابع بازگردد.
-In Listing 6-9, you can see how Listing 6-8 looks when using `let...else` in
-place of `if let`.
+در لیستینگ 6-9 میتوانید ببینید که چگونه لیستینگ 6-8 با استفاده از `let...else`
+به جای `if let` بازنویسی شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-09/src/main.rs:describe}}
@@ -121,27 +95,16 @@ place of `if let`.
-Notice that it stays “on the happy path” in the main body of the function this
-way, without having significantly different control flow for two branches the
-way the `if let` did.
+توجه داشته باشید که با این روش، بدنهی اصلی تابع در «مسیر خوشحال» باقی میماند،
+بدون آنکه مانند `if let` جریان کنترل متفاوت و قابلتوجهی بین دو شاخه ایجاد کند.
-If you have a situation in which your program has logic that is too verbose to
-express using a `match`, remember that `if let` and `let...else` are in your
-Rust toolbox as well.
+اگر در موقعیتی هستید که منطق برنامهتان برای بیان با استفاده از `match` بیش از حد مفصل است،
+به یاد داشته باشید که `if let` و `let...else` نیز در جعبهابزار Rust شما قرار دارند.
-## Summary
+## خلاصه
-We’ve now covered how to use enums to create custom types that can be one of a
-set of enumerated values. We’ve shown how the standard library’s `Option`
-type helps you use the type system to prevent errors. When enum values have
-data inside them, you can use `match` or `if let` to extract and use those
-values, depending on how many cases you need to handle.
+ما اکنون پوشش دادهایم که چگونه از enumها برای ایجاد انواع سفارشی که میتوانند یکی از مجموعه مقادیر شمارششده باشند استفاده کنید. ما نشان دادهایم که چگونه نوع `Option` از کتابخانه استاندارد به شما کمک میکند از سیستم نوع برای جلوگیری از خطاها استفاده کنید. وقتی مقادیر enum دادههایی درون خود دارند، میتوانید از `match` یا `if let` برای استخراج و استفاده از آن مقادیر استفاده کنید، بسته به تعداد مواردی که باید مدیریت کنید.
-Your Rust programs can now express concepts in your domain using structs and
-enums. Creating custom types to use in your API ensures type safety: the
-compiler will make certain your functions only get values of the type each
-function expects.
+برنامههای Rust شما اکنون میتوانند مفاهیمی را در حوزه خود بیان کنند و از ساختارها و enumها استفاده کنند. ایجاد انواع سفارشی برای استفاده در API شما ایمنی نوع را تضمین میکند: کامپایلر مطمئن میشود که توابع شما فقط مقادیری از نوعی که هر تابع انتظار دارد دریافت میکنند.
-In order to provide a well-organized API to your users that is straightforward
-to use and only exposes exactly what your users will need, let’s now turn to
-Rust’s modules.
+برای ارائه یک API سازمانیافته به کاربران خود که استفاده از آن ساده باشد و فقط دقیقاً آنچه کاربران شما نیاز دارند را آشکار کند، حالا به ماژولهای Rust میپردازیم.
diff --git a/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md b/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md
index 22afd6000f..fa8a12c222 100644
--- a/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md
+++ b/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md
@@ -1,48 +1,27 @@
-# Managing Growing Projects with Packages, Crates, and Modules
-
-As you write large programs, organizing your code will become increasingly
-important. By grouping related functionality and separating code with distinct
-features, you’ll clarify where to find code that implements a particular
-feature and where to go to change how a feature works.
-
-The programs we’ve written so far have been in one module in one file. As a
-project grows, you should organize code by splitting it into multiple modules
-and then multiple files. A package can contain multiple binary crates and
-optionally one library crate. As a package grows, you can extract parts into
-separate crates that become external dependencies. This chapter covers all
-these techniques. For very large projects comprising a set of interrelated
-packages that evolve together, Cargo provides _workspaces_, which we’ll cover
-in [“Cargo Workspaces”][workspaces] in Chapter 14.
-
-We’ll also discuss encapsulating implementation details, which lets you reuse
-code at a higher level: once you’ve implemented an operation, other code can
-call your code via its public interface without having to know how the
-implementation works. The way you write code defines which parts are public for
-other code to use and which parts are private implementation details that you
-reserve the right to change. This is another way to limit the amount of detail
-you have to keep in your head.
-
-A related concept is scope: the nested context in which code is written has a
-set of names that are defined as “in scope.” When reading, writing, and
-compiling code, programmers and compilers need to know whether a particular
-name at a particular spot refers to a variable, function, struct, enum, module,
-constant, or other item and what that item means. You can create scopes and
-change which names are in or out of scope. You can’t have two items with the
-same name in the same scope; tools are available to resolve name conflicts.
-
-Rust has a number of features that allow you to manage your code’s
-organization, including which details are exposed, which details are private,
-and what names are in each scope in your programs. These features, sometimes
-collectively referred to as the _module system_, include:
-
-* **Packages**: A Cargo feature that lets you build, test, and share crates
-* **Crates**: A tree of modules that produces a library or executable
-* **Modules and use**: Let you control the organization, scope, and privacy of
-paths
-* **Paths**: A way of naming an item, such as a struct, function, or module
-
-In this chapter, we’ll cover all these features, discuss how they interact, and
-explain how to use them to manage scope. By the end, you should have a solid
-understanding of the module system and be able to work with scopes like a pro!
+# مدیریت پروژههای بزرگ با بستهها، جعبهها (crates) و ماژولها
+
+با نوشتن برنامههای بزرگتر، سازماندهی کد شما اهمیت بیشتری پیدا میکند. با گروهبندی قابلیتهای مرتبط و جدا کردن کدی که ویژگیهای متمایزی دارد، میتوانید مشخص کنید که کد یک ویژگی خاص در کجا پیادهسازی شده و کجا میتوان آن را تغییر داد.
+
+برنامههایی که تا اینجا نوشتهایم، همگی در یک ماژول و در یک فایل بودهاند.
+با رشد یک پروژه، باید کد را با تقسیم آن به چند ماژول و سپس چند فایل، سازماندهی کنید.
+یک پکیج میتواند شامل چندین crate دودویی باشد و بهصورت اختیاری یک crate کتابخانهای نیز داشته باشد.
+با گسترش یک پکیج، میتوانید بخشهایی از آن را به crateهای جداگانه استخراج کنید
+که به وابستگیهای خارجی تبدیل میشوند. این فصل تمام این تکنیکها را پوشش میدهد.
+برای پروژههای بسیار بزرگی که از مجموعهای از پکیجهای مرتبط بههم تشکیل شدهاند
+و با هم رشد میکنند، Cargo قابلیتی به نام *workspaces* ارائه میدهد
+که آن را در فصل ۱۴ با عنوان [“Cargo Workspaces”][workspaces] بررسی خواهیم کرد.
+
+همچنین درباره جزئیات پیادهسازی که به شما امکان میدهد کد را در سطح بالاتری بازاستفاده کنید صحبت خواهیم کرد: وقتی یک عملیات را پیادهسازی کردهاید، سایر کدها میتوانند از طریق رابط عمومی کد شما آن را فراخوانی کنند بدون این که لازم باشد بدانند چگونه پیادهسازی شده است. نحوه نوشتن کد شما مشخص میکند که کدام بخشها برای سایر کدها عمومی و قابل استفاده هستند و کدام بخشها جزئیات پیادهسازی خصوصی هستند که میتوانید هر زمان بخواهید تغییر دهید. این رویکرد یکی دیگر از روشهایی است که مقدار جزئیاتی که باید به خاطر بسپارید را محدود میکند.
+
+یک مفهوم مرتبط، محدوده (scope) است: زمینهای که در آن کد نوشته شده است و مجموعهای از نامها که به عنوان «در محدوده» تعریف میشوند. هنگام خواندن، نوشتن و کامپایل کد، برنامهنویسان و کامپایلرها باید بدانند که آیا یک نام خاص در یک مکان خاص به متغیر، تابع، ساختار، enum، ماژول، ثابت یا مورد دیگری اشاره دارد و معنای آن مورد چیست. شما میتوانید محدودهها ایجاد کنید و مشخص کنید که کدام نامها در محدوده هستند یا خارج از آن. نمیتوانید دو مورد با نام یکسان در یک محدوده داشته باشید؛ ابزارهایی برای رفع تعارض نامها در دسترس هستند.
+
+Rust مجموعهای از ویژگیها دارد که به شما امکان میدهد سازماندهی کد خود را مدیریت کنید، از جمله جزئیاتی که آشکار میشوند، جزئیاتی که خصوصی هستند، و نامهایی که در هر محدوده در برنامههای شما قرار دارند. این ویژگیها که گاهی به صورت جمعی _سیستم ماژول_ نامیده میشوند شامل موارد زیر هستند:
+
+* **Packages**: یکی از قابلیتهای Cargo که به شما اجازه میدهد crateها را بسازید، تست کنید و به اشتراک بگذارید
+* **Crates**: یک درخت از ماژولها که یک کتابخانه یا فایل اجرایی تولید میکند
+* **Modules و use**: به شما امکان میدهد سازماندهی، حوزه (scope)، و سطح دسترسی مسیرها را کنترل کنید
+* **Paths**: روشی برای نامگذاری یک آیتم، مانند یک `struct`، تابع، یا ماژول
+
+در این فصل، تمام این ویژگیها را پوشش خواهیم داد، نحوه تعامل آنها را توضیح میدهیم و نحوه استفاده از آنها برای مدیریت محدوده را بررسی میکنیم. تا پایان، باید درک جامعی از سیستم ماژول داشته باشید و بتوانید با محدودهها مانند یک حرفهای کار کنید!
[workspaces]: ch14-03-cargo-workspaces.html
diff --git a/src/ch07-01-packages-and-crates.md b/src/ch07-01-packages-and-crates.md
index da9ea55651..e933ebea39 100644
--- a/src/ch07-01-packages-and-crates.md
+++ b/src/ch07-01-packages-and-crates.md
@@ -1,45 +1,42 @@
-## Packages and Crates
-
-The first parts of the module system we’ll cover are packages and crates.
-
-A _crate_ is the smallest amount of code that the Rust compiler considers at a
-time. Even if you run `rustc` rather than `cargo` and pass a single source code
-file (as we did all the way back in “Writing and Running a Rust Program” in
-Chapter 1), the compiler considers that file to be a crate. Crates can contain
-modules, and the modules may be defined in other files that get compiled with
-the crate, as we’ll see in the coming sections.
-
-A crate can come in one of two forms: a binary crate or a library crate.
-_Binary crates_ are programs you can compile to an executable that you can run,
-such as a command line program or a server. Each must have a function called
-`main` that defines what happens when the executable runs. All the crates we’ve
-created so far have been binary crates.
-
-_Library crates_ don’t have a `main` function, and they don’t compile to an
-executable. Instead, they define functionality intended to be shared with
-multiple projects. For example, the `rand` crate we used in [Chapter
-2][rand] provides functionality that generates random numbers.
-Most of the time when Rustaceans say “crate,” they mean library crate, and they
-use “crate” interchangeably with the general programming concept of a “library.”
-
-The _crate root_ is a source file that the Rust compiler starts from and makes
-up the root module of your crate (we’ll explain modules in depth in [“Defining
-Modules to Control Scope and Privacy”][modules]).
-
-A _package_ is a bundle of one or more crates that provides a set of
-functionality. A package contains a _Cargo.toml_ file that describes how to
-build those crates. Cargo is actually a package that contains the binary crate
-for the command line tool you’ve been using to build your code. The Cargo
-package also contains a library crate that the binary crate depends on. Other
-projects can depend on the Cargo library crate to use the same logic the Cargo
-command line tool uses.
-
-A package can contain as many binary crates as you like, but at most only one
-library crate. A package must contain at least one crate, whether that’s a
-library or binary crate.
-
-Let’s walk through what happens when we create a package. First we enter the
-command `cargo new my-project`:
+## بستهها و جعبهها (crates)
+
+اولین بخشهایی که در سیستم ماژول بررسی خواهیم کرد، بستهها و جعبهها (crates) هستند.
+
+یک *crate* کوچکترین واحدی از کد است که کامپایلر Rust در هر لحظه به آن توجه میکند.
+حتی اگر به جای استفاده از `cargo`، مستقیماً `rustc` را اجرا کنید و تنها یک فایل کد منبع را (همانطور که در فصل اول در بخش «نوشتن و اجرای یک برنامه Rust» انجام دادیم) به آن بدهید،
+کامپایلر آن فایل را به عنوان یک crate در نظر میگیرد.
+crateها میتوانند شامل ماژولهایی باشند، و این ماژولها ممکن است در فایلهای دیگری تعریف شده باشند
+که هنگام کامپایل، همراه با crate پردازش میشوند، همانطور که در بخشهای بعدی خواهیم دید.
+
+یک crate میتواند یکی از دو نوع زیر باشد: crate دودویی (binary) یا crate کتابخانهای (library).
+*crateهای دودویی* برنامههایی هستند که میتوانید آنها را به فایل اجرایی کامپایل کرده و اجرا کنید،
+مانند یک برنامهی خط فرمان یا یک سرور. هر crate دودویی باید تابعی به نام `main` داشته باشد
+که مشخص میکند هنگام اجرای فایل اجرایی، چه اتفاقی میافتد.
+تمام crateهایی که تا اینجا ایجاد کردهایم، crateهای دودویی بودهاند.
+
+*crateهای کتابخانهای* تابع `main` ندارند و به فایل اجرایی کامپایل نمیشوند.
+در عوض، آنها قابلیتهایی را تعریف میکنند که برای اشتراکگذاری میان پروژههای مختلف در نظر گرفته شدهاند.
+برای مثال، crate `rand` که در [فصل ۲][rand] از آن استفاده کردیم، قابلیتهایی برای تولید اعداد تصادفی فراهم میکند.
+در اغلب موارد، زمانی که Rustaceanها از واژهی “crate” استفاده میکنند، منظورشان crate کتابخانهای است
+و این واژه را بهطور معادل با مفهوم عمومی «کتابخانه» در برنامهنویسی به کار میبرند.
+
+*ریشهی crate* (crate root) فایلی از کد منبع است که کامپایلر Rust از آن شروع میکند
+و ماژول ریشهی crate را تشکیل میدهد (ماژولها را در بخش [“تعریف ماژولها برای کنترل حوزه و سطح دسترسی”][modules] با جزئیات توضیح خواهیم داد).
+
+یک *package* مجموعهای از یک یا چند crate است که مجموعهای از قابلیتها را ارائه میدهد.
+یک package شامل یک فایل *Cargo.toml* است که مشخص میکند چگونه crateها باید ساخته شوند.
+خود Cargo در واقع یک package است که شامل یک crate دودویی برای ابزار خط فرمانی است
+که تاکنون از آن برای ساخت کد خود استفاده کردهاید.
+پکیج Cargo همچنین شامل یک crate کتابخانهای است که crate دودویی به آن وابسته است.
+سایر پروژهها میتوانند به crate کتابخانهای Cargo وابسته شوند تا از همان منطق استفاده کنند
+که ابزار خط فرمان Cargo از آن بهره میبرد.
+
+یک package میتواند هر تعداد crate دودویی داشته باشد،
+اما در بیشترین حالت، تنها یک crate کتابخانهای میتواند داشته باشد.
+هر package باید دستکم شامل یک crate باشد،
+چه crate کتابخانهای و چه crate دودویی.
+
+بیایید ببینیم وقتی یک بسته ایجاد میکنیم چه اتفاقی میافتد. ابتدا دستور `cargo new my-project` را وارد میکنیم:
```console
$ cargo new my-project
@@ -51,21 +48,9 @@ $ ls my-project/src
main.rs
```
-After we run `cargo new my-project`, we use `ls` to see what Cargo creates. In
-the project directory, there’s a _Cargo.toml_ file, giving us a package.
-There’s also a _src_ directory that contains _main.rs_. Open _Cargo.toml_ in
-your text editor, and note there’s no mention of _src/main.rs_. Cargo follows a
-convention that _src/main.rs_ is the crate root of a binary crate with the same
-name as the package. Likewise, Cargo knows that if the package directory
-contains _src/lib.rs_, the package contains a library crate with the same name
-as the package, and _src/lib.rs_ is its crate root. Cargo passes the crate root
-files to `rustc` to build the library or binary.
+بعد از اجرای `cargo new my-project`، از دستور `ls` استفاده میکنیم تا ببینیم Cargo چه چیزی ایجاد کرده است. در دایرکتوری پروژه، یک فایل _Cargo.toml_ وجود دارد که به ما یک بسته میدهد. همچنین یک دایرکتوری _src_ وجود دارد که شامل فایل _main.rs_ است. فایل _Cargo.toml_ را در ویرایشگر متن خود باز کنید و توجه کنید که هیچ اشارهای به _src/main.rs_ نشده است. Cargo از یک قرارداد پیروی میکند که _src/main.rs_ ریشه جعبه (crate) یک جعبه (crate) باینری با همان نام بسته است. به همین ترتیب، Cargo میداند که اگر دایرکتوری بسته شامل _src/lib.rs_ باشد، بسته شامل یک جعبه (crate) کتابخانهای با همان نام بسته است و _src/lib.rs_ ریشه جعبه (crate) آن است. Cargo فایلهای ریشه جعبه (crate) را به `rustc` ارسال میکند تا کتابخانه یا فایل اجرایی ساخته شود.
-Here, we have a package that only contains _src/main.rs_, meaning it only
-contains a binary crate named `my-project`. If a package contains _src/main.rs_
-and _src/lib.rs_, it has two crates: a binary and a library, both with the same
-name as the package. A package can have multiple binary crates by placing files
-in the _src/bin_ directory: each file will be a separate binary crate.
+در اینجا، ما یک بسته داریم که تنها شامل _src/main.rs_ است، به این معنی که تنها یک جعبه (crate) باینری به نام `my-project` دارد. اگر یک بسته شامل _src/main.rs_ و _src/lib.rs_ باشد، آن بسته دو جعبه (crate) خواهد داشت: یک جعبه (crate) باینری و یک کتابخانه، هر دو با همان نام بسته. یک بسته میتواند چندین جعبه (crate) باینری داشته باشد با قرار دادن فایلها در دایرکتوری _src/bin_: هر فایل یک جعبه (crate) باینری جداگانه خواهد بود.
[modules]: ch07-02-defining-modules-to-control-scope-and-privacy.html
[rand]: ch02-00-guessing-game-tutorial.html#generating-a-random-number
diff --git a/src/ch07-02-defining-modules-to-control-scope-and-privacy.md b/src/ch07-02-defining-modules-to-control-scope-and-privacy.md
index 37a41e3c2c..207f805a28 100644
--- a/src/ch07-02-defining-modules-to-control-scope-and-privacy.md
+++ b/src/ch07-02-defining-modules-to-control-scope-and-privacy.md
@@ -1,54 +1,26 @@
-## Defining Modules to Control Scope and Privacy
-
-In this section, we’ll talk about modules and other parts of the module system,
-namely _paths_, which allow you to name items; the `use` keyword that brings a
-path into scope; and the `pub` keyword to make items public. We’ll also discuss
-the `as` keyword, external packages, and the glob operator.
-
-### Modules Cheat Sheet
-
-Before we get to the details of modules and paths, here we provide a quick
-reference on how modules, paths, the `use` keyword, and the `pub` keyword work
-in the compiler, and how most developers organize their code. We’ll be going
-through examples of each of these rules throughout this chapter, but this is a
-great place to refer to as a reminder of how modules work.
-
-- **Start from the crate root**: When compiling a crate, the compiler first
- looks in the crate root file (usually _src/lib.rs_ for a library crate or
- _src/main.rs_ for a binary crate) for code to compile.
-- **Declaring modules**: In the crate root file, you can declare new modules;
- say you declare a “garden” module with `mod garden;`. The compiler will look
- for the module’s code in these places:
- - Inline, within curly brackets that replace the semicolon following `mod
- garden`
- - In the file _src/garden.rs_
- - In the file _src/garden/mod.rs_
-- **Declaring submodules**: In any file other than the crate root, you can
- declare submodules. For example, you might declare `mod vegetables;` in
- _src/garden.rs_. The compiler will look for the submodule’s code within the
- directory named for the parent module in these places:
- - Inline, directly following `mod vegetables`, within curly brackets instead
- of the semicolon
- - In the file _src/garden/vegetables.rs_
- - In the file _src/garden/vegetables/mod.rs_
-- **Paths to code in modules**: Once a module is part of your crate, you can
- refer to code in that module from anywhere else in that same crate, as long
- as the privacy rules allow, using the path to the code. For example, an
- `Asparagus` type in the garden vegetables module would be found at
+## تعریف ماژولها برای کنترل محدوده و حریم خصوصی
+
+در این بخش، ما درباره ماژولها و سایر بخشهای سیستم ماژول صحبت خواهیم کرد، یعنی _مسیرها_ که به شما امکان میدهند آیتمها را نامگذاری کنید؛ کلمه کلیدی `use` که مسیر را به محدوده وارد میکند؛ و کلمه کلیدی `pub` برای عمومی کردن آیتمها. همچنین درباره کلمه کلیدی `as`، بستههای خارجی، و عملگر `glob` صحبت خواهیم کرد.
+
+### خلاصهای از ماژولها
+
+قبل از اینکه به جزئیات ماژولها و مسیرها بپردازیم، اینجا یک مرجع سریع در مورد نحوه عملکرد ماژولها، مسیرها، کلمه کلیدی `use` و کلمه کلیدی `pub` در کامپایلر ارائه میدهیم و همچنین نحوه سازماندهی کد توسط اکثر توسعهدهندگان را نشان میدهیم. ما در طول این فصل به مثالهایی از هر یک از این قواعد خواهیم پرداخت، اما این یک مکان عالی برای یادآوری نحوه عملکرد ماژولها است.
+
+- **شروع از ریشه جعبه (crate):** هنگام کامپایل یک جعبه (crate)، کامپایلر ابتدا در فایل ریشه جعبه (crate) (معمولاً _src/lib.rs_ برای یک جعبه (crate) کتابخانهای یا _src/main.rs_ برای یک جعبه (crate) باینری) به دنبال کد برای کامپایل میگردد.
+- **تعریف ماژولها:** در فایل ریشه جعبه (crate)، میتوانید ماژولهای جدید تعریف کنید؛ مثلاً میتوانید یک ماژول "garden" با `mod garden;` تعریف کنید. کامپایلر کد ماژول را در مکانهای زیر جستجو میکند:
+ - به صورت درونخطی، داخل براکتهای موجدار که به جای علامت نقطهویرگول بعد از `mod garden` قرار میگیرند.
+ - در فایل _src/garden.rs_
+ - در فایل _src/garden/mod.rs_
+- **تعریف زیرماژولها:** در هر فایلی به جز فایل ریشه جعبه (crate)، میتوانید زیرماژولها تعریف کنید. برای مثال، ممکن است `mod vegetables;` را در فایل _src/garden.rs_ تعریف کنید. کامپایلر کد زیرماژول را در دایرکتوریای که به نام ماژول والد است، در مکانهای زیر جستجو میکند:
+ - به صورت درونخطی، مستقیماً بعد از `mod vegetables`، داخل براکتهای موجدار به جای نقطهویرگول
+ - در فایل _src/garden/vegetables.rs_
+ - در فایل _src/garden/vegetables/mod.rs_
+- **مسیرها به کد در ماژولها:** وقتی یک ماژول بخشی از جعبه (crate) شما باشد، میتوانید از هر جای دیگر در همان جعبه (crate) (تا زمانی که قواعد حریم خصوصی اجازه دهند) با استفاده از مسیر به کد آن ارجاع دهید. برای مثال، یک نوع `Asparagus` در ماژول vegetables در garden به این صورت پیدا میشود:
`crate::garden::vegetables::Asparagus`.
-- **Private vs. public**: Code within a module is private from its parent
- modules by default. To make a module public, declare it with `pub mod`
- instead of `mod`. To make items within a public module public as well, use
- `pub` before their declarations.
-- **The `use` keyword**: Within a scope, the `use` keyword creates shortcuts to
- items to reduce repetition of long paths. In any scope that can refer to
- `crate::garden::vegetables::Asparagus`, you can create a shortcut with `use
- crate::garden::vegetables::Asparagus;` and from then on you only need to
- write `Asparagus` to make use of that type in the scope.
-
-Here, we create a binary crate named `backyard` that illustrates these rules.
-The crate’s directory, also named `backyard`, contains these files and
-directories:
+- **خصوصی در مقابل عمومی:** کد درون یک ماژول به صورت پیشفرض برای ماژولهای والد خصوصی است. برای عمومی کردن یک ماژول، آن را با `pub mod` به جای `mod` تعریف کنید. برای عمومی کردن آیتمهای داخل یک ماژول عمومی، از `pub` قبل از اعلان آنها استفاده کنید.
+- **کلمه کلیدی `use`:** در یک محدوده، کلمه کلیدی `use` میانبری به آیتمها ایجاد میکند تا تکرار مسیرهای طولانی کاهش یابد. در هر محدودهای که میتواند به `crate::garden::vegetables::Asparagus` ارجاع دهد، میتوانید یک میانبر با `use crate::garden::vegetables::Asparagus;` ایجاد کنید و از آن به بعد فقط کافی است `Asparagus` را در آن محدوده استفاده کنید.
+
+اینجا، ما یک جعبه (crate) باینری به نام `backyard` ایجاد میکنیم که این قواعد را نشان میدهد. دایرکتوری جعبه (crate) که آن هم `backyard` نامیده میشود شامل این فایلها و دایرکتوریها است:
```text
backyard
@@ -61,7 +33,7 @@ backyard
└── main.rs
```
-The crate root file in this case is _src/main.rs_, and it contains:
+فایل ریشه جعبه (crate) در اینجا _src/main.rs_ است و حاوی موارد زیر است:
@@ -71,8 +43,7 @@ The crate root file in this case is _src/main.rs_, and it contains:
-The `pub mod garden;` line tells the compiler to include the code it finds in
-_src/garden.rs_, which is:
+خط `pub mod garden;` به کامپایلر میگوید که کدی را که در _src/garden.rs_ پیدا میکند وارد کند، که شامل موارد زیر است:
@@ -82,43 +53,25 @@ _src/garden.rs_, which is:
-Here, `pub mod vegetables;` means the code in _src/garden/vegetables.rs_ is
-included too. That code is:
+اینجا، `pub mod vegetables;` به این معنا است که کد موجود در _src/garden/vegetables.rs_ نیز وارد میشود. آن کد به صورت زیر است:
```rust,noplayground,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/quick-reference-example/src/garden/vegetables.rs}}
```
-Now let’s get into the details of these rules and demonstrate them in action!
+حالا بیایید به جزئیات این قواعد بپردازیم و آنها را در عمل نشان دهیم!
-### Grouping Related Code in Modules
+### گروهبندی کدهای مرتبط در ماژولها
-_Modules_ let us organize code within a crate for readability and easy reuse.
-Modules also allow us to control the _privacy_ of items because code within a
-module is private by default. Private items are internal implementation details
-not available for outside use. We can choose to make modules and the items
-within them public, which exposes them to allow external code to use and depend
-on them.
+_ماژولها_ به ما امکان میدهند کد را در یک جعبه (crate) برای خوانایی و بازاستفاده آسان سازماندهی کنیم. ماژولها همچنین به ما امکان کنترل _حریم خصوصی_ آیتمها را میدهند زیرا کد درون یک ماژول به صورت پیشفرض خصوصی است. آیتمهای خصوصی جزئیات پیادهسازی داخلی هستند که برای استفاده خارجی در دسترس نیستند. ما میتوانیم انتخاب کنیم که ماژولها و آیتمهای درون آنها عمومی باشند، که این موارد را برای استفاده خارجی آشکار میکند.
-As an example, let’s write a library crate that provides the functionality of a
-restaurant. We’ll define the signatures of functions but leave their bodies
-empty to concentrate on the organization of the code rather than the
-implementation of a restaurant.
+برای مثال، بیایید یک جعبه (crate) کتابخانهای بنویسیم که عملکرد یک رستوران را ارائه دهد. امضای توابع را تعریف میکنیم اما بدنه آنها را خالی میگذاریم تا بیشتر بر سازماندهی کد تمرکز کنیم تا پیادهسازی عملکرد یک رستوران.
-In the restaurant industry, some parts of a restaurant are referred to as
-_front of house_ and others as _back of house_. Front of house is where
-customers are; this encompasses where the hosts seat customers, servers take
-orders and payment, and bartenders make drinks. Back of house is where the
-chefs and cooks work in the kitchen, dishwashers clean up, and managers do
-administrative work.
+در صنعت رستوران، برخی قسمتهای یک رستوران به عنوان _جلوی خانه_ و دیگر قسمتها به عنوان _پشت خانه_ شناخته میشوند. جلوی خانه جایی است که مشتریان هستند؛ این شامل جایی است که میزبانها مشتریان را مینشانند، گارسونها سفارش میگیرند و پرداختها را انجام میدهند، و بارتندرها نوشیدنی درست میکنند. پشت خانه جایی است که سرآشپزها و آشپزها در آشپزخانه کار میکنند، ظرفشورها ظروف را تمیز میکنند، و مدیران کارهای اداری انجام میدهند.
-To structure our crate in this way, we can organize its functions into nested
-modules. Create a new library named `restaurant` by running `cargo new
-restaurant --lib`. Then enter the code in Listing 7-1 into _src/lib.rs_ to
-define some modules and function signatures; this code is the front of house
-section.
+برای ساختاردهی جعبه (crate) خود به این روش، میتوانیم عملکردها را در ماژولهای تو در تو سازماندهی کنیم. یک کتابخانه جدید به نام `restaurant` با اجرای دستور `cargo new restaurant --lib` ایجاد کنید. سپس کد لیستینگ 7-1 را در _src/lib.rs_ وارد کنید تا برخی ماژولها و امضای توابع تعریف شود. این کد بخش جلوی خانه را تعریف میکند.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs}}
@@ -126,27 +79,23 @@ section.
-We define a module with the `mod` keyword followed by the name of the module
-(in this case, `front_of_house`). The body of the module then goes inside curly
-brackets. Inside modules, we can place other modules, as in this case with the
-modules `hosting` and `serving`. Modules can also hold definitions for other
-items, such as structs, enums, constants, traits, and as in Listing 7-1,
-functions.
+ماژولی را با استفاده از کلیدواژهی `mod` و بهدنبال آن نام ماژول تعریف میکنیم
+(در این مثال، `front_of_house`). بدنهی ماژول درون آکولاد قرار میگیرد.
+درون ماژولها میتوان ماژولهای دیگری نیز قرار داد، همانطور که در این مثال،
+ماژولهای `hosting` و `serving` درون `front_of_house` قرار گرفتهاند.
+ماژولها همچنین میتوانند شامل تعریف آیتمهای دیگر نیز باشند،
+مانند `struct`ها، `enum`ها، ثابتها (constants)، `trait`ها،
+و همانطور که در لیستینگ 7-1 میبینید، توابع.
+
+با استفاده از ماژولها، میتوانیم تعاریف مرتبط را با هم گروهبندی کنیم و دلیل ارتباط آنها را نامگذاری کنیم. برنامهنویسانی که از این کد استفاده میکنند میتوانند بر اساس گروهها کد را مرور کنند، به جای اینکه مجبور باشند تمام تعاریف را بخوانند. این کار پیدا کردن تعاریف مرتبط با آنها را آسانتر میکند. برنامهنویسانی که عملکرد جدیدی به این کد اضافه میکنند میدانند که کد را کجا قرار دهند تا برنامه سازماندهی شده باقی بماند.
-By using modules, we can group related definitions together and name why
-they’re related. Programmers using this code can navigate the code based on the
-groups rather than having to read through all the definitions, making it easier
-to find the definitions relevant to them. Programmers adding new functionality
-to this code would know where to place the code to keep the program organized.
+### درخت ماژول
-Earlier, we mentioned that _src/main.rs_ and _src/lib.rs_ are called crate
-roots. The reason for their name is that the contents of either of these two
-files form a module named `crate` at the root of the crate’s module structure,
-known as the _module tree_.
+قبلاً اشاره کردیم که _src/main.rs_ و _src/lib.rs_ به نام ریشه جعبه (crate) شناخته میشوند. دلیل نامگذاری آنها این است که محتوای هر یک از این دو فایل یک ماژول به نام `crate` را در ریشه ساختار ماژول جعبه (crate) تشکیل میدهند، که به عنوان _درخت ماژول_ شناخته میشود.
-Listing 7-2 shows the module tree for the structure in Listing 7-1.
+لیستینگ 7-2 درخت ماژول را برای ساختار موجود در لیستینگ 7-1 نشان میدهد.
-
+
```text
crate
@@ -162,15 +111,6 @@ crate
-This tree shows how some of the modules nest inside other modules; for example,
-`hosting` nests inside `front_of_house`. The tree also shows that some modules
-are _siblings_, meaning they’re defined in the same module; `hosting` and
-`serving` are siblings defined within `front_of_house`. If module A is
-contained inside module B, we say that module A is the _child_ of module B and
-that module B is the _parent_ of module A. Notice that the entire module tree
-is rooted under the implicit module named `crate`.
-
-The module tree might remind you of the filesystem’s directory tree on your
-computer; this is a very apt comparison! Just like directories in a filesystem,
-you use modules to organize your code. And just like files in a directory, we
-need a way to find our modules.
+این درخت نشان میدهد که برخی از ماژولها در داخل ماژولهای دیگر قرار دارند؛ برای مثال، `hosting` در داخل `front_of_house` قرار دارد. درخت همچنین نشان میدهد که برخی از ماژولها _همسطح_ هستند، به این معنی که در همان ماژول تعریف شدهاند؛ `hosting` و `serving` همسطح هستند و درون `front_of_house` تعریف شدهاند. اگر ماژول A درون ماژول B قرار گیرد، میگوییم ماژول A _فرزند_ ماژول B است و ماژول B _والد_ ماژول A است. توجه کنید که کل درخت ماژول در زیر ماژول ضمنی به نام `crate` ریشه دارد.
+
+درخت ماژول ممکن است شما را به یاد درخت دایرکتوریهای فایلسیستم کامپیوتر بیندازد؛ این مقایسه بسیار مناسبی است! درست همانطور که دایرکتوریها در فایلسیستم کد را سازماندهی میکنند، شما میتوانید از ماژولها برای سازماندهی کد خود استفاده کنید. و درست مانند فایلها در یک دایرکتوری، ما نیاز به روشی برای پیدا کردن ماژولها داریم.
diff --git a/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md b/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md
index 63e49420e9..ca5c932352 100644
--- a/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md
+++ b/src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md
@@ -1,35 +1,21 @@
-## Paths for Referring to an Item in the Module Tree
+## مسیرها برای اشاره به یک آیتم در درخت ماژول
-To show Rust where to find an item in a module tree, we use a path in the same
-way we use a path when navigating a filesystem. To call a function, we need to
-know its path.
+برای نشان دادن به Rust که یک آیتم را در درخت ماژول کجا پیدا کند، از یک مسیر استفاده میکنیم، مشابه استفاده از مسیر هنگام پیمایش در یک فایلسیستم. برای فراخوانی یک تابع، باید مسیر آن را بدانیم.
-A path can take two forms:
+یک مسیر میتواند به دو شکل باشد:
-- An _absolute path_ is the full path starting from a crate root; for code
- from an external crate, the absolute path begins with the crate name, and for
- code from the current crate, it starts with the literal `crate`.
-- A _relative path_ starts from the current module and uses `self`, `super`, or
- an identifier in the current module.
+- یک _مسیر مطلق_ مسیری کامل است که از ریشه جعبه (crate) شروع میشود؛ برای کدی که از یک جعبه (crate) خارجی میآید، مسیر مطلق با نام جعبه (crate) شروع میشود، و برای کدی که از جعبه (crate) فعلی میآید، با کلمه کلیدی `crate` شروع میشود.
+- یک _مسیر نسبی_ از ماژول فعلی شروع میشود و از `self`، `super` یا یک شناسه در ماژول فعلی استفاده میکند.
-Both absolute and relative paths are followed by one or more identifiers
-separated by double colons (`::`).
+هر دو مسیر مطلق و نسبی با یک یا چند شناسه که با دو نقطه دوبل (`::`) جدا شدهاند دنبال میشوند.
-Returning to Listing 7-1, say we want to call the `add_to_waitlist` function.
-This is the same as asking: what’s the path of the `add_to_waitlist` function?
-Listing 7-3 contains Listing 7-1 with some of the modules and functions
-removed.
+با بازگشت به لیستینگ 7-1، فرض کنید که میخواهیم تابع `add_to_waitlist` را فراخوانی کنیم. این کار مشابه پرسیدن این است: مسیر تابع `add_to_waitlist` چیست؟ لیستینگ 7-3 شامل لیستینگ 7-1 با حذف برخی از ماژولها و توابع است.
-We’ll show two ways to call the `add_to_waitlist` function from a new function,
-`eat_at_restaurant`, defined in the crate root. These paths are correct, but
-there’s another problem remaining that will prevent this example from compiling
-as is. We’ll explain why in a bit.
+ما دو روش برای فراخوانی تابع `add_to_waitlist` از یک تابع جدید، `eat_at_restaurant`، که در ریشه جعبه (crate) تعریف شده است، نشان خواهیم داد. این مسیرها درست هستند، اما یک مشکل دیگر وجود دارد که مانع کامپایل این مثال به شکل فعلی میشود. بعداً توضیح خواهیم داد که چرا.
-The `eat_at_restaurant` function is part of our library crate’s public API, so
-we mark it with the `pub` keyword. In the [“Exposing Paths with the `pub`
-Keyword”][pub] section, we’ll go into more detail about `pub`.
+تابع `eat_at_restaurant` بخشی از API عمومی جعبه (crate) کتابخانهای ما است، بنابراین آن را با کلمه کلیدی `pub` علامت میزنیم. در بخش [«آشکار کردن مسیرها با کلمه کلیدی `pub`»][pub]، به جزئیات بیشتری درباره `pub` خواهیم پرداخت.
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs}}
@@ -37,39 +23,17 @@ Keyword”][pub] section, we’ll go into more detail about `pub`
-The first time we call the `add_to_waitlist` function in `eat_at_restaurant`,
-we use an absolute path. The `add_to_waitlist` function is defined in the same
-crate as `eat_at_restaurant`, which means we can use the `crate` keyword to
-start an absolute path. We then include each of the successive modules until we
-make our way to `add_to_waitlist`. You can imagine a filesystem with the same
-structure: we’d specify the path `/front_of_house/hosting/add_to_waitlist` to
-run the `add_to_waitlist` program; using the `crate` name to start from the
-crate root is like using `/` to start from the filesystem root in your shell.
-
-The second time we call `add_to_waitlist` in `eat_at_restaurant`, we use a
-relative path. The path starts with `front_of_house`, the name of the module
-defined at the same level of the module tree as `eat_at_restaurant`. Here the
-filesystem equivalent would be using the path
-`front_of_house/hosting/add_to_waitlist`. Starting with a module name means
-that the path is relative.
-
-Choosing whether to use a relative or absolute path is a decision you’ll make
-based on your project, and it depends on whether you’re more likely to move
-item definition code separately from or together with the code that uses the
-item. For example, if we moved the `front_of_house` module and the
-`eat_at_restaurant` function into a module named `customer_experience`, we’d
-need to update the absolute path to `add_to_waitlist`, but the relative path
-would still be valid. However, if we moved the `eat_at_restaurant` function
-separately into a module named `dining`, the absolute path to the
-`add_to_waitlist` call would stay the same, but the relative path would need to
-be updated. Our preference in general is to specify absolute paths because it’s
-more likely we’ll want to move code definitions and item calls independently of
-each other.
-
-Let’s try to compile Listing 7-3 and find out why it won’t compile yet! The
-errors we get are shown in Listing 7-4.
-
-
+بار اولی که تابع `add_to_waitlist` را در `eat_at_restaurant` فراخوانی میکنیم، از یک مسیر مطلق استفاده میکنیم. تابع `add_to_waitlist` در همان جعبه (crate) تعریف شده است که `eat_at_restaurant` در آن قرار دارد، که به این معنی است که میتوانیم از کلمه کلیدی `crate` برای شروع مسیر مطلق استفاده کنیم. سپس هر یک از ماژولهای متوالی را شامل میکنیم تا به `add_to_waitlist` برسیم. میتوانید یک فایلسیستم با ساختار مشابه تصور کنید: ما مسیر `/front_of_house/hosting/add_to_waitlist` را برای اجرای برنامه `add_to_waitlist` مشخص میکنیم؛ استفاده از نام `crate` برای شروع از ریشه جعبه (crate) مانند استفاده از `/` برای شروع از ریشه فایلسیستم در شل است.
+
+بار دوم که تابع `add_to_waitlist` را در `eat_at_restaurant` فراخوانی میکنیم، از یک مسیر نسبی استفاده میکنیم. مسیر با `front_of_house` شروع میشود، که نام ماژولی است که در همان سطح از درخت ماژول به عنوان `eat_at_restaurant` تعریف شده است. اینجا معادل فایلسیستم استفاده از مسیر `front_of_house/hosting/add_to_waitlist` است. شروع با نام ماژول به این معنی است که مسیر نسبی است.
+
+### انتخاب بین مسیرهای مطلق و نسبی
+
+انتخاب بین استفاده از مسیر نسبی یا مطلق یک تصمیم است که بر اساس پروژه شما گرفته میشود، و به این بستگی دارد که آیا احتمال بیشتری دارد کد تعریف آیتم را به طور مستقل از یا همراه با کدی که از آیتم استفاده میکند جابجا کنید. برای مثال، اگر ماژول `front_of_house` و تابع `eat_at_restaurant` را به یک ماژول به نام `customer_experience` منتقل کنیم، باید مسیر مطلق به `add_to_waitlist` را بهروزرسانی کنیم، اما مسیر نسبی همچنان معتبر خواهد بود. با این حال، اگر تابع `eat_at_restaurant` را به طور مستقل به یک ماژول به نام `dining` منتقل کنیم، مسیر مطلق به فراخوانی `add_to_waitlist` تغییر نمیکند، اما مسیر نسبی باید بهروزرسانی شود. ترجیح ما به طور کلی این است که مسیرهای مطلق را مشخص کنیم زیرا احتمال بیشتری دارد که بخواهیم تعریف کد و فراخوانی آیتمها را مستقل از یکدیگر جابجا کنیم.
+
+بیایید سعی کنیم کد لیستینگ 7-3 را کامپایل کنیم و ببینیم چرا هنوز کامپایل نمیشود! خطاهایی که دریافت میکنیم در لیستینگ 7-4 نشان داده شدهاند.
+
+
```console
{{#include ../listings/ch07-managing-growing-projects/listing-07-03/output.txt}}
@@ -77,35 +41,17 @@ errors we get are shown in Listing 7-4.
-The error messages say that module `hosting` is private. In other words, we
-have the correct paths for the `hosting` module and the `add_to_waitlist`
-function, but Rust won’t let us use them because it doesn’t have access to the
-private sections. In Rust, all items (functions, methods, structs, enums,
-modules, and constants) are private to parent modules by default. If you want
-to make an item like a function or struct private, you put it in a module.
+پیامهای خطا میگویند که ماژول `hosting` خصوصی است. به عبارت دیگر، ما مسیرهای صحیح برای ماژول `hosting` و تابع `add_to_waitlist` داریم، اما Rust به ما اجازه نمیدهد از آنها استفاده کنیم زیرا به بخشهای خصوصی دسترسی ندارد. در Rust، تمام آیتمها (توابع، متدها، ساختارها، enumها، ماژولها و ثابتها) به صورت پیشفرض برای ماژولهای والد خصوصی هستند. اگر بخواهید آیتمی مانند یک تابع یا ساختار را خصوصی کنید، آن را در یک ماژول قرار میدهید.
-Items in a parent module can’t use the private items inside child modules, but
-items in child modules can use the items in their ancestor modules. This is
-because child modules wrap and hide their implementation details, but the child
-modules can see the context in which they’re defined. To continue with our
-metaphor, think of the privacy rules as being like the back office of a
-restaurant: what goes on in there is private to restaurant customers, but
-office managers can see and do everything in the restaurant they operate.
+آیتمهای موجود در یک ماژول والد نمیتوانند از آیتمهای خصوصی درون ماژولهای فرزند استفاده کنند، اما آیتمهای درون ماژولهای فرزند میتوانند از آیتمهای ماژولهای اجداد خود استفاده کنند. این به این دلیل است که ماژولهای فرزند جزئیات پیادهسازی خود را بستهبندی و پنهان میکنند، اما ماژولهای فرزند میتوانند زمینهای که در آن تعریف شدهاند را ببینند. برای ادامه مثال، قواعد حریم خصوصی را مانند دفتر پشتی یک رستوران تصور کنید: آنچه در آنجا میگذرد برای مشتریان رستوران خصوصی است، اما مدیران دفتر میتوانند همه چیز را در رستوران ببینند و انجام دهند.
-Rust chose to have the module system function this way so that hiding inner
-implementation details is the default. That way, you know which parts of the
-inner code you can change without breaking outer code. However, Rust does give
-you the option to expose inner parts of child modules’ code to outer ancestor
-modules by using the `pub` keyword to make an item public.
+Rust تصمیم گرفته است که سیستم ماژول به این صورت کار کند تا پنهان کردن جزئیات پیادهسازی داخلی به صورت پیشفرض باشد. به این ترتیب، میدانید کدام بخشهای کد داخلی را میتوانید تغییر دهید بدون اینکه کد بیرونی را خراب کنید. با این حال، Rust به شما این امکان را میدهد که بخشهای داخلی کد ماژولهای فرزند را به ماژولهای اجداد بیرونی با استفاده از کلمه کلیدی `pub` عمومی کنید.
-### Exposing Paths with the `pub` Keyword
+### آشکار کردن مسیرها با کلمه کلیدی `pub`
-Let’s return to the error in Listing 7-4 that told us the `hosting` module is
-private. We want the `eat_at_restaurant` function in the parent module to have
-access to the `add_to_waitlist` function in the child module, so we mark the
-`hosting` module with the `pub` keyword, as shown in Listing 7-5.
+بیایید به خطای لیستینگ 7-4 برگردیم که به ما گفت ماژول `hosting` خصوصی است. ما میخواهیم تابع `eat_at_restaurant` در ماژول والد به تابع `add_to_waitlist` در ماژول فرزند دسترسی داشته باشد، بنابراین ماژول `hosting` را با کلمه کلیدی `pub` علامت میزنیم، همانطور که در لیستینگ 7-5 نشان داده شده است.
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs:here}}
@@ -113,10 +59,9 @@ access to the `add_to_waitlist` function in the child module, so we mark the
-Unfortunately, the code in Listing 7-5 still results in compiler errors, as
-shown in Listing 7-6.
+متأسفانه، کد در لیستینگ 7-5 همچنان به خطاهای کامپایلر منجر میشود، همانطور که در لیستینگ 7-6 نشان داده شده است.
-
+
```console
{{#include ../listings/ch07-managing-growing-projects/listing-07-05/output.txt}}
@@ -124,23 +69,13 @@ shown in Listing 7-6.
-What happened? Adding the `pub` keyword in front of `mod hosting` makes the
-module public. With this change, if we can access `front_of_house`, we can
-access `hosting`. But the _contents_ of `hosting` are still private; making the
-module public doesn’t make its contents public. The `pub` keyword on a module
-only lets code in its ancestor modules refer to it, not access its inner code.
-Because modules are containers, there’s not much we can do by only making the
-module public; we need to go further and choose to make one or more of the
-items within the module public as well.
+چه اتفاقی افتاد؟ اضافه کردن کلمه کلیدی `pub` در جلوی `mod hosting` ماژول را عمومی میکند. با این تغییر، اگر به `front_of_house` دسترسی داشته باشیم، میتوانیم به `hosting` نیز دسترسی داشته باشیم. اما _محتویات_ `hosting` همچنان خصوصی است؛ عمومی کردن ماژول به معنای عمومی کردن محتوای آن نیست. کلمه کلیدی `pub` روی یک ماژول فقط به کدهای موجود در ماژولهای اجداد اجازه میدهد به آن ارجاع دهند، نه اینکه به کد داخلی آن دسترسی داشته باشند. از آنجایی که ماژولها به عنوان ظرف عمل میکنند، تنها عمومی کردن ماژول کافی نیست؛ باید فراتر رفته و یک یا چند مورد از آیتمهای درون ماژول را نیز عمومی کنیم.
-The errors in Listing 7-6 say that the `add_to_waitlist` function is private.
-The privacy rules apply to structs, enums, functions, and methods as well as
-modules.
+خطاهای موجود در لیستینگ 7-6 نشان میدهند که تابع `add_to_waitlist` خصوصی است. قواعد حریم خصوصی برای ساختارها، enumها، توابع، متدها و همچنین ماژولها اعمال میشوند.
-Let’s also make the `add_to_waitlist` function public by adding the `pub`
-keyword before its definition, as in Listing 7-7.
+بیایید تابع `add_to_waitlist` را نیز با اضافه کردن کلمه کلیدی `pub` قبل از تعریف آن عمومی کنیم، همانطور که در لیستینگ 7-7 نشان داده شده است.
-
+
```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs:here}}
@@ -174,39 +109,34 @@ If you plan on sharing your library crate so other projects can use your code,
your public API is your contract with users of your crate that determines how
they can interact with your code. There are many considerations around managing
changes to your public API to make it easier for people to depend on your
-crate. These considerations are beyond the scope of this book; if you’re
+crate. These considerations are out of the scope of this book; if you’re
interested in this topic, see [The Rust API Guidelines][api-guidelines].
-> #### Best Practices for Packages with a Binary and a Library
+> #### بهترین شیوهها برای بستههایی که یک جعبه (crate) باینری و یک جعبه (crate) کتابخانهای دارند
>
> We mentioned that a package can contain both a _src/main.rs_ binary crate
> root as well as a _src/lib.rs_ library crate root, and both crates will have
> the package name by default. Typically, packages with this pattern of
> containing both a library and a binary crate will have just enough code in the
-> binary crate to start an executable that calls code defined in the library
-> crate. This lets other projects benefit from the most functionality that the
+> binary crate to start an executable that calls code within the library crate.
+> This lets other projects benefit from most of the functionality that the
> package provides because the library crate’s code can be shared.
>
-> The module tree should be defined in _src/lib.rs_. Then, any public items can
-> be used in the binary crate by starting paths with the name of the package.
-> The binary crate becomes a user of the library crate just like a completely
-> external crate would use the library crate: it can only use the public API.
-> This helps you design a good API; not only are you the author, you’re also a
-> client!
+> درخت ماژول باید در _src/lib.rs_ تعریف شود. سپس، هر آیتم عمومی را میتوان در جعبه (crate) باینری با شروع مسیرها با نام بسته استفاده کرد. جعبه (crate) باینری به یک کاربر از جعبه (crate) کتابخانهای تبدیل میشود، درست مثل اینکه یک جعبه (crate) کاملاً خارجی از جعبه (crate) کتابخانهای استفاده میکند: تنها میتواند از API عمومی استفاده کند. این کار به شما کمک میکند یک API خوب طراحی کنید؛ نه تنها نویسنده آن هستید، بلکه یک کاربر نیز هستید!
>
> In [Chapter 12][ch12], we’ll demonstrate this organizational
-> practice with a command line program that will contain both a binary crate
+> practice with a command-line program that will contain both a binary crate
> and a library crate.
### Starting Relative Paths with `super`
We can construct relative paths that begin in the parent module, rather than
the current module or the crate root, by using `super` at the start of the
-path. This is like starting a filesystem path with the `..` syntax that means
-to go to the parent directory. Using `super` allows us to reference an item
-that we know is in the parent module, which can make rearranging the module
-tree easier when the module is closely related to the parent but the parent
-might be moved elsewhere in the module tree someday.
+path. This is like starting a filesystem path with the `..` syntax. Using
+`super` allows us to reference an item that we know is in the parent module,
+which can make rearranging the module tree easier when the module is closely
+related to the parent but the parent might be moved elsewhere in the module
+tree someday.
Consider the code in Listing 7-8 that models the situation in which a chef
fixes an incorrect order and personally brings it out to the customer. The
@@ -214,7 +144,7 @@ function `fix_incorrect_order` defined in the `back_of_house` module calls the
function `deliver_order` defined in the parent module by specifying the path to
`deliver_order`, starting with `super`.
-
+
```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs}}
@@ -222,29 +152,13 @@ function `deliver_order` defined in the parent module by specifying the path to
-The `fix_incorrect_order` function is in the `back_of_house` module, so we can
-use `super` to go to the parent module of `back_of_house`, which in this case
-is `crate`, the root. From there, we look for `deliver_order` and find it.
-Success! We think the `back_of_house` module and the `deliver_order` function
-are likely to stay in the same relationship to each other and get moved
-together should we decide to reorganize the crate’s module tree. Therefore, we
-used `super` so we’ll have fewer places to update code in the future if this
-code gets moved to a different module.
-
-### Making Structs and Enums Public
-
-We can also use `pub` to designate structs and enums as public, but there are a
-few extra details to the usage of `pub` with structs and enums. If we use `pub`
-before a struct definition, we make the struct public, but the struct’s fields
-will still be private. We can make each field public or not on a case-by-case
-basis. In Listing 7-9, we’ve defined a public `back_of_house::Breakfast` struct
-with a public `toast` field but a private `seasonal_fruit` field. This models
-the case in a restaurant where the customer can pick the type of bread that
-comes with a meal, but the chef decides which fruit accompanies the meal based
-on what’s in season and in stock. The available fruit changes quickly, so
-customers can’t choose the fruit or even see which fruit they’ll get.
-
-
+تابع `fix_incorrect_order` در ماژول `back_of_house` است، بنابراین میتوانیم از `super` برای رفتن به ماژول والد `back_of_house` استفاده کنیم، که در این مورد `crate`، یعنی ریشه است. از آنجا به دنبال `deliver_order` میگردیم و آن را پیدا میکنیم. موفقیت! ما فکر میکنیم که ماژول `back_of_house` و تابع `deliver_order` احتمالاً در همان رابطه با یکدیگر باقی میمانند و اگر بخواهیم درخت ماژول جعبه (crate) را سازماندهی مجدد کنیم، با هم جابجا میشوند. بنابراین، از `super` استفاده کردیم تا در آینده، اگر این کد به ماژول دیگری منتقل شد، تغییرات کمتری در کد لازم باشد.
+
+### عمومی کردن ساختارها و enumها
+
+ما همچنین میتوانیم از `pub` برای مشخص کردن ساختارها و enumها به عنوان عمومی استفاده کنیم، اما چند جزئیات اضافی در مورد استفاده از `pub` با ساختارها و enumها وجود دارد. اگر از `pub` قبل از تعریف یک ساختار استفاده کنیم، ساختار عمومی میشود، اما فیلدهای ساختار همچنان خصوصی خواهند بود. ما میتوانیم هر فیلد را به صورت موردی عمومی یا خصوصی کنیم. در لیستینگ 7-9، یک ساختار عمومی به نام `back_of_house::Breakfast` تعریف کردهایم که یک فیلد عمومی به نام `toast` دارد اما فیلد `seasonal_fruit` خصوصی است. این مدلسازی حالتی است که در آن مشتری میتواند نوع نان همراه با وعده غذایی را انتخاب کند، اما سرآشپز تصمیم میگیرد که کدام میوه همراه وعده غذایی باشد بر اساس آنچه در فصل و موجودی است. میوههای موجود به سرعت تغییر میکنند، بنابراین مشتریان نمیتوانند میوه را انتخاب کنند یا حتی ببینند که چه میوهای دریافت خواهند کرد.
+
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs}}
@@ -252,23 +166,14 @@ customers can’t choose the fruit or even see which fruit they’ll get.
-Because the `toast` field in the `back_of_house::Breakfast` struct is public,
-in `eat_at_restaurant` we can write and read to the `toast` field using dot
-notation. Notice that we can’t use the `seasonal_fruit` field in
-`eat_at_restaurant`, because `seasonal_fruit` is private. Try uncommenting the
-line modifying the `seasonal_fruit` field value to see what error you get!
+از آنجا که فیلد `toast` در ساختار `back_of_house::Breakfast` عمومی است، میتوانیم در `eat_at_restaurant` به این فیلد با استفاده از نقطهگذاری مقدار بدهیم یا مقدار آن را بخوانیم. توجه کنید که نمیتوانیم از فیلد `seasonal_fruit` در `eat_at_restaurant` استفاده کنیم، زیرا `seasonal_fruit` خصوصی است. خطی که مقدار فیلد `seasonal_fruit` را تغییر میدهد را لغو کامنت کنید تا ببینید چه خطایی دریافت میکنید!
+
+همچنین توجه کنید که چون `back_of_house::Breakfast` یک فیلد خصوصی دارد، ساختار باید یک تابع وابسته عمومی ارائه دهد که یک نمونه از `Breakfast` بسازد (ما آن را اینجا `summer` نامیدهایم). اگر `Breakfast` چنین تابعی نداشت، نمیتوانستیم یک نمونه از `Breakfast` را در `eat_at_restaurant` ایجاد کنیم، زیرا نمیتوانستیم مقدار فیلد خصوصی `seasonal_fruit` را در `eat_at_restaurant` تنظیم کنیم.
-Also, note that because `back_of_house::Breakfast` has a private field, the
-struct needs to provide a public associated function that constructs an
-instance of `Breakfast` (we’ve named it `summer` here). If `Breakfast` didn’t
-have such a function, we couldn’t create an instance of `Breakfast` in
-`eat_at_restaurant` because we couldn’t set the value of the private
-`seasonal_fruit` field in `eat_at_restaurant`.
+در مقابل، اگر یک enum را عمومی کنیم، تمام متغیرهای آن نیز عمومی میشوند. ما فقط به `pub` قبل از کلمه کلیدی `enum` نیاز داریم، همانطور که در لیستینگ 7-10 نشان داده شده است.
-In contrast, if we make an enum public, all of its variants are then public. We
-only need the `pub` before the `enum` keyword, as shown in Listing 7-10.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs}}
@@ -276,18 +181,11 @@ only need the `pub` before the `enum` keyword, as shown in Listing 7-10.
-Because we made the `Appetizer` enum public, we can use the `Soup` and `Salad`
-variants in `eat_at_restaurant`.
+از آنجایی که enum `Appetizer` را عمومی کردیم، میتوانیم از متغیرهای `Soup` و `Salad` در `eat_at_restaurant` استفاده کنیم.
-Enums aren’t very useful unless their variants are public; it would be annoying
-to have to annotate all enum variants with `pub` in every case, so the default
-for enum variants is to be public. Structs are often useful without their
-fields being public, so struct fields follow the general rule of everything
-being private by default unless annotated with `pub`.
+Enums خیلی مفید نیستند مگر اینکه متغیرهای آنها عمومی باشند؛ اضافه کردن `pub` به تمام متغیرهای enum در هر مورد کار خستهکنندهای خواهد بود، بنابراین به طور پیشفرض متغیرهای enum عمومی هستند. ساختارها اغلب بدون عمومی بودن فیلدهایشان مفید هستند، بنابراین فیلدهای ساختار از قانون کلی پیروی میکنند که همه چیز به صورت پیشفرض خصوصی است مگر اینکه با `pub` مشخص شود.
-There’s one more situation involving `pub` that we haven’t covered, and that is
-our last module system feature: the `use` keyword. We’ll cover `use` by itself
-first, and then we’ll show how to combine `pub` and `use`.
+یک وضعیت دیگر مرتبط با `pub` وجود دارد که هنوز آن را پوشش ندادهایم، و آن آخرین ویژگی سیستم ماژول ما است: کلمه کلیدی `use`. ابتدا `use` را به تنهایی بررسی خواهیم کرد، و سپس نشان خواهیم داد چگونه `pub` و `use` را ترکیب کنیم.
[pub]: ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html#exposing-paths-with-the-pub-keyword
[api-guidelines]: https://rust-lang.github.io/api-guidelines/
diff --git a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
index 52a85415b3..3c12f8587a 100644
--- a/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
+++ b/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
@@ -1,18 +1,10 @@
-## Bringing Paths into Scope with the `use` Keyword
+## وارد کردن مسیرها به محدوده با کلمه کلیدی `use`
-Having to write out the paths to call functions can feel inconvenient and
-repetitive. In Listing 7-7, whether we chose the absolute or relative path to
-the `add_to_waitlist` function, every time we wanted to call `add_to_waitlist`
-we had to specify `front_of_house` and `hosting` too. Fortunately, there’s a
-way to simplify this process: we can create a shortcut to a path with the `use`
-keyword once, and then use the shorter name everywhere else in the scope.
+نوشتن مسیرهای کامل برای فراخوانی توابع میتواند خستهکننده و تکراری باشد. در لیستینگ 7-7، چه مسیر مطلق یا نسبی را برای تابع `add_to_waitlist` انتخاب کنیم، هر بار که بخواهیم این تابع را فراخوانی کنیم باید `front_of_house` و `hosting` را نیز مشخص کنیم. خوشبختانه، راهی برای سادهتر کردن این فرآیند وجود دارد: میتوانیم یک میانبر به یک مسیر با استفاده از کلمه کلیدی `use` ایجاد کنیم و سپس در هر جای دیگر محدوده، از نام کوتاهتر استفاده کنیم.
-In Listing 7-11, we bring the `crate::front_of_house::hosting` module into the
-scope of the `eat_at_restaurant` function so we only have to specify
-`hosting::add_to_waitlist` to call the `add_to_waitlist` function in
-`eat_at_restaurant`.
+در لیستینگ 7-11، ماژول `crate::front_of_house::hosting` را به محدوده تابع `eat_at_restaurant` میآوریم تا فقط نیاز به مشخص کردن `hosting::add_to_waitlist` برای فراخوانی تابع `add_to_waitlist` در `eat_at_restaurant` داشته باشیم.
-
+
```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-11/src/lib.rs}}
@@ -20,18 +12,11 @@ scope of the `eat_at_restaurant` function so we only have to specify
-Adding `use` and a path in a scope is similar to creating a symbolic link in
-the filesystem. By adding `use crate::front_of_house::hosting` in the crate
-root, `hosting` is now a valid name in that scope, just as though the `hosting`
-module had been defined in the crate root. Paths brought into scope with `use`
-also check privacy, like any other paths.
+اضافه کردن `use` و یک مسیر در یک محدوده مشابه ایجاد یک لینک نمادین در فایلسیستم است. با اضافه کردن `use crate::front_of_house::hosting` در ریشه جعبه (crate)، `hosting` اکنون یک نام معتبر در آن محدوده است، درست مانند اینکه ماژول `hosting` در ریشه جعبه (crate) تعریف شده باشد. مسیرهایی که با `use` به محدوده آورده میشوند مانند هر مسیر دیگری حریم خصوصی را بررسی میکنند.
-Note that `use` only creates the shortcut for the particular scope in which the
-`use` occurs. Listing 7-12 moves the `eat_at_restaurant` function into a new
-child module named `customer`, which is then a different scope than the `use`
-statement, so the function body won’t compile.
+توجه کنید که `use` فقط میانبر را برای محدوده خاصی که در آن `use` استفاده شده ایجاد میکند. لیستینگ 7-12 تابع `eat_at_restaurant` را به یک زیرماژول جدید به نام `customer` منتقل میکند که سپس یک محدوده متفاوت از دستور `use` است، بنابراین بدنه تابع کامپایل نمیشود.
-
+
```rust,noplayground,test_harness,does_not_compile,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-12/src/lib.rs}}
@@ -39,26 +24,19 @@ statement, so the function body won’t compile.
-The compiler error shows that the shortcut no longer applies within the
-`customer` module:
+خطای کامپایلر نشان میدهد که میانبر دیگر در ماژول `customer` اعمال نمیشود:
```console
{{#include ../listings/ch07-managing-growing-projects/listing-07-12/output.txt}}
```
-Notice there’s also a warning that the `use` is no longer used in its scope! To
-fix this problem, move the `use` within the `customer` module too, or reference
-the shortcut in the parent module with `super::hosting` within the child
-`customer` module.
+توجه کنید که همچنین یک هشدار وجود دارد که `use` دیگر در محدوده خود استفاده نمیشود! برای رفع این مشکل، دستور `use` را نیز به داخل ماژول `customer` منتقل کنید، یا میانبر را در ماژول والد با `super::hosting` در داخل ماژول `customer` ارجاع دهید.
-### Creating Idiomatic `use` Paths
+### ایجاد مسیرهای `use` به صورت ایدیوماتیک
-In Listing 7-11, you might have wondered why we specified `use
-crate::front_of_house::hosting` and then called `hosting::add_to_waitlist` in
-`eat_at_restaurant`, rather than specifying the `use` path all the way out to
-the `add_to_waitlist` function to achieve the same result, as in Listing 7-13.
+در لیستینگ 7-11، ممکن است این سوال پیش بیاید که چرا ما `use crate::front_of_house::hosting` را مشخص کردهایم و سپس `hosting::add_to_waitlist` را در `eat_at_restaurant` فراخوانی کردهایم، به جای اینکه مسیر `use` را تا تابع `add_to_waitlist` مشخص کنیم تا همان نتیجه را به دست آوریم، همانطور که در لیستینگ 7-13 نشان داده شده است.
-
+
```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-13/src/lib.rs}}
@@ -66,20 +44,11 @@ the `add_to_waitlist` function to achieve the same result, as in Listing 7-13.
-Although both Listing 7-11 and Listing 7-13 accomplish the same task, Listing
-7-11 is the idiomatic way to bring a function into scope with `use`. Bringing
-the function’s parent module into scope with `use` means we have to specify the
-parent module when calling the function. Specifying the parent module when
-calling the function makes it clear that the function isn’t locally defined
-while still minimizing repetition of the full path. The code in Listing 7-13 is
-unclear as to where `add_to_waitlist` is defined.
+اگرچه هم لیستینگ 7-11 و هم لیستینگ 7-13 کار مشابهی انجام میدهند، لیستینگ 7-11 روش ایدیوماتیک برای وارد کردن یک تابع به محدوده با `use` است. وارد کردن ماژول والد تابع با `use` به این معنا است که باید ماژول والد را هنگام فراخوانی تابع مشخص کنیم. مشخص کردن ماژول والد هنگام فراخوانی تابع نشان میدهد که تابع به صورت محلی تعریف نشده است، در حالی که همچنان تکرار مسیر کامل را به حداقل میرساند. کد موجود در لیستینگ 7-13 مشخص نمیکند که `add_to_waitlist` کجا تعریف شده است.
-On the other hand, when bringing in structs, enums, and other items with `use`,
-it’s idiomatic to specify the full path. Listing 7-14 shows the idiomatic way
-to bring the standard library’s `HashMap` struct into the scope of a binary
-crate.
+از طرف دیگر، وقتی ساختارها، enumها، و سایر آیتمها را با `use` وارد میکنیم، ایدیوماتیک است که مسیر کامل را مشخص کنیم. لیستینگ 7-14 روش ایدیوماتیک برای وارد کردن ساختار `HashMap` از کتابخانه استاندارد به محدوده جعبه (crate) باینری را نشان میدهد.
-
+
```rust
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-14/src/main.rs}}
@@ -87,15 +56,11 @@ crate.
-There’s no strong reason behind this idiom: it’s just the convention that has
-emerged, and folks have gotten used to reading and writing Rust code this way.
+هیچ دلیل قوی پشت این عرف نیست: این فقط کنوانسیونی است که در جامعه Rust به وجود آمده و افراد به خواندن و نوشتن کد Rust به این روش عادت کردهاند.
-The exception to this idiom is if we’re bringing two items with the same name
-into scope with `use` statements, because Rust doesn’t allow that. Listing 7-15
-shows how to bring two `Result` types into scope that have the same name but
-different parent modules, and how to refer to them.
+استثنای این عرف زمانی است که دو آیتم با نام یکسان را با دستورات `use` وارد محدوده میکنیم، زیرا Rust این اجازه را نمیدهد. لیستینگ 7-15 نشان میدهد که چگونه دو نوع `Result` را که نام یکسانی دارند اما از ماژولهای والد متفاوتی میآیند وارد محدوده کنیم و چگونه به آنها ارجاع دهیم.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-15/src/lib.rs:here}}
@@ -103,19 +68,13 @@ different parent modules, and how to refer to them.
-As you can see, using the parent modules distinguishes the two `Result` types.
-If instead we specified `use std::fmt::Result` and `use std::io::Result`, we’d
-have two `Result` types in the same scope, and Rust wouldn’t know which one we
-meant when we used `Result`.
+همانطور که میبینید، استفاده از ماژولهای والد دو نوع `Result` را از هم متمایز میکند. اگر به جای آن `use std::fmt::Result` و `use std::io::Result` مشخص کنیم، دو نوع `Result` در یک محدوده خواهیم داشت و Rust نمیتواند بفهمد منظور ما از `Result` کدام است.
-### Providing New Names with the `as` Keyword
+### ارائه نامهای جدید با کلمه کلیدی `as`
-There’s another solution to the problem of bringing two types of the same name
-into the same scope with `use`: after the path, we can specify `as` and a new
-local name, or _alias_, for the type. Listing 7-16 shows another way to write
-the code in Listing 7-15 by renaming one of the two `Result` types using `as`.
+یک راهحل دیگر برای مشکل وارد کردن دو نوع با نام یکسان به یک محدوده با `use` این است که پس از مسیر، با استفاده از `as` یک نام محلی جدید یا _نام مستعار_ برای نوع مشخص کنیم. لیستینگ 7-16 راه دیگری برای نوشتن کد در لیستینگ 7-15 را نشان میدهد که در آن یکی از دو نوع `Result` را با استفاده از `as` تغییر نام دادهایم.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-16/src/lib.rs:here}}
@@ -123,24 +82,20 @@ the code in Listing 7-15 by renaming one of the two `Result` types using `as`.
-In the second `use` statement, we chose the new name `IoResult` for the
-`std::io::Result` type, which won’t conflict with the `Result` from `std::fmt`
-that we’ve also brought into scope. Listing 7-15 and Listing 7-16 are
-considered idiomatic, so the choice is up to you!
+در دستور دوم `use`، ما نام جدید `IoResult` را برای نوع `std::io::Result` انتخاب کردیم، که با نوع `Result` از `std::fmt` که آن را نیز وارد محدوده کردهایم، تضاد نخواهد داشت. هر دو لیستینگ 7-15 و 7-16 ایدیوماتیک در نظر گرفته میشوند، بنابراین انتخاب با شماست!
-### Re-exporting Names with `pub use`
+### دوباره صادر کردن نامها با `pub use`
-When we bring a name into scope with the `use` keyword, the name is private to
-the scope into which we imported it. To enable code outside that scope to refer
-to that name as if it had been defined in that scope, we can combine `pub` and
-`use`. This technique is called _re-exporting_ because we’re bringing an item
-into scope but also making that item available for others to bring into their
-scope.
+وقتی با استفاده از کلیدواژهی `use` یک نام را وارد حوزهای میکنیم،
+آن نام تنها در همان حوزه خصوصی است که در آن وارد شده است.
+برای اینکه کد خارج از آن حوزه نیز بتواند به آن نام دسترسی داشته باشد،
+انگار که در همان حوزه تعریف شده است، میتوانیم `pub` و `use` را با هم ترکیب کنیم.
+این تکنیک _re-exporting_ نامیده میشود، زیرا در حالی که یک آیتم را وارد حوزه میکنیم،
+همزمان آن را برای دیگران نیز قابل دسترس میکنیم تا بتوانند آن را وارد حوزهی خود کنند.
-Listing 7-17 shows the code in Listing 7-11 with `use` in the root module
-changed to `pub use`.
+لیستینگ 7-17 کد موجود در لیستینگ 7-11 را با تغییر دستور `use` در ماژول ریشه به `pub use` نشان میدهد.
-
+
```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-17/src/lib.rs}}
@@ -148,35 +103,19 @@ changed to `pub use`.
-Before this change, external code would have to call the `add_to_waitlist`
-function by using the path
-`restaurant::front_of_house::hosting::add_to_waitlist()`, which also would have
-required the `front_of_house` module to be marked as `pub`. Now that this `pub
-use` has re-exported the `hosting` module from the root module, external code
-can use the path `restaurant::hosting::add_to_waitlist()` instead.
-
-Re-exporting is useful when the internal structure of your code is different
-from how programmers calling your code would think about the domain. For
-example, in this restaurant metaphor, the people running the restaurant think
-about “front of house” and “back of house.” But customers visiting a restaurant
-probably won’t think about the parts of the restaurant in those terms. With `pub
-use`, we can write our code with one structure but expose a different structure.
-Doing so makes our library well organized for programmers working on the library
-and programmers calling the library. We’ll look at another example of `pub use`
-and how it affects your crate’s documentation in [“Exporting a Convenient Public
-API with `pub use`”][ch14-pub-use] in Chapter 14.
-
-### Using External Packages
-
-In Chapter 2, we programmed a guessing game project that used an external
-package called `rand` to get random numbers. To use `rand` in our project, we
-added this line to _Cargo.toml_:
-
-
+قبل از این تغییر، کد خارجی باید تابع `add_to_waitlist` را با استفاده از مسیر `restaurant::front_of_house::hosting::add_to_waitlist()` فراخوانی میکرد، که همچنین نیاز داشت ماژول `front_of_house` به عنوان `pub` علامتگذاری شود. حالا که این `pub use` ماژول `hosting` را از ماژول ریشه دوباره صادر کرده است، کد خارجی میتواند از مسیر `restaurant::hosting::add_to_waitlist()` استفاده کند.
+
+Re-exporting زمانی مفید است که ساختار داخلی کد شما با نحوهی تفکر برنامهنویسانی که از کد شما استفاده میکنند دربارهی دامنه، متفاوت باشد.
+برای مثال، در این تمثیل رستوران، کسانی که رستوران را اداره میکنند دربارهی «بخش جلویی» (front of house) و «بخش پشتی» (back of house) فکر میکنند.
+اما مشتریانی که به رستوران میآیند احتمالاً دربارهی قسمتهای رستوران با چنین اصطلاحاتی فکر نمیکنند.
+با استفاده از `pub use` میتوانیم کد خود را با یک ساختار بنویسیم ولی ساختاری متفاوت را در معرض استفاده قرار دهیم.
+این کار باعث میشود کتابخانهی ما هم برای برنامهنویسانی که روی کتابخانه کار میکنند و هم برای برنامهنویسانی که از آن استفاده میکنند، بهخوبی سازماندهی شده باشد.
+در فصل ۱۴، در بخش [“صادرات یک API عمومی راحت با استفاده از `pub use`”][ch14-pub-use]،
+مثال دیگری از `pub use` و تأثیر آن بر مستندات crate شما را بررسی خواهیم کرد.
+
+### استفاده از بستههای خارجی
+
+در فصل ۲، ما یک پروژه بازی حدسزنی برنامهریزی کردیم که از یک بسته خارجی به نام `rand` برای تولید اعداد تصادفی استفاده میکرد. برای استفاده از `rand` در پروژه خود، این خط را به _Cargo.toml_ اضافه کردیم:
@@ -186,44 +125,31 @@ added this line to _Cargo.toml_:
-Adding `rand` as a dependency in _Cargo.toml_ tells Cargo to download the
-`rand` package and any dependencies from [crates.io](https://crates.io/) and
-make `rand` available to our project.
+اضافه کردن `rand` به عنوان یک وابستگی در _Cargo.toml_ به Cargo میگوید که بسته `rand` و هرگونه وابستگی را از [crates.io](https://crates.io/) دانلود کرده و `rand` را در پروژه ما در دسترس قرار دهد.
-Then, to bring `rand` definitions into the scope of our package, we added a
-`use` line starting with the name of the crate, `rand`, and listed the items we
-wanted to bring into scope. Recall that in [“Generating a Random
-Number”][rand] in Chapter 2, we brought the `Rng` trait into
-scope and called the `rand::thread_rng` function:
+سپس، برای وارد کردن تعاریف crate `rand` به حوزهی پکیج خود،
+یک خط `use` اضافه کردیم که با نام crate، یعنی `rand`، آغاز شد
+و آیتمهایی را که میخواستیم وارد حوزه کنیم، فهرست کردیم.
+به یاد داشته باشید که در بخش [“تولید یک عدد تصادفی”][rand] در فصل ۲،
+`trait` مربوط به `Rng` را وارد حوزه کردیم و تابع `rand::thread_rng` را فراخوانی نمودیم:
```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:ch07-04}}
```
-Members of the Rust community have made many packages available at
-[crates.io](https://crates.io/), and pulling any of them into your package
-involves these same steps: listing them in your package’s _Cargo.toml_ file and
-using `use` to bring items from their crates into scope.
+اعضای جامعه Rust بسیاری از بستهها را در [crates.io](https://crates.io/) به اشتراک گذاشتهاند، و وارد کردن هر یک از آنها به بسته شما شامل این مراحل است: فهرست کردن آنها در فایل _Cargo.toml_ بسته شما و استفاده از `use` برای وارد کردن آیتمها از جعبه (crate) آنها به محدوده.
-Note that the standard `std` library is also a crate that’s external to our
-package. Because the standard library is shipped with the Rust language, we
-don’t need to change _Cargo.toml_ to include `std`. But we do need to refer to
-it with `use` to bring items from there into our package’s scope. For example,
-with `HashMap` we would use this line:
+توجه داشته باشید که کتابخانه استاندارد `std` نیز یک جعبه (crate) خارجی برای بسته ما است. از آنجا که کتابخانه استاندارد همراه با زبان Rust ارائه میشود، نیازی به تغییر _Cargo.toml_ برای گنجاندن `std` نداریم. اما برای وارد کردن آیتمها از آن به محدوده بسته خود، باید به آن با `use` ارجاع دهیم. برای مثال، با `HashMap` از این خط استفاده میکردیم:
```rust
use std::collections::HashMap;
```
-This is an absolute path starting with `std`, the name of the standard library
-crate.
+این یک مسیر مطلق است که با `std`، نام جعبه (crate) کتابخانه استاندارد، شروع میشود.
-### Using Nested Paths to Clean Up Large `use` Lists
+### استفاده از مسیرهای تو در تو برای سادهسازی لیستهای بزرگ `use`
-If we’re using multiple items defined in the same crate or same module, listing
-each item on its own line can take up a lot of vertical space in our files. For
-example, these two `use` statements we had in the guessing game in Listing 2-4
-bring items from `std` into scope:
+اگر از چندین آیتم تعریفشده در یک جعبه (crate) یا ماژول استفاده کنیم، فهرست کردن هر آیتم در خط خود میتواند فضای عمودی زیادی در فایلهای ما اشغال کند. برای مثال، این دو دستور `use` که در بازی حدسزنی در لیستینگ ۲-۴ استفاده کردیم آیتمهایی از `std` را به محدوده میآورند:
@@ -233,12 +159,9 @@ bring items from `std` into scope:
-Instead, we can use nested paths to bring the same items into scope in one
-line. We do this by specifying the common part of the path, followed by two
-colons, and then curly brackets around a list of the parts of the paths that
-differ, as shown in Listing 7-18.
+در عوض، میتوانیم از مسیرهای تو در تو استفاده کنیم تا همان آیتمها را در یک خط به محدوده بیاوریم. این کار را با مشخص کردن بخش مشترک مسیر، به دنبال آن دو نقطه دوبل و سپس یک لیست از بخشهای متفاوت مسیرها در داخل آکولاد انجام میدهیم، همانطور که در لیستینگ 7-18 نشان داده شده است.
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-18/src/main.rs:here}}
@@ -246,16 +169,11 @@ differ, as shown in Listing 7-18.
-In bigger programs, bringing many items into scope from the same crate or
-module using nested paths can reduce the number of separate `use` statements
-needed by a lot!
+در برنامههای بزرگتر، وارد کردن بسیاری از آیتمها از یک جعبه (crate) یا ماژول مشابه با استفاده از مسیرهای تو در تو میتواند تعداد دستورات `use` جداگانه مورد نیاز را به طور قابلتوجهی کاهش دهد.
-We can use a nested path at any level in a path, which is useful when combining
-two `use` statements that share a subpath. For example, Listing 7-19 shows two
-`use` statements: one that brings `std::io` into scope and one that brings
-`std::io::Write` into scope.
+ما میتوانیم در هر سطحی از یک مسیر، از یک مسیر تو در تو استفاده کنیم، که این کار در مواقعی که دو دستور `use` دارای یک زیرمسیر مشترک هستند، مفید است. برای مثال، لیستینگ 7-19 دو دستور `use` را نشان میدهد: یکی که `std::io` را به محدوده وارد میکند و دیگری که `std::io::Write` را به محدوده وارد میکند.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-19/src/lib.rs}}
@@ -263,11 +181,9 @@ two `use` statements that share a subpath. For example, Listing 7-19 shows two
-The common part of these two paths is `std::io`, and that’s the complete first
-path. To merge these two paths into one `use` statement, we can use `self` in
-the nested path, as shown in Listing 7-20.
+بخش مشترک این دو مسیر، `std::io` است که مسیر کامل اولین دستور `use` را تشکیل میدهد. برای ترکیب این دو مسیر به یک دستور `use`، میتوانیم از `self` در مسیر تو در تو استفاده کنیم، همانطور که در لیستینگ 7-20 نشان داده شده است.
-
+
```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-20/src/lib.rs}}
@@ -275,32 +191,30 @@ the nested path, as shown in Listing 7-20.
-This line brings `std::io` and `std::io::Write` into scope.
+این خط، `std::io` و `std::io::Write` را به محدوده وارد میکند.
-### The Glob Operator
+### عملگر Glob
-If we want to bring _all_ public items defined in a path into scope, we can
-specify that path followed by the `*` glob operator:
+اگر بخواهیم _تمام_ آیتمهای عمومی تعریفشده در یک مسیر را به محدوده وارد کنیم، میتوانیم آن مسیر را به همراه عملگر `*` مشخص کنیم:
```rust
use std::collections::*;
```
-This `use` statement brings all public items defined in `std::collections` into
-the current scope. Be careful when using the glob operator! Glob can make it
-harder to tell what names are in scope and where a name used in your program
-was defined. Additionally, if the dependency changes its definitions, what
-you’ve imported changes as well, which may lead to compiler errors when you
-upgrade the dependency if the dependency adds a definition with the same name
-as a definition of yours in the same scope, for example.
-
-The glob operator is often used when testing to bring everything under test into
-the `tests` module; we’ll talk about that in [“How to Write
-Tests”][writing-tests] in Chapter 11. The glob operator is also
-sometimes used as part of the prelude pattern: see [the standard library
-documentation](../std/prelude/index.html#other-preludes) for more
-information on that pattern.
+این دستور `use` تمام آیتمهای عمومی تعریفشده در `std::collections` را وارد حوزهی فعلی میکند.
+در استفاده از عملگر glob دقت کنید!
+استفاده از glob میتواند باعث شود تشخیص اینکه چه نامهایی در حوزه هستند
+و یک نام استفادهشده در برنامه از کجا آمده، دشوارتر شود.
+علاوه بر این، اگر وابستگی تغییراتی در تعاریف خود ایجاد کند، آنچه شما وارد کردهاید نیز تغییر میکند،
+که ممکن است هنگام بهروزرسانی وابستگی، باعث بروز خطای کامپایلر شود—
+برای مثال، اگر وابستگی تعریفی با همان نامی اضافه کند که شما نیز در همان حوزه تعریف کردهاید.
+
+عملگر glob اغلب هنگام تست برای وارد کردن تمام آیتمهای تحت تست به ماژول `tests` استفاده میشود؛
+در فصل ۱۱ در بخش [“چگونه تست بنویسیم”][writing-tests] دربارهی آن صحبت خواهیم کرد.
+همچنین، عملگر glob گاهی در قالب الگوی prelude نیز بهکار میرود؛
+برای اطلاعات بیشتر دربارهی این الگو، به [مستندات کتابخانهی استاندارد](https://doc.rust-lang.org/std/prelude/index.html#other-preludes) مراجعه کنید.
[ch14-pub-use]: ch14-02-publishing-to-crates-io.html#exporting-a-convenient-public-api-with-pub-use
[rand]: ch02-00-guessing-game-tutorial.html#generating-a-random-number
[writing-tests]: ch11-01-writing-tests.html#how-to-write-tests
+
diff --git a/src/ch07-05-separating-modules-into-different-files.md b/src/ch07-05-separating-modules-into-different-files.md
index 4de9b6908f..f20e72ccb8 100644
--- a/src/ch07-05-separating-modules-into-different-files.md
+++ b/src/ch07-05-separating-modules-into-different-files.md
@@ -1,22 +1,12 @@
-## Separating Modules into Different Files
+## جدا کردن ماژولها به فایلهای مختلف
-So far, all the examples in this chapter defined multiple modules in one file.
-When modules get large, you might want to move their definitions to a separate
-file to make the code easier to navigate.
+تا به اینجا، تمام مثالهای این فصل چندین ماژول را در یک فایل تعریف کردهاند. هنگامی که ماژولها بزرگ میشوند، ممکن است بخواهید تعریفهای آنها را به یک فایل جداگانه منتقل کنید تا کد آسانتر خوانده و مدیریت شود.
-For example, let’s start from the code in Listing 7-17 that had multiple
-restaurant modules. We’ll extract modules into files instead of having all the
-modules defined in the crate root file. In this case, the crate root file is
-_src/lib.rs_, but this procedure also works with binary crates whose crate root
-file is _src/main.rs_.
+برای مثال، بیایید از کد موجود در لیستینگ 7-17 شروع کنیم که شامل چندین ماژول مرتبط با رستوران بود. ما این ماژولها را به جای تعریف در فایل ریشه جعبه (crate)، به فایلهای جداگانه منتقل میکنیم. در این مثال، فایل ریشه جعبه (crate) _src/lib.rs_ است، اما این روش برای جعبهها (crates)ی باینری که فایل ریشه آنها _src/main.rs_ است نیز کار میکند.
-First we’ll extract the `front_of_house` module to its own file. Remove the
-code inside the curly brackets for the `front_of_house` module, leaving only
-the `mod front_of_house;` declaration, so that _src/lib.rs_ contains the code
-shown in Listing 7-21. Note that this won’t compile until we create the
-_src/front_of_house.rs_ file in Listing 7-22.
+ابتدا ماژول `front_of_house` را به فایل خودش منتقل میکنیم. کدی که داخل آکولادهای ماژول `front_of_house` است را حذف کرده و فقط اعلان `mod front_of_house;` را باقی میگذاریم. نتیجه کد در _src/lib.rs_ مانند لیستینگ 7-21 خواهد بود. توجه داشته باشید که این کد تا زمانی که فایل _src/front_of_house.rs_ مطابق لیستینگ 7-22 ایجاد نشود کامپایل نخواهد شد.
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs}}
@@ -24,12 +14,9 @@ _src/front_of_house.rs_ file in Listing 7-22.
-Next, place the code that was in the curly brackets into a new file named
-_src/front_of_house.rs_, as shown in Listing 7-22. The compiler knows to look
-in this file because it came across the module declaration in the crate root
-with the name `front_of_house`.
+سپس، کدی که داخل آکولادهای ماژول `front_of_house` بود را به یک فایل جدید به نام _src/front_of_house.rs_ منتقل میکنیم، همانطور که در لیستینگ 7-22 نشان داده شده است. کامپایلر میداند که باید این فایل را بررسی کند زیرا در فایل ریشه جعبه (crate) با نام `front_of_house` اعلان ماژول را دیده است.
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs}}
@@ -37,22 +24,11 @@ with the name `front_of_house`.
-Note that you only need to load a file using a `mod` declaration _once_ in your
-module tree. Once the compiler knows the file is part of the project (and knows
-where in the module tree the code resides because of where you’ve put the `mod`
-statement), other files in your project should refer to the loaded file’s code
-using a path to where it was declared, as covered in the [“Paths for Referring
-to an Item in the Module Tree”][paths] section. In other words,
-`mod` is _not_ an “include” operation that you may have seen in other
-programming languages.
+توجه داشته باشید که شما فقط یک بار نیاز دارید تا یک فایل را با استفاده از دستور `mod` در درخت ماژول خود بارگذاری کنید. وقتی کامپایلر میفهمد که فایل بخشی از پروژه است (و میفهمد که کد در کجای درخت ماژول قرار دارد به خاطر جایی که دستور `mod` را قرار دادهاید)، سایر فایلهای پروژه شما باید با استفاده از مسیری که به محل اعلان فایل اشاره میکند به کد بارگذاری شده ارجاع دهند، همانطور که در بخش [«مسیرها برای اشاره به یک آیتم در درخت ماژول»][paths] توضیح داده شد. به عبارت دیگر، `mod` یک عملیات "شامل کردن" (include) نیست که ممکن است در زبانهای برنامهنویسی دیگر دیده باشید.
-Next, we’ll extract the `hosting` module to its own file. The process is a bit
-different because `hosting` is a child module of `front_of_house`, not of the
-root module. We’ll place the file for `hosting` in a new directory that will be
-named for its ancestors in the module tree, in this case _src/front_of_house_.
+در مرحله بعد، ماژول `hosting` را به فایل خودش منتقل میکنیم. این فرآیند کمی متفاوت است زیرا `hosting` یک زیرماژول از `front_of_house` است، نه از ماژول ریشه. فایل مربوط به `hosting` را در یک دایرکتوری جدید قرار میدهیم که به نام والدین آن در درخت ماژول نامگذاری شده است، که در اینجا _src/front_of_house_ است.
-To start moving `hosting`, we change _src/front_of_house.rs_ to contain only
-the declaration of the `hosting` module:
+برای شروع انتقال `hosting`، فایل _src/front_of_house.rs_ را تغییر میدهیم تا فقط شامل اعلان ماژول `hosting` باشد:
@@ -62,8 +38,7 @@ the declaration of the `hosting` module:
-Then we create a _src/front_of_house_ directory and a _hosting.rs_ file to
-contain the definitions made in the `hosting` module:
+سپس یک دایرکتوری به نام _src/front_of_house_ و یک فایل _hosting.rs_ ایجاد میکنیم تا تعریفهایی که در ماژول `hosting` انجام شدهاند را در آن قرار دهیم:
@@ -73,57 +48,32 @@ contain the definitions made in the `hosting` module:
-If we instead put _hosting.rs_ in the _src_ directory, the compiler would
-expect the _hosting.rs_ code to be in a `hosting` module declared in the crate
-root, and not declared as a child of the `front_of_house` module. The
-compiler’s rules for which files to check for which modules’ code mean the
-directories and files more closely match the module tree.
+اگر به جای آن فایل _hosting.rs_ را در دایرکتوری _src_ قرار دهیم، کامپایلر انتظار خواهد داشت که کد _hosting.rs_ در یک ماژول `hosting` که در ریشه جعبه (crate) اعلان شده باشد قرار داشته باشد، نه به عنوان یک زیرماژول از ماژول `front_of_house`. قوانین کامپایلر برای مشخص کردن این که کدام فایلها برای کدام ماژولها بررسی شوند، به این معناست که دایرکتوریها و فایلها با درخت ماژول مطابقت بیشتری دارند.
-> ### Alternate File Paths
+> ### مسیرهای فایل جایگزین
>
-> So far we’ve covered the most idiomatic file paths the Rust compiler uses,
-> but Rust also supports an older style of file path. For a module named
-> `front_of_house` declared in the crate root, the compiler will look for the
-> module’s code in:
+> تاکنون مسیرهای فایل ایدیوماتیک را که کامپایلر Rust استفاده میکند پوشش دادهایم، اما Rust از یک سبک قدیمیتر از مسیر فایل نیز پشتیبانی میکند. برای یک ماژول به نام `front_of_house` که در ریشه جعبه (crate) اعلان شده است، کامپایلر کد ماژول را در مکانهای زیر جستجو میکند:
>
-> - _src/front_of_house.rs_ (what we covered)
-> - _src/front_of_house/mod.rs_ (older style, still supported path)
+> - _src/front_of_house.rs_ (روشی که پوشش داده شد)
+> - _src/front_of_house/mod.rs_ (مسیر قدیمیتر، همچنان پشتیبانیشده)
>
-> For a module named `hosting` that is a submodule of `front_of_house`, the
-> compiler will look for the module’s code in:
+> برای یک ماژول به نام `hosting` که زیرماژولی از `front_of_house` است، کامپایلر کد ماژول را در مکانهای زیر جستجو میکند:
>
-> - _src/front_of_house/hosting.rs_ (what we covered)
-> - _src/front_of_house/hosting/mod.rs_ (older style, still supported path)
+> - _src/front_of_house/hosting.rs_ (روشی که پوشش داده شد)
+> - _src/front_of_house/hosting/mod.rs_ (مسیر قدیمیتر، همچنان پشتیبانیشده)
>
-> If you use both styles for the same module, you’ll get a compiler error.
-> Using a mix of both styles for different modules in the same project is
-> allowed, but might be confusing for people navigating your project.
+> اگر هر دو سبک را برای یک ماژول استفاده کنید، یک خطای کامپایلر دریافت خواهید کرد. استفاده از ترکیبی از هر دو سبک برای ماژولهای مختلف در یک پروژه مجاز است، اما ممکن است برای کسانی که پروژه شما را مرور میکنند گیجکننده باشد.
>
-> The main downside to the style that uses files named _mod.rs_ is that your
-> project can end up with many files named _mod.rs_, which can get confusing
-> when you have them open in your editor at the same time.
-
-We’ve moved each module’s code to a separate file, and the module tree remains
-the same. The function calls in `eat_at_restaurant` will work without any
-modification, even though the definitions live in different files. This
-technique lets you move modules to new files as they grow in size.
-
-Note that the `pub use crate::front_of_house::hosting` statement in
-_src/lib.rs_ also hasn’t changed, nor does `use` have any impact on what files
-are compiled as part of the crate. The `mod` keyword declares modules, and Rust
-looks in a file with the same name as the module for the code that goes into
-that module.
-
-## Summary
-
-Rust lets you split a package into multiple crates and a crate into modules so
-you can refer to items defined in one module from another module. You can do
-this by specifying absolute or relative paths. These paths can be brought into
-scope with a `use` statement so you can use a shorter path for multiple uses of
-the item in that scope. Module code is private by default, but you can make
-definitions public by adding the `pub` keyword.
-
-In the next chapter, we’ll look at some collection data structures in the
-standard library that you can use in your neatly organized code.
+> نکته منفی اصلی سبک استفاده از فایلهایی با نام _mod.rs_ این است که پروژه شما ممکن است تعداد زیادی فایل با نام _mod.rs_ داشته باشد، که میتواند هنگام باز بودن همزمان این فایلها در ویرایشگر شما گیجکننده باشد.
+
+ما کد هر ماژول را به یک فایل جداگانه منتقل کردهایم و درخت ماژول به همان شکل باقی مانده است. فراخوانی توابع در `eat_at_restaurant` بدون هیچ تغییری کار خواهد کرد، حتی اگر تعریفها در فایلهای مختلف قرار داشته باشند. این تکنیک به شما امکان میدهد ماژولها را به فایلهای جدید منتقل کنید زیرا اندازه آنها افزایش مییابد.
+
+توجه داشته باشید که دستور `pub use crate::front_of_house::hosting` در _src/lib.rs_ نیز تغییری نکرده است، و همچنین `use` هیچ تأثیری بر اینکه چه فایلهایی به عنوان بخشی از جعبه (crate) کامپایل شوند ندارد. کلمه کلیدی `mod` ماژولها را اعلان میکند و Rust در فایلی با همان نام ماژول به دنبال کدی میگردد که وارد آن ماژول شود.
+
+## خلاصه
+
+Rust به شما اجازه میدهد یک بسته را به چندین جعبه (crate) و یک جعبه (crate) را به ماژولها تقسیم کنید تا بتوانید به آیتمهایی که در یک ماژول تعریف شدهاند از ماژول دیگری ارجاع دهید. میتوانید این کار را با مشخص کردن مسیرهای مطلق یا نسبی انجام دهید. این مسیرها میتوانند با یک دستور `use` به محدوده وارد شوند تا بتوانید از یک مسیر کوتاهتر برای استفادههای متعدد از آن آیتم در آن محدوده استفاده کنید. کد ماژول به صورت پیشفرض خصوصی است، اما میتوانید با افزودن کلمه کلیدی `pub` تعریفها را عمومی کنید.
+
+در فصل بعدی، به برخی از ساختارهای دادهای مجموعه در کتابخانه استاندارد خواهیم پرداخت که میتوانید در کد مرتب و سازماندهیشده خود از آنها استفاده کنید.
[paths]: ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html
diff --git a/src/ch08-00-common-collections.md b/src/ch08-00-common-collections.md
index adf600c942..ab0646b0fc 100644
--- a/src/ch08-00-common-collections.md
+++ b/src/ch08-00-common-collections.md
@@ -1,25 +1,25 @@
-# Common Collections
+# مجموعههای معمول
-Rust’s standard library includes a number of very useful data structures called
-_collections_. Most other data types represent one specific value, but
-collections can contain multiple values. Unlike the built-in array and tuple
-types, the data that these collections point to is stored on the heap, which
-means the amount of data does not need to be known at compile time and can grow
-or shrink as the program runs. Each kind of collection has different
-capabilities and costs, and choosing an appropriate one for your current
-situation is a skill you’ll develop over time. In this chapter, we’ll discuss
-three collections that are used very often in Rust programs:
+کتابخانهی استاندارد Rust شامل تعدادی ساختار دادهی بسیار مفید به نام *collections* (مجموعهها) است.
+اکثر انواع دادهی دیگر نمایانگر یک مقدار خاص هستند، اما مجموعهها میتوانند چندین مقدار را در خود نگه دارند.
+بر خلاف انواع داخلی مانند `array` و `tuple`، دادههایی که این مجموعهها به آنها اشاره میکنند،
+روی heap ذخیره میشوند؛ به این معنا که نیازی نیست اندازهی دادهها در زمان کامپایل مشخص باشد،
+و این اندازه میتواند در طول اجرای برنامه رشد یا کاهش یابد.
+هر نوع مجموعه قابلیتها و هزینههای متفاوتی دارد،
+و انتخاب مجموعهی مناسب برای موقعیت فعلی، مهارتی است که با گذشت زمان به دست خواهید آورد.
+در این فصل، دربارهی سه نوع مجموعه که بسیار در برنامههای Rust استفاده میشوند صحبت خواهیم کرد:
-- A _vector_ allows you to store a variable number of values next to each other.
-- A _string_ is a collection of characters. We’ve mentioned the `String` type
- previously, but in this chapter we’ll talk about it in depth.
-- A _hash map_ allows you to associate a value with a specific key. It’s a
- particular implementation of the more general data structure called a _map_.
+- یک _بردار_ به شما اجازه میدهد که تعداد متغیری از مقادیر را در کنار
+ یکدیگر ذخیره کنید.
+- یک _رشته_ یک مجموعه از کاراکترها است. ما قبلاً نوع `String` را ذکر کردهایم،
+ اما در این فصل به طور عمیقتر درباره آن صحبت خواهیم کرد.
+- یک _هش مپ_ به شما اجازه میدهد که یک مقدار را با یک کلید مشخص مرتبط کنید.
+ این یک پیادهسازی خاص از ساختار داده کلیتر به نام _نقشه_ است.
-To learn about the other kinds of collections provided by the standard library,
-see [the documentation][collections].
+برای یادگیری درباره انواع دیگر مجموعههایی که توسط کتابخانه استاندارد ارائه شدهاند،
+[مستندات][collections] را مشاهده کنید.
-We’ll discuss how to create and update vectors, strings, and hash maps, as well
-as what makes each special.
+ما درباره نحوه ایجاد و بهروزرسانی بردارها، رشتهها و هش مپها، همچنین ویژگیهایی
+که هر کدام را خاص میکند، صحبت خواهیم کرد.
-[collections]: ../std/collections/index.html
+[collections]: https://doc.rust-lang.org/std/collections/index.html
diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md
index d62b7f65d8..6a2e13fd7b 100644
--- a/src/ch08-01-vectors.md
+++ b/src/ch08-01-vectors.md
@@ -1,17 +1,17 @@
-## Storing Lists of Values with Vectors
+## ذخیره لیستهایی از مقادیر با بردارها
-The first collection type we’ll look at is `Vec`, also known as a _vector_.
-Vectors allow you to store more than one value in a single data structure that
-puts all the values next to each other in memory. Vectors can only store values
-of the same type. They are useful when you have a list of items, such as the
-lines of text in a file or the prices of items in a shopping cart.
+اولین نوع مجموعهای که به آن خواهیم پرداخت، `Vec` یا همان _بردار_ است.
+بردارها به شما اجازه میدهند که بیش از یک مقدار را در یک ساختار داده ذخیره کنید
+که تمامی مقادیر را در کنار یکدیگر در حافظه قرار میدهد. بردارها فقط میتوانند
+مقادیر از یک نوع را ذخیره کنند. این ابزار زمانی مفید است که لیستی از آیتمها
+مانند خطوط متنی در یک فایل یا قیمت آیتمها در یک سبد خرید داشته باشید.
-### Creating a New Vector
+### ایجاد یک بردار جدید
-To create a new empty vector, we call the `Vec::new` function, as shown in
-Listing 8-1.
+برای ایجاد یک بردار خالی جدید، از تابع `Vec::new` استفاده میکنیم، همانطور که
+در لیست ۸-۱ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}}
@@ -19,24 +19,24 @@ Listing 8-1.
-Note that we added a type annotation here. Because we aren’t inserting any
-values into this vector, Rust doesn’t know what kind of elements we intend to
-store. This is an important point. Vectors are implemented using generics;
-we’ll cover how to use generics with your own types in Chapter 10. For now,
-know that the `Vec` type provided by the standard library can hold any type.
-When we create a vector to hold a specific type, we can specify the type within
-angle brackets. In Listing 8-1, we’ve told Rust that the `Vec` in `v` will
-hold elements of the `i32` type.
-
-More often, you’ll create a `Vec` with initial values and Rust will infer
-the type of value you want to store, so you rarely need to do this type
-annotation. Rust conveniently provides the `vec!` macro, which will create a
-new vector that holds the values you give it. Listing 8-2 creates a new
-`Vec` that holds the values `1`, `2`, and `3`. The integer type is `i32`
-because that’s the default integer type, as we discussed in the [“Data
-Types”][data-types] section of Chapter 3.
-
-
+توجه داشته باشید که ما یک توضیح نوع اضافه کردهایم. چون ما هیچ مقداری به این بردار
+اضافه نکردهایم، Rust نمیداند چه نوع عناصری را قصد داریم ذخیره کنیم. این نکته
+مهمی است. بردارها با استفاده از جنریکها پیادهسازی شدهاند؛ در فصل ۱۰ خواهیم دید
+که چگونه میتوان جنریکها را در انواع خودتان استفاده کرد. در حال حاضر بدانید که
+نوع `Vec` ارائه شده توسط کتابخانه استاندارد میتواند هر نوعی را نگهداری کند.
+وقتی یک بردار برای نگهداری نوع خاصی ایجاد میکنیم، میتوانیم نوع موردنظر را داخل
+براکتهای زاویهای مشخص کنیم. در لیست ۸-۱، ما به Rust اعلام کردهایم که بردار `Vec`
+در `v` عناصر نوع `i32` را نگهداری خواهد کرد.
+
+بیشتر اوقات، شما یک `Vec` با مقادیر اولیه ایجاد خواهید کرد و Rust نوع مقادیر
+را از روی آنها استنتاج خواهد کرد، بنابراین به ندرت نیاز به توضیح نوع خواهید داشت.
+Rust به راحتی ماکروی `vec!` را فراهم میکند که یک بردار جدید ایجاد کرده و مقادیر
+مورد نظر شما را در آن قرار میدهد. لیست ۸-۲ یک بردار جدید `Vec` را ایجاد میکند
+که مقادیر `1`، `2` و `3` را نگهداری میکند. نوع عدد صحیح `i32` است چون این نوع
+پیشفرض برای اعداد صحیح است، همانطور که در بخش [“انواع دادهها”][data-types]
+فصل ۳ بحث کردیم.
+
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}}
@@ -44,16 +44,16 @@ Types”][data-types] section of Chapter 3.
-Because we’ve given initial `i32` values, Rust can infer that the type of `v`
-is `Vec`, and the type annotation isn’t necessary. Next, we’ll look at how
-to modify a vector.
+چون مقادیر اولیه `i32` دادهایم، Rust میتواند استنتاج کند که نوع `v`
+`Vec` است و نیازی به توضیح نوع نیست. حالا به نحوه بهروزرسانی یک بردار
+خواهیم پرداخت.
-### Updating a Vector
+### بهروزرسانی یک بردار
-To create a vector and then add elements to it, we can use the `push` method,
-as shown in Listing 8-3.
+برای ایجاد یک بردار و سپس اضافه کردن عناصر به آن، میتوانیم از متد `push` استفاده کنیم،
+همانطور که در لیست ۸-۳ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}}
@@ -61,21 +61,21 @@ as shown in Listing 8-3.
-As with any variable, if we want to be able to change its value, we need to
-make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers
-we place inside are all of type `i32`, and Rust infers this from the data, so
-we don’t need the `Vec` annotation.
+همانطور که با هر متغیری دیگر انجام میدهیم، اگر بخواهیم بتوانیم مقدار آن را تغییر دهیم،
+باید آن را با استفاده از کلیدواژه `mut` قابل تغییر کنیم، همانطور که در فصل ۳ بحث شد.
+اعدادی که در داخل بردار قرار میدهیم همه از نوع `i32` هستند و Rust این نوع را از دادهها
+استنتاج میکند، بنابراین نیازی به توضیح نوع `Vec` نیست.
-### Reading Elements of Vectors
+### خواندن عناصر بردار
-There are two ways to reference a value stored in a vector: via indexing or by
-using the `get` method. In the following examples, we’ve annotated the types of
-the values that are returned from these functions for extra clarity.
+دو روش برای ارجاع به یک مقدار ذخیره شده در بردار وجود دارد: از طریق استفاده از اندیس (index)یا
+با استفاده از متد `get`. در مثالهای زیر، انواع مقادیر بازگشتی از این توابع برای وضوح بیشتر
+مشخص شدهاند.
-Listing 8-4 shows both methods of accessing a value in a vector, with indexing
-syntax and the `get` method.
+لیست ۸-۴ هر دو روش دسترسی به یک مقدار در بردار، با استفاده از سینتکس اندیس (index)و متد `get`
+را نشان میدهد.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}}
@@ -83,19 +83,17 @@ syntax and the `get` method.
-Note a few details here. We use the index value of `2` to get the third element
-because vectors are indexed by number, starting at zero. Using `&` and `[]`
-gives us a reference to the element at the index value. When we use the `get`
-method with the index passed as an argument, we get an `Option<&T>` that we can
-use with `match`.
+به چند جزئیات اینجا توجه کنید. ما از مقدار اندیس (index)`2` برای دسترسی به عنصر سوم استفاده میکنیم
+زیرا بردارها با شماره از صفر اندیسگذاری میشوند. استفاده از `&` و `[]` یک مرجع به عنصر
+در مقدار اندیس (index)را به ما میدهد. وقتی از متد `get` با اندیسی که به عنوان آرگومان داده میشود
+استفاده میکنیم، یک `Option<&T>` دریافت میکنیم که میتوانیم با `match` از آن استفاده کنیم.
-Rust provides these two ways to reference an element so you can choose how the
-program behaves when you try to use an index value outside the range of
-existing elements. As an example, let’s see what happens when we have a vector
-of five elements and then we try to access an element at index 100 with each
-technique, as shown in Listing 8-5.
+Rust این دو روش ارجاع به یک عنصر را ارائه میدهد تا بتوانید انتخاب کنید که برنامه شما چگونه رفتار کند
+وقتی تلاش میکنید از یک مقدار اندیس (index)خارج از محدوده عناصر موجود استفاده کنید. به عنوان یک مثال،
+بیایید ببینیم چه اتفاقی میافتد وقتی یک بردار با پنج عنصر داشته باشیم و سپس تلاش کنیم به یک عنصر
+در اندیس (index)۱۰۰ با هر دو تکنیک دسترسی پیدا کنیم، همانطور که در لیست ۸-۵ نشان داده شده است.
-
+
```rust,should_panic,panics
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}}
@@ -103,32 +101,27 @@ technique, as shown in Listing 8-5.
-When we run this code, the first `[]` method will cause the program to panic
-because it references a nonexistent element. This method is best used when you
-want your program to crash if there’s an attempt to access an element past the
-end of the vector.
-
-When the `get` method is passed an index that is outside the vector, it returns
-`None` without panicking. You would use this method if accessing an element
-beyond the range of the vector may happen occasionally under normal
-circumstances. Your code will then have logic to handle having either
-`Some(&element)` or `None`, as discussed in Chapter 6. For example, the index
-could be coming from a person entering a number. If they accidentally enter a
-number that’s too large and the program gets a `None` value, you could tell the
-user how many items are in the current vector and give them another chance to
-enter a valid value. That would be more user-friendly than crashing the program
-due to a typo!
-
-When the program has a valid reference, the borrow checker enforces the
-ownership and borrowing rules (covered in Chapter 4) to ensure this reference
-and any other references to the contents of the vector remain valid. Recall the
-rule that states you can’t have mutable and immutable references in the same
-scope. That rule applies in Listing 8-6, where we hold an immutable reference
-to the first element in a vector and try to add an element to the end. This
-program won’t work if we also try to refer to that element later in the
-function.
-
-
+وقتی این کد را اجرا میکنیم، روش اول `[]` باعث میشود برنامه متوقف شود زیرا به یک
+عنصر غیرموجود اشاره میکند. این روش زمانی بهترین استفاده را دارد که بخواهید برنامهتان
+در صورت تلاش برای دسترسی به عنصری خارج از انتهای بردار، متوقف شود.
+
+وقتی متد `get` یک اندیس (index)خارج از بردار دریافت میکند، مقدار `None` را بدون متوقف کردن
+برنامه بازمیگرداند. شما از این روش استفاده میکنید اگر دسترسی به عنصری خارج از محدوده بردار
+ممکن است گاهبهگاه در شرایط عادی رخ دهد. کد شما سپس منطق لازم برای مدیریت داشتن
+`Some(&element)` یا `None` را خواهد داشت، همانطور که در فصل ۶ بحث شد. برای مثال،
+اندیس (index)ممکن است از یک عدد ورودی توسط کاربر بیاید. اگر کاربر تصادفاً عددی وارد کند که بیش از حد
+بزرگ باشد و برنامه مقدار `None` دریافت کند، شما میتوانید به کاربر اطلاع دهید که چند آیتم
+در بردار موجود است و به او فرصت دیگری برای وارد کردن یک مقدار معتبر بدهید. این راهکار برای
+کاربر پسندتر است تا این که برنامه به دلیل یک اشتباه تایپی متوقف شود!
+
+وقتی برنامه یک مرجع معتبر دارد، بررسیکننده قرض قوانین مالکیت و قرضگیری (که در فصل ۴ پوشش داده شد)
+را اعمال میکند تا اطمینان حاصل کند که این مرجع و هر مرجع دیگری به محتوای بردار معتبر باقی میمانند.
+به یاد بیاورید که قانون بیان میکند نمیتوانید مرجعهای قابل تغییر و غیرقابل تغییر را در یک حوزه
+داشته باشید. این قانون در لیست ۸-۶ اعمال میشود، جایی که یک مرجع غیرقابل تغییر به اولین عنصر
+در یک بردار نگه داشته شده است و سعی داریم یک عنصر به انتها اضافه کنیم. این برنامه زمانی کار نخواهد کرد
+اگر همچنین بخواهیم بعداً در تابع به آن عنصر ارجاع دهیم.
+
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}}
@@ -136,33 +129,31 @@ function.
-Compiling this code will result in this error:
+کامپایل کردن این کد به این خطا منجر میشود:
```console
{{#include ../listings/ch08-common-collections/listing-08-06/output.txt}}
```
-The code in Listing 8-6 might look like it should work: why should a reference
-to the first element care about changes at the end of the vector? This error is
-due to the way vectors work: because vectors put the values next to each other
-in memory, adding a new element onto the end of the vector might require
-allocating new memory and copying the old elements to the new space, if there
-isn’t enough room to put all the elements next to each other where the vector
-is currently stored. In that case, the reference to the first element would be
-pointing to deallocated memory. The borrowing rules prevent programs from
-ending up in that situation.
+کد در لیست ۸-۶ ممکن است به نظر بیاید که باید کار کند: چرا یک مرجع به اولین عنصر باید
+به تغییرات انتهای بردار اهمیت دهد؟ این خطا به نحوه کار بردارها مربوط است: چون بردارها مقادیر
+را در کنار یکدیگر در حافظه قرار میدهند، اضافه کردن یک عنصر جدید به انتهای بردار ممکن است نیازمند
+اختصاص حافظه جدید و کپی کردن عناصر قدیمی به مکان جدید باشد، اگر فضای کافی برای قرار دادن
+همه عناصر در کنار یکدیگر در محل کنونی بردار وجود نداشته باشد. در این حالت، مرجع به اولین
+عنصر به حافظهای اشاره میکند که آزاد شده است. قوانین قرضگیری از به وجود آمدن این شرایط
+در برنامهها جلوگیری میکنند.
-> Note: For more on the implementation details of the `Vec` type, see [“The
-> Rustonomicon”][nomicon].
+> نکته: برای اطلاعات بیشتر درباره جزئیات پیادهسازی نوع `Vec`، به [“The Rustonomicon”][nomicon] مراجعه کنید.
-### Iterating Over the Values in a Vector
-To access each element in a vector in turn, we would iterate through all of the
-elements rather than use indices to access one at a time. Listing 8-7 shows how
-to use a `for` loop to get immutable references to each element in a vector of
-`i32` values and print them.
+### پیمایش بر روی مقادیر در یک بردار
-
+برای دسترسی به هر عنصر در یک بردار به ترتیب، میتوانیم به جای استفاده از اندیسها
+برای دسترسی به یک عنصر در هر بار، بر روی تمامی عناصر پیمایش کنیم. لیست ۸-۷ نشان میدهد
+چگونه میتوان از یک حلقه `for` برای گرفتن مرجعهای غیرقابل تغییر به هر عنصر در یک بردار
+از مقادیر `i32` استفاده کرد و آنها را چاپ کرد.
+
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
@@ -170,11 +161,10 @@ to use a `for` loop to get immutable references to each element in a vector of
-We can also iterate over mutable references to each element in a mutable vector
-in order to make changes to all the elements. The `for` loop in Listing 8-8
-will add `50` to each element.
+همچنین میتوانیم بر روی مرجعهای قابل تغییر به هر عنصر در یک بردار قابل تغییر پیمایش کنیم
+تا تغییراتی روی تمام عناصر اعمال کنیم. حلقه `for` در لیست ۸-۸ مقدار `50` را به هر عنصر اضافه میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}}
@@ -182,34 +172,36 @@ will add `50` to each element.
-To change the value that the mutable reference refers to, we have to use the
-`*` dereference operator to get to the value in `i` before we can use the `+=`
-operator. We’ll talk more about the dereference operator in the [“Following the
-Reference to the Value”][deref] section of Chapter 15.
+برای تغییر مقداری که رفرنس قابلتغییر به آن اشاره میکند،
+باید از عملگر `*` برای dereference کردن استفاده کنیم تا به مقدار درون `i` دسترسی پیدا کنیم
+و سپس بتوانیم از عملگر `+=` استفاده کنیم.
+در بخش [“دنبال کردن رفرنس تا رسیدن به مقدار”][deref] در فصل ۱۵،
+بیشتر دربارهی عملگر dereference صحبت خواهیم کرد.
+
+پیمایش بر روی یک بردار، چه به صورت غیرقابل تغییر و چه به صورت قابل تغییر، امن است
+زیرا از قوانین بررسیکننده قرض پیروی میکند. اگر بخواهیم در بدنه حلقههای `for` در لیست ۸-۷
+و لیست ۸-۸ آیتمها را درج یا حذف کنیم، با خطای کامپایل مشابهی با کدی که در لیست ۸-۶ دیدیم
+روبرو خواهیم شد. مرجع به برداری که حلقه `for` نگه میدارد از تغییر همزمان کل بردار
+جلوگیری میکند.
-Iterating over a vector, whether immutably or mutably, is safe because of the
-borrow checker’s rules. If we attempted to insert or remove items in the `for`
-loop bodies in Listing 8-7 and Listing 8-8, we would get a compiler error
-similar to the one we got with the code in Listing 8-6. The reference to the
-vector that the `for` loop holds prevents simultaneous modification of the
-whole vector.
-### Using an Enum to Store Multiple Types
+### استفاده از Enum برای ذخیره انواع مختلف
-Vectors can only store values that are of the same type. This can be
-inconvenient; there are definitely use cases for needing to store a list of
-items of different types. Fortunately, the variants of an enum are defined
-under the same enum type, so when we need one type to represent elements of
-different types, we can define and use an enum!
+بردارها فقط میتوانند مقادیر از یک نوع را ذخیره کنند. این موضوع ممکن است گاهی
+ناخوشایند باشد؛ مطمئناً موارد استفادهای وجود دارند که نیاز به ذخیره یک لیست
+از آیتمها با انواع مختلف دارید. خوشبختانه، متغیرهای یک `enum` تحت یک نوع
+`enum` تعریف شدهاند، بنابراین وقتی نیاز به یک نوع برای نمایش عناصر از انواع
+مختلف دارید، میتوانید یک `enum` تعریف کرده و از آن استفاده کنید!
-For example, say we want to get values from a row in a spreadsheet in which
-some of the columns in the row contain integers, some floating-point numbers,
-and some strings. We can define an enum whose variants will hold the different
-value types, and all the enum variants will be considered the same type: that
-of the enum. Then we can create a vector to hold that enum and so, ultimately,
-hold different types. We’ve demonstrated this in Listing 8-9.
+برای مثال، فرض کنید میخواهیم مقادیر یک ردیف از یک صفحه گسترده را که برخی از
+ستونهای آن شامل اعداد صحیح، برخی شامل اعداد اعشاری و برخی شامل رشتهها
+میباشند، دریافت کنیم. میتوانیم یک `enum` تعریف کنیم که متغیرهای آن انواع
+مختلف مقادیر را نگهداری کنند، و تمام متغیرهای `enum` به عنوان یک نوع مشابه
+(یعنی نوع `enum`) در نظر گرفته میشوند. سپس میتوانیم یک بردار ایجاد کنیم
+که این `enum` را نگهداری کند و در نتیجه انواع مختلف را ذخیره کند. این موضوع
+در لیست ۸-۹ نمایش داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}}
@@ -217,29 +209,31 @@ hold different types. We’ve demonstrated this in Listing 8-9.
-Rust needs to know what types will be in the vector at compile time so it knows
-exactly how much memory on the heap will be needed to store each element. We
-must also be explicit about what types are allowed in this vector. If Rust
-allowed a vector to hold any type, there would be a chance that one or more of
-the types would cause errors with the operations performed on the elements of
-the vector. Using an enum plus a `match` expression means that Rust will ensure
-at compile time that every possible case is handled, as discussed in Chapter 6.
+Rust باید بداند چه انواعی در بردار خواهند بود تا بتواند در زمان کامپایل
+دقیقاً مشخص کند چه مقدار حافظه در heap برای ذخیره هر عنصر نیاز است.
+همچنین باید به طور صریح مشخص کنیم که چه انواعی در این بردار مجاز هستند.
+اگر Rust اجازه میداد که بردار هر نوعی را نگهداری کند، احتمال داشت که
+یک یا چند نوع باعث ایجاد خطا در عملیات انجام شده روی عناصر بردار شوند.
+استفاده از یک `enum` به علاوه یک عبارت `match` به این معنی است که Rust
+در زمان کامپایل اطمینان حاصل خواهد کرد که تمام حالتهای ممکن مدیریت شدهاند،
+همانطور که در فصل ۶ بحث شد.
-If you don’t know the exhaustive set of types a program will get at runtime to
-store in a vector, the enum technique won’t work. Instead, you can use a trait
-object, which we’ll cover in Chapter 18.
+اگر مجموعه جامعی از انواعی که برنامه در زمان اجرا دریافت میکند و باید
+در بردار ذخیره شود را نمیدانید، تکنیک `enum` کار نخواهد کرد. به جای آن،
+میتوانید از یک شیء ویژگی (`trait object`) استفاده کنید که در فصل ۱۸
+مورد بررسی قرار خواهد گرفت.
-Now that we’ve discussed some of the most common ways to use vectors, be sure
-to review [the API documentation][vec-api] for all of the many
-useful methods defined on `Vec` by the standard library. For example, in
-addition to `push`, a `pop` method removes and returns the last element.
+اکنون که برخی از رایجترین روشهای استفاده از بردارها را بحث کردیم، مطمئن شوید
+که [مستندات API][vec-api] را برای تمام متدهای مفیدی که کتابخانه
+استاندارد روی `Vec` تعریف کرده است مرور کنید. برای مثال، علاوه بر `push`،
+متد `pop` عنصر آخر را حذف کرده و بازمیگرداند.
-### Dropping a Vector Drops Its Elements
+### حذف یک بردار، عناصر آن را نیز حذف میکند
-Like any other `struct`, a vector is freed when it goes out of scope, as
-annotated in Listing 8-10.
+مانند هر `struct` دیگری، یک بردار وقتی از محدوده خارج میشود آزاد میشود،
+همانطور که در لیست ۸-۱۰ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}}
@@ -247,14 +241,13 @@ annotated in Listing 8-10.
-When the vector gets dropped, all of its contents are also dropped, meaning the
-integers it holds will be cleaned up. The borrow checker ensures that any
-references to contents of a vector are only used while the vector itself is
-valid.
+وقتی بردار حذف میشود، تمام محتوای آن نیز حذف میشوند، به این معنی که اعداد
+صحیحی که نگهداری میکند تمیزکاری میشوند. بررسیکننده قرض اطمینان حاصل میکند
+که هر مرجع به محتوای یک بردار فقط تا زمانی که خود بردار معتبر است استفاده شود.
-Let’s move on to the next collection type: `String`!
+حال به نوع مجموعه بعدی میپردازیم: `String`!
[data-types]: ch03-02-data-types.html#data-types
[nomicon]: ../nomicon/vec/vec.html
-[vec-api]: ../std/vec/struct.Vec.html
+[vec-api]: https://doc.rust-lang.org/std/vec/struct.Vec.html
[deref]: ch15-02-deref.html#following-the-pointer-to-the-value-with-the-dereference-operator
diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md
index 6df7dd83cb..269fbeca51 100644
--- a/src/ch08-02-strings.md
+++ b/src/ch08-02-strings.md
@@ -1,47 +1,43 @@
-## Storing UTF-8 Encoded Text with Strings
-
-We talked about strings in Chapter 4, but we’ll look at them in more depth now.
-New Rustaceans commonly get stuck on strings for a combination of three
-reasons: Rust’s propensity for exposing possible errors, strings being a more
-complicated data structure than many programmers give them credit for, and
-UTF-8. These factors combine in a way that can seem difficult when you’re
-coming from other programming languages.
-
-We discuss strings in the context of collections because strings are
-implemented as a collection of bytes, plus some methods to provide useful
-functionality when those bytes are interpreted as text. In this section, we’ll
-talk about the operations on `String` that every collection type has, such as
-creating, updating, and reading. We’ll also discuss the ways in which `String`
-is different from the other collections, namely how indexing into a `String` is
-complicated by the differences between how people and computers interpret
-`String` data.
-
-### What Is a String?
-
-We’ll first define what we mean by the term _string_. Rust has only one string
-type in the core language, which is the string slice `str` that is usually seen
-in its borrowed form `&str`. In Chapter 4, we talked about _string slices_,
-which are references to some UTF-8 encoded string data stored elsewhere. String
-literals, for example, are stored in the program’s binary and are therefore
-string slices.
-
-The `String` type, which is provided by Rust’s standard library rather than
-coded into the core language, is a growable, mutable, owned, UTF-8 encoded
-string type. When Rustaceans refer to “strings” in Rust, they might be
-referring to either the `String` or the string slice `&str` types, not just one
-of those types. Although this section is largely about `String`, both types are
-used heavily in Rust’s standard library, and both `String` and string slices
-are UTF-8 encoded.
-
-### Creating a New String
-
-Many of the same operations available with `Vec` are available with `String`
-as well because `String` is actually implemented as a wrapper around a vector
-of bytes with some extra guarantees, restrictions, and capabilities. An example
-of a function that works the same way with `Vec` and `String` is the `new`
-function to create an instance, shown in Listing 8-11.
-
-
+## ذخیره متنهای کدگذاری شده UTF-8 با رشتهها (strings)
+
+ما در فصل ۴ درباره رشتهها صحبت کردیم، اما اکنون به آنها با عمق بیشتری نگاه خواهیم کرد.
+Rustaceanهای تازهوارد معمولاً به دلیل ترکیبی از سه عامل در رشتهها دچار مشکل میشوند:
+گرایش Rust به آشکارسازی خطاهای ممکن، رشتهها به عنوان یک ساختار داده پیچیدهتر از آنچه
+بسیاری از برنامهنویسان تصور میکنند، و UTF-8. این عوامل به نحوی ترکیب میشوند که میتوانند
+برای کسانی که از زبانهای برنامهنویسی دیگر میآیند دشوار باشند.
+
+ما رشتهها را در زمینه مجموعهها بررسی میکنیم، زیرا رشتهها به عنوان مجموعهای از بایتها
+پیادهسازی شدهاند، به علاوه تعدادی متد برای ارائه قابلیتهای مفید زمانی که این بایتها
+به عنوان متن تفسیر میشوند. در این بخش، درباره عملیاتهایی که روی `String` انجام میشود
+و هر نوع مجموعهای آنها را دارد، مانند ایجاد، بهروزرسانی، و خواندن صحبت خواهیم کرد.
+همچنین تفاوتهای `String` با سایر مجموعهها را مورد بحث قرار میدهیم، بهویژه نحوه پیچیدگی
+اندیسگذاری در یک `String` به دلیل تفاوتهای بین تفسیر دادههای `String` توسط انسانها
+و کامپیوترها.
+
+### رشته (string) چیست؟
+
+ابتدا تعریف میکنیم که منظور ما از اصطلاح _رشته_ چیست. Rust فقط یک نوع رشته در زبان
+هسته خود دارد که همان قطعه رشته `str` است که معمولاً به صورت قرض گرفته شده `&str`
+دیده میشود. در فصل ۴ درباره _قطعههای رشته_ صحبت کردیم، که ارجاعاتی به دادههای رشتهای
+کدگذاری شده UTF-8 هستند که در جای دیگری ذخیره شدهاند. به عنوان مثال، رشتههای
+لیترال در باینری برنامه ذخیره میشوند و بنابراین قطعههای رشته هستند.
+
+نوع `String`، که توسط کتابخانه استاندارد Rust ارائه شده است و نه مستقیماً در زبان هسته
+کدگذاری شده، یک نوع رشته رشدپذیر، قابل تغییر، و مالک UTF-8 است. وقتی Rustaceanها
+به "رشتهها" در Rust اشاره میکنند، ممکن است به نوع `String` یا قطعه رشته `&str` اشاره
+کنند، نه فقط یکی از این دو نوع. اگرچه این بخش عمدتاً درباره `String` است، اما هر دو نوع
+در کتابخانه استاندارد Rust به شدت مورد استفاده قرار میگیرند و هر دو `String` و قطعههای
+رشته کدگذاری UTF-8 دارند.
+
+### ایجاد یک رشته (strings) جدید
+
+بسیاری از عملیات مشابه موجود در `Vec` برای `String` نیز در دسترس است، زیرا `String`
+در واقع به عنوان یک پوششی بر روی یک بردار از بایتها پیادهسازی شده است، با برخی
+ضمانتها، محدودیتها، و قابلیتهای اضافی. مثالی از یک تابع که به همان روش با
+`Vec` و `String` کار میکند، تابع `new` برای ایجاد یک نمونه است، همانطور که در لیست
+۸-۱۱ نشان داده شده است.
+
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-11/src/main.rs:here}}
@@ -49,13 +45,12 @@ function to create an instance, shown in Listing 8-11.
-This line creates a new, empty string called `s`, into which we can then load
-data. Often, we’ll have some initial data with which we want to start the
-string. For that, we use the `to_string` method, which is available on any type
-that implements the `Display` trait, as string literals do. Listing 8-12 shows
-two examples.
+این خط یک رشته جدید و خالی به نام `s` ایجاد میکند که میتوانیم دادهها را در آن بارگذاری کنیم.
+اغلب، دادههای اولیهای خواهیم داشت که میخواهیم رشته را با آنها شروع کنیم. برای این کار،
+از متد `to_string` استفاده میکنیم که بر روی هر نوعی که ویژگی `Display` را پیادهسازی
+میکند، همانند رشتههای لیترال، در دسترس است. لیست ۸-۱۲ دو مثال را نشان میدهد.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}}
@@ -63,13 +58,12 @@ two examples.
-This code creates a string containing `initial contents`.
+این کد یک رشته حاوی `initial contents` ایجاد میکند.
-We can also use the function `String::from` to create a `String` from a string
-literal. The code in Listing 8-13 is equivalent to the code in Listing 8-12
-that uses `to_string`.
+ما همچنین میتوانیم از تابع `String::from` برای ایجاد یک `String` از یک رشته لیترال
+استفاده کنیم. کد در لیست ۸-۱۳ معادل کدی است که در لیست ۸-۱۲ از `to_string` استفاده میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}}
@@ -77,16 +71,16 @@ that uses `to_string`.
-Because strings are used for so many things, we can use many different generic
-APIs for strings, providing us with a lot of options. Some of them can seem
-redundant, but they all have their place! In this case, `String::from` and
-`to_string` do the same thing, so which one you choose is a matter of style and
-readability.
+از آنجا که رشتهها برای موارد بسیاری استفاده میشوند، میتوانیم از بسیاری از APIهای
+جنریک مختلف برای رشتهها استفاده کنیم که گزینههای زیادی را در اختیار ما قرار میدهند.
+برخی از اینها ممکن است به نظر اضافی بیایند، اما هرکدام جایگاه خاص خود را دارند!
+در این مورد، `String::from` و `to_string` عملکرد یکسانی دارند، بنابراین انتخاب بین آنها
+مسئله سبک و خوانایی کد است.
-Remember that strings are UTF-8 encoded, so we can include any properly encoded
-data in them, as shown in Listing 8-14.
+به یاد داشته باشید که رشتهها با کدگذاری UTF-8 هستند، بنابراین میتوانیم هر دادهای که
+به طور صحیح کدگذاری شده باشد را در آنها قرار دهیم، همانطور که در لیست ۸-۱۴ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}}
@@ -94,20 +88,20 @@ data in them, as shown in Listing 8-14.
-All of these are valid `String` values.
+تمام این موارد مقادیر معتبر `String` هستند.
-### Updating a String
+### بهروزرسانی یک رشته
-A `String` can grow in size and its contents can change, just like the contents
-of a `Vec`, if you push more data into it. In addition, you can conveniently
-use the `+` operator or the `format!` macro to concatenate `String` values.
+یک `String` میتواند از نظر اندازه رشد کند و محتوای آن تغییر کند، همانطور که محتوای
+یک `Vec` تغییر میکند، اگر داده بیشتری به آن اضافه کنیم. علاوه بر این، میتوانیم به راحتی
+از عملگر `+` یا ماکروی `format!` برای الحاق مقادیر `String` استفاده کنیم.
-#### Appending to a String with `push_str` and `push`
+#### الحاق به یک رشته (string) با `push_str` و `push`
-We can grow a `String` by using the `push_str` method to append a string slice,
-as shown in Listing 8-15.
+ما میتوانیم یک `String` را با استفاده از متد `push_str` برای الحاق یک قطعه رشته رشد دهیم،
+همانطور که در لیست ۸-۱۵ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}}
@@ -115,12 +109,11 @@ as shown in Listing 8-15.
-After these two lines, `s` will contain `foobar`. The `push_str` method takes a
-string slice because we don’t necessarily want to take ownership of the
-parameter. For example, in the code in Listing 8-16, we want to be able to use
-`s2` after appending its contents to `s1`.
+بعد از این دو خط، مقدار `s` شامل `foobar` خواهد بود. متد `push_str` یک قطعه رشته را به عنوان آرگومان میگیرد
+زیرا ما لزوماً نمیخواهیم مالکیت پارامتر را بگیریم. برای مثال، در کدی که در لیست ۸-۱۶ نشان داده شده است،
+ما میخواهیم بتوانیم پس از الحاق محتوای `s2` به `s1` همچنان از `s2` استفاده کنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}}
@@ -128,14 +121,13 @@ parameter. For example, in the code in Listing 8-16, we want to be able to use
-If the `push_str` method took ownership of `s2`, we wouldn’t be able to print
-its value on the last line. However, this code works as we’d expect!
+اگر متد `push_str` مالکیت `s2` را میگرفت، نمیتوانستیم مقدار آن را در خط آخر چاپ کنیم. با این حال،
+این کد همانطور که انتظار میرود کار میکند!
-The `push` method takes a single character as a parameter and adds it to the
-`String`. Listing 8-17 adds the letter _l_ to a `String` using the `push`
-method.
+متد `push` یک کاراکتر را به عنوان پارامتر میگیرد و آن را به `String` اضافه میکند. لیست ۸-۱۷
+حرف _l_ را با استفاده از متد `push` به یک `String` اضافه میکند.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}}
@@ -143,14 +135,14 @@ method.
-As a result, `s` will contain `lol`.
+در نتیجه، مقدار `s` شامل `lol` خواهد بود.
-#### Concatenation with the `+` Operator or the `format!` Macro
+#### الحاق با استفاده از عملگر `+` یا ماکروی `format!`
-Often, you’ll want to combine two existing strings. One way to do so is to use
-the `+` operator, as shown in Listing 8-18.
+اغلب، ممکن است بخواهید دو رشته موجود را با هم ترکیب کنید. یکی از راههای انجام این کار
+استفاده از عملگر `+` است، همانطور که در لیست ۸-۱۸ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}}
@@ -158,73 +150,64 @@ the `+` operator, as shown in Listing 8-18.
-The string `s3` will contain `Hello, world!`. The reason `s1` is no longer
-valid after the addition, and the reason we used a reference to `s2`, has to do
-with the signature of the method that’s called when we use the `+` operator.
-The `+` operator uses the `add` method, whose signature looks something like
-this:
+مقدار `s3` شامل `Hello, world!` خواهد بود. دلیل اینکه `s1` پس از این الحاق دیگر معتبر نیست
+و دلیل اینکه ما از یک مرجع به `s2` استفاده کردیم، به امضای متدی که هنگام استفاده از
+عملگر `+` فراخوانی میشود مربوط است. عملگر `+` از متد `add` استفاده میکند که امضای آن به شکل زیر است:
```rust,ignore
fn add(self, s: &str) -> String {
```
-In the standard library, you’ll see `add` defined using generics and associated
-types. Here, we’ve substituted in concrete types, which is what happens when we
-call this method with `String` values. We’ll discuss generics in Chapter 10.
-This signature gives us the clues we need in order to understand the tricky
-bits of the `+` operator.
-
-First, `s2` has an `&`, meaning that we’re adding a _reference_ of the second
-string to the first string. This is because of the `s` parameter in the `add`
-function: we can only add a `&str` to a `String`; we can’t add two `String`
-values together. But wait—the type of `&s2` is `&String`, not `&str`, as
-specified in the second parameter to `add`. So why does Listing 8-18 compile?
-
-The reason we’re able to use `&s2` in the call to `add` is that the compiler
-can _coerce_ the `&String` argument into a `&str`. When we call the `add`
-method, Rust uses a _deref coercion_, which here turns `&s2` into `&s2[..]`.
-We’ll discuss deref coercion in more depth in Chapter 15. Because `add` does
-not take ownership of the `s` parameter, `s2` will still be a valid `String`
-after this operation.
-
-Second, we can see in the signature that `add` takes ownership of `self`
-because `self` does _not_ have an `&`. This means `s1` in Listing 8-18 will be
-moved into the `add` call and will no longer be valid after that. So, although
-`let s3 = s1 + &s2;` looks like it will copy both strings and create a new one,
-this statement actually takes ownership of `s1`, appends a copy of the contents
-of `s2`, and then returns ownership of the result. In other words, it looks
-like it’s making a lot of copies, but it isn’t; the implementation is more
-efficient than copying.
-
-If we need to concatenate multiple strings, the behavior of the `+` operator
-gets unwieldy:
+در کتابخانه استاندارد، شما `add` را خواهید دید که با استفاده از جنریکها و انواع مرتبط تعریف شده است.
+اینجا، ما انواع مشخصی را جایگزین کردهایم، که این همان چیزی است که هنگام فراخوانی این متد با مقادیر
+`String` اتفاق میافتد. درباره جنریکها در فصل ۱۰ صحبت خواهیم کرد. این امضا به ما سرنخهایی میدهد
+تا بتوانیم بخشهای چالشبرانگیز عملگر `+` را درک کنیم.
+
+اول، `s2` یک `&` دارد، به این معنی که ما یک _مرجع_ از رشته دوم را به رشته اول اضافه میکنیم.
+این به دلیل پارامتر `s` در تابع `add` است: ما فقط میتوانیم یک `&str` را به یک `String` اضافه کنیم؛
+نمیتوانیم دو مقدار `String` را با هم جمع کنیم. اما صبر کنید—نوع `&s2`، `&String` است، نه `&str`
+همانطور که در پارامتر دوم `add` مشخص شده است. پس چرا کد در لیست ۸-۱۸ کامپایل میشود؟
+
+دلیل اینکه میتوانیم از `&s2` در فراخوانی `add` استفاده کنیم این است که کامپایلر میتواند آرگومان
+`&String` را به `&str` تبدیل کند. هنگامی که ما متد `add` را فراخوانی میکنیم، Rust از یک
+_coercion deref_ استفاده میکند که در اینجا `&s2` را به `&s2[..]` تبدیل میکند. ما این موضوع
+را در فصل ۱۵ به طور عمیقتری بررسی خواهیم کرد. از آنجا که `add` مالکیت پارامتر `s` را نمیگیرد،
+`s2` پس از این عملیات همچنان یک `String` معتبر باقی خواهد ماند.
+
+دوم، میتوانیم در امضا ببینیم که `add` مالکیت `self` را میگیرد زیرا `self` یک `&` ندارد.
+این بدان معناست که `s1` در لیست ۸-۱۸ به فراخوانی `add` منتقل میشود و پس از آن دیگر معتبر نخواهد بود.
+بنابراین، اگرچه `let s3 = s1 + &s2;` به نظر میرسد که هر دو رشته را کپی میکند و یک رشته جدید ایجاد
+میکند، این عبارت در واقع مالکیت `s1` را میگیرد، یک کپی از محتوای `s2` را اضافه میکند، و سپس مالکیت
+نتیجه را بازمیگرداند. به عبارت دیگر، به نظر میرسد که کپیهای زیادی انجام میدهد، اما اینطور نیست؛
+پیادهسازی کارآمدتر از کپی کردن است.
+
+اگر نیاز به الحاق چندین رشته داشته باشیم، رفتار عملگر `+` دستوپاگیر میشود:
```rust
{{#rustdoc_include ../listings/ch08-common-collections/no-listing-01-concat-multiple-strings/src/main.rs:here}}
```
-At this point, `s` will be `tic-tac-toe`. With all of the `+` and `"`
-characters, it’s difficult to see what’s going on. For combining strings in
-more complicated ways, we can instead use the `format!` macro:
+در این نقطه، مقدار `s` برابر با `tic-tac-toe` خواهد بود. با تمام این `+` و کاراکترهای `"`،
+دیدن اینکه چه اتفاقی میافتد دشوار است. برای ترکیب رشتهها به روشهای پیچیدهتر، میتوانیم
+به جای آن از ماکروی `format!` استفاده کنیم:
```rust
{{#rustdoc_include ../listings/ch08-common-collections/no-listing-02-format/src/main.rs:here}}
```
-This code also sets `s` to `tic-tac-toe`. The `format!` macro works like
-`println!`, but instead of printing the output to the screen, it returns a
-`String` with the contents. The version of the code using `format!` is much
-easier to read, and the code generated by the `format!` macro uses references
-so that this call doesn’t take ownership of any of its parameters.
+این کد نیز مقدار `s` را به `tic-tac-toe` تنظیم میکند. ماکروی `format!` شبیه به `println!` کار میکند،
+اما به جای چاپ خروجی روی صفحه، یک `String` با محتوای مورد نظر بازمیگرداند. نسخه کد با استفاده از
+`format!` بسیار خواناتر است و کدی که توسط ماکروی `format!` تولید میشود از مراجع استفاده میکند،
+بنابراین این فراخوانی مالکیت هیچیک از پارامترهایش را نمیگیرد.
-### Indexing into Strings
+### اندیسگذاری در رشتهها
-In many other programming languages, accessing individual characters in a
-string by referencing them by index is a valid and common operation. However,
-if you try to access parts of a `String` using indexing syntax in Rust, you’ll
-get an error. Consider the invalid code in Listing 8-19.
+در بسیاری از زبانهای برنامهنویسی دیگر، دسترسی به کاراکترهای منفرد در یک رشته با اشاره به آنها
+توسط اندیس (index)یک عملیات معتبر و رایج است. با این حال، اگر تلاش کنید در Rust با استفاده از سینتکس
+اندیسگذاری به بخشهایی از یک `String` دسترسی پیدا کنید، با خطا مواجه میشوید. کد نامعتبر
+در لیست ۸-۱۹ را در نظر بگیرید.
-
+
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}}
@@ -232,109 +215,102 @@ get an error. Consider the invalid code in Listing 8-19.
-This code will result in the following error:
+این کد به خطای زیر منجر خواهد شد:
```console
{{#include ../listings/ch08-common-collections/listing-08-19/output.txt}}
```
-The error and the note tell the story: Rust strings don’t support indexing. But
-why not? To answer that question, we need to discuss how Rust stores strings in
-memory.
+خطا و توضیحات آن گویای موضوع است: رشتههای Rust از اندیسگذاری پشتیبانی نمیکنند. اما چرا؟
+برای پاسخ به این سؤال، باید درباره نحوه ذخیرهسازی رشتهها در حافظه توسط Rust صحبت کنیم.
-#### Internal Representation
+#### نمایش داخلی
-A `String` is a wrapper over a `Vec`. Let’s look at some of our properly
-encoded UTF-8 example strings from Listing 8-14. First, this one:
+یک `String` در واقع یک پوشش بر روی `Vec` است. بیایید به برخی از مثالهای رشتههای کدگذاری
+شده UTF-8 در لیست ۸-۱۴ نگاه کنیم. ابتدا این مورد:
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:spanish}}
```
-In this case, `len` will be `4`, which means the vector storing the string
-`"Hola"` is 4 bytes long. Each of these letters takes one byte when encoded in
-UTF-8. The following line, however, may surprise you (note that this string
-begins with the capital Cyrillic letter _Ze_, not the number 3):
+در این حالت، مقدار `len` برابر با `4` خواهد بود، به این معنی که برداری که رشته `"Hola"` را
+ذخیره میکند ۴ بایت طول دارد. هر یک از این حروف هنگام کدگذاری در UTF-8 یک بایت میگیرد.
+با این حال، خط زیر ممکن است شما را شگفتزده کند (توجه داشته باشید که این رشته با حرف بزرگ
+سیریلیک _Ze_ آغاز میشود، نه عدد ۳):
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:russian}}
```
-If you were asked how long the string is, you might say 12. In fact, Rust’s
-answer is 24: that’s the number of bytes it takes to encode “Здравствуйте” in
-UTF-8, because each Unicode scalar value in that string takes 2 bytes of
-storage. Therefore, an index into the string’s bytes will not always correlate
-to a valid Unicode scalar value. To demonstrate, consider this invalid Rust
-code:
+اگر از شما پرسیده شود طول این رشته چقدر است، ممکن است بگویید ۱۲. اما در واقع، پاسخ Rust
+۲۴ است: این تعداد بایتهایی است که برای کدگذاری “Здравствуйте” در UTF-8 نیاز است، زیرا هر مقدار
+اسکالر Unicode در این رشته ۲ بایت فضای ذخیرهسازی میگیرد. بنابراین، یک اندیس (index)در بایتهای رشته
+همیشه با یک مقدار اسکالر Unicode معتبر مطابقت ندارد. برای نشان دادن این موضوع، کد نامعتبر
+زیر در Rust را در نظر بگیرید:
```rust,ignore,does_not_compile
let hello = "Здравствуйте";
let answer = &hello[0];
```
-You already know that `answer` will not be `З`, the first letter. When encoded
-in UTF-8, the first byte of `З` is `208` and the second is `151`, so it would
-seem that `answer` should in fact be `208`, but `208` is not a valid character
-on its own. Returning `208` is likely not what a user would want if they asked
-for the first letter of this string; however, that’s the only data that Rust
-has at byte index 0. Users generally don’t want the byte value returned, even
-if the string contains only Latin letters: if `&"hi"[0]` were valid code that
-returned the byte value, it would return `104`, not `h`.
+شما قبلاً میدانید که مقدار `answer` برابر با `З`، اولین حرف، نخواهد بود. وقتی در UTF-8 کدگذاری
+میشود، اولین بایت از `З` برابر با `208` و دومین بایت برابر با `151` است، بنابراین ممکن است به نظر
+برسد که `answer` باید در واقع `208` باشد، اما `208` به تنهایی یک کاراکتر معتبر نیست. بازگرداندن
+`208` احتمالاً چیزی نیست که یک کاربر بخواهد اگر درخواست اولین حرف این رشته را داشته باشد؛
+با این حال، این تنها دادهای است که Rust در اندیس (index)بایت ۰ دارد. کاربران به طور کلی نمیخواهند
+مقدار بایت بازگردانده شود، حتی اگر رشته فقط حروف لاتین داشته باشد: اگر `&"hi"[0]` یک کد معتبر
+بود که مقدار بایت را بازمیگرداند، مقدار `104` و نه `h` را بازمیگرداند.
-The answer, then, is that to avoid returning an unexpected value and causing
-bugs that might not be discovered immediately, Rust doesn’t compile this code
-at all and prevents misunderstandings early in the development process.
+پاسخ این است که برای جلوگیری از بازگرداندن یک مقدار غیرمنتظره و ایجاد باگهایی که ممکن است فوراً
+کشف نشوند، Rust این کد را اصلاً کامپایل نمیکند و از سوءتفاهمها در اوایل فرآیند توسعه جلوگیری میکند.
-#### Bytes and Scalar Values and Grapheme Clusters! Oh My!
+#### بایتها، مقادیر اسکالر و خوشههای گرافیمی! اوه خدای من!
-Another point about UTF-8 is that there are actually three relevant ways to
-look at strings from Rust’s perspective: as bytes, scalar values, and grapheme
-clusters (the closest thing to what we would call _letters_).
+نکته دیگری درباره UTF-8 این است که در واقع سه روش مرتبط برای مشاهده رشتهها از دیدگاه Rust وجود دارد:
+به صورت بایت، مقادیر اسکالر، و خوشههای گرافیمی (نزدیکترین چیز به چیزی که ما _حروف_ مینامیم).
-If we look at the Hindi word “नमस्ते” written in the Devanagari script, it is
-stored as a vector of `u8` values that looks like this:
+اگر به کلمه هندی "नमस्ते" نوشته شده در اسکریپت Devanagari نگاه کنیم، این کلمه به صورت یک بردار
+از مقادیر `u8` ذخیره میشود که به شکل زیر است:
```text
[224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224, 164, 164,
224, 165, 135]
```
-That’s 18 bytes and is how computers ultimately store this data. If we look at
-them as Unicode scalar values, which are what Rust’s `char` type is, those
-bytes look like this:
+این ۱۸ بایت است و این همان چیزی است که کامپیوترها در نهایت این داده را ذخیره میکنند.
+اگر به آنها به عنوان مقادیر اسکالر Unicode نگاه کنیم، که همان نوع `char` در Rust است، این بایتها
+به این صورت به نظر میرسند:
```text
['न', 'म', 'स', '्', 'त', 'े']
```
-There are six `char` values here, but the fourth and sixth are not letters:
-they’re diacritics that don’t make sense on their own. Finally, if we look at
-them as grapheme clusters, we’d get what a person would call the four letters
-that make up the Hindi word:
+اینجا شش مقدار `char` وجود دارد، اما مقدار چهارم و ششم حروف نیستند: اینها دیاکریتیکهایی هستند که
+به تنهایی معنایی ندارند. در نهایت، اگر به آنها به عنوان خوشههای گرافیمی نگاه کنیم، همان چیزی
+که یک فرد به عنوان حروف کلمه هندی تشخیص میدهد، اینطور خواهد بود:
```text
["न", "म", "स्", "ते"]
```
-Rust provides different ways of interpreting the raw string data that computers
-store so that each program can choose the interpretation it needs, no matter
-what human language the data is in.
+Rust روشهای مختلفی برای تفسیر داده خام رشته ارائه میدهد که کامپیوترها ذخیره میکنند، بنابراین هر
+برنامه میتواند تفسیری را که نیاز دارد انتخاب کند، صرف نظر از اینکه داده به چه زبان انسانی است.
+
+یکی دیگر از دلایل اینکه Rust به ما اجازه نمیدهد در یک `String` اندیسگذاری کنیم تا یک کاراکتر را
+دریافت کنیم این است که عملیات اندیسگذاری باید همیشه در زمان ثابت (O(1)) انجام شود. اما امکان
+تضمین این عملکرد با یک `String` وجود ندارد، زیرا Rust باید محتویات را از ابتدا تا اندیس (index)مرور کند تا
+تعیین کند که چند کاراکتر معتبر وجود دارد.
-A final reason Rust doesn’t allow us to index into a `String` to get a
-character is that indexing operations are expected to always take constant time
-(O(1)). But it isn’t possible to guarantee that performance with a `String`,
-because Rust would have to walk through the contents from the beginning to the
-index to determine how many valid characters there were.
-### Slicing Strings
+### برش رشتهها
-Indexing into a string is often a bad idea because it’s not clear what the
-return type of the string-indexing operation should be: a byte value, a
-character, a grapheme cluster, or a string slice. If you really need to use
-indices to create string slices, therefore, Rust asks you to be more specific.
+اندیسگذاری در یک رشته اغلب ایده خوبی نیست زیرا مشخص نیست که نوع بازگشتی عملیات اندیسگذاری
+رشته چه باید باشد: یک مقدار بایت، یک کاراکتر، یک خوشه گرافیمی، یا یک قطعه رشته. بنابراین،
+اگر واقعاً نیاز به استفاده از اندیسها برای ایجاد قطعههای رشته دارید، Rust از شما میخواهد
+بیشتر مشخص کنید.
-Rather than indexing using `[]` with a single number, you can use `[]` with a
-range to create a string slice containing particular bytes:
+به جای اندیسگذاری با استفاده از `[]` و یک عدد، میتوانید از `[]` با یک بازه استفاده کنید
+تا یک قطعه رشته که شامل بایتهای خاصی است ایجاد کنید:
```rust
let hello = "Здравствуйте";
@@ -342,27 +318,26 @@ let hello = "Здравствуйте";
let s = &hello[0..4];
```
-Here, `s` will be a `&str` that contains the first four bytes of the string.
-Earlier, we mentioned that each of these characters was two bytes, which means
-`s` will be `Зд`.
+اینجا، `s` یک `&str` خواهد بود که شامل چهار بایت اول رشته است. پیشتر اشاره کردیم که هر
+یک از این کاراکترها دو بایت طول دارند، که به این معنی است که مقدار `s` برابر با `Зд` خواهد بود.
-If we were to try to slice only part of a character’s bytes with something like
-`&hello[0..1]`, Rust would panic at runtime in the same way as if an invalid
-index were accessed in a vector:
+اگر سعی کنیم فقط بخشی از بایتهای یک کاراکتر را با چیزی مثل `&hello[0..1]` برش دهیم،
+Rust در زمان اجرا دچار خطا میشود، به همان شکلی که اگر یک اندیس (index)نامعتبر در یک بردار
+دسترسی داده شود:
```console
{{#include ../listings/ch08-common-collections/output-only-01-not-char-boundary/output.txt}}
```
-You should use caution when creating string slices with ranges, because doing
-so can crash your program.
+هنگام ایجاد قطعههای رشته با بازهها باید احتیاط کنید، زیرا این کار ممکن است باعث خرابی
+برنامه شما شود.
-### Methods for Iterating Over Strings
+### متدهایی برای پیمایش در رشتهها
-The best way to operate on pieces of strings is to be explicit about whether
-you want characters or bytes. For individual Unicode scalar values, use the
-`chars` method. Calling `chars` on “Зд” separates out and returns two values of
-type `char`, and you can iterate over the result to access each element:
+بهترین راه برای کار با بخشهایی از رشتهها این است که به وضوح مشخص کنید که آیا میخواهید
+روی کاراکترها یا بایتها کار کنید. برای مقادیر اسکالر Unicode منفرد، از متد `chars` استفاده کنید.
+فراخوانی `chars` روی `"Зд"` دو مقدار از نوع `char` را جدا کرده و بازمیگرداند، و میتوانید
+با استفاده از نتیجه پیمایش کنید تا به هر عنصر دسترسی پیدا کنید:
```rust
for c in "Зд".chars() {
@@ -370,15 +345,14 @@ for c in "Зд".chars() {
}
```
-This code will print the following:
+این کد خروجی زیر را چاپ خواهد کرد:
```text
З
д
```
-Alternatively, the `bytes` method returns each raw byte, which might be
-appropriate for your domain:
+به صورت جایگزین، متد `bytes` هر بایت خام را بازمیگرداند که ممکن است برای حوزه کاری شما مناسب باشد:
```rust
for b in "Зд".bytes() {
@@ -386,7 +360,7 @@ for b in "Зд".bytes() {
}
```
-This code will print the four bytes that make up this string:
+این کد چهار بایتی که این رشته را تشکیل میدهند چاپ خواهد کرد:
```text
208
@@ -395,29 +369,24 @@ This code will print the four bytes that make up this string:
180
```
-But be sure to remember that valid Unicode scalar values may be made up of more
-than one byte.
+اما حتماً به یاد داشته باشید که مقادیر اسکالر Unicode معتبر ممکن است از بیش از یک بایت تشکیل شده باشند.
-Getting grapheme clusters from strings, as with the Devanagari script, is
-complex, so this functionality is not provided by the standard library. Crates
-are available on [crates.io](https://crates.io/) if this is the
-functionality you need.
+دریافت خوشههای گرافیمی از رشتهها، همانند اسکریپت Devanagari، پیچیده است، بنابراین این قابلیت
+توسط کتابخانه استاندارد ارائه نمیشود. اگر به این قابلیت نیاز دارید، کرایتهایی در
+[crates.io](https://crates.io/) موجود هستند.
-### Strings Are Not So Simple
+### رشتهها اینقدر ساده نیستند
-To summarize, strings are complicated. Different programming languages make
-different choices about how to present this complexity to the programmer. Rust
-has chosen to make the correct handling of `String` data the default behavior
-for all Rust programs, which means programmers have to put more thought into
-handling UTF-8 data up front. This trade-off exposes more of the complexity of
-strings than is apparent in other programming languages, but it prevents you
-from having to handle errors involving non-ASCII characters later in your
-development life cycle.
+به طور خلاصه، رشتهها پیچیده هستند. زبانهای برنامهنویسی مختلف انتخابهای متفاوتی درباره نحوه
+نمایش این پیچیدگی به برنامهنویس میکنند. Rust انتخاب کرده است که مدیریت صحیح دادههای
+`String` رفتار پیشفرض برای تمام برنامههای Rust باشد، که به این معنی است که برنامهنویسان
+باید در ابتدا بیشتر درباره مدیریت دادههای UTF-8 فکر کنند. این معامله پیچیدگی بیشتری از رشتهها
+را نسبت به سایر زبانهای برنامهنویسی نشان میدهد، اما از مواجهه با خطاهای مربوط به کاراکترهای
+غیر-ASCII در مراحل بعدی چرخه توسعه جلوگیری میکند.
-The good news is that the standard library offers a lot of functionality built
-off the `String` and `&str` types to help handle these complex situations
-correctly. Be sure to check out the documentation for useful methods like
-`contains` for searching in a string and `replace` for substituting parts of a
-string with another string.
+خبر خوب این است که کتابخانه استاندارد عملکردهای زیادی را بر اساس انواع `String` و `&str`
+برای کمک به مدیریت صحیح این شرایط پیچیده ارائه میدهد. حتماً مستندات را برای متدهای مفیدی مانند
+`contains` برای جستجو در یک رشته و `replace` برای جایگزینی بخشهایی از یک رشته با رشتهای دیگر
+بررسی کنید.
-Let’s switch to something a bit less complex: hash maps!
+بیایید به چیزی کمی کمتر پیچیده برویم: هش مپها!
diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md
index 4d185c84c9..e9ae211859 100644
--- a/src/ch08-03-hash-maps.md
+++ b/src/ch08-03-hash-maps.md
@@ -1,30 +1,27 @@
-## Storing Keys with Associated Values in Hash Maps
+## ذخیره کلیدها با مقادیر مرتبط در هش مپها
-The last of our common collections is the _hash map_. The type `HashMap`
-stores a mapping of keys of type `K` to values of type `V` using a _hashing
-function_, which determines how it places these keys and values into memory.
-Many programming languages support this kind of data structure, but they often
-use a different name, such as _hash_, _map_, _object_, _hash table_,
-_dictionary_, or _associative array_, just to name a few.
+آخرین مورد از مجموعههای رایج ما، _هش مپ_ است. نوع `HashMap` یک نگاشت از کلیدهایی
+با نوع `K` به مقادیری با نوع `V` را با استفاده از یک _تابع هش_ ذخیره میکند، که تعیین میکند
+چگونه این کلیدها و مقادیر در حافظه قرار بگیرند. بسیاری از زبانهای برنامهنویسی از این نوع
+ساختار داده پشتیبانی میکنند، اما اغلب از نامهای متفاوتی مانند _هش_، _مپ_، _شیء_،
+_جدول هش_، _دایرکتوری_، یا _آرایه ارتباطی_ برای اشاره به آن استفاده میکنند.
-Hash maps are useful when you want to look up data not by using an index, as
-you can with vectors, but by using a key that can be of any type. For example,
-in a game, you could keep track of each team’s score in a hash map in which
-each key is a team’s name and the values are each team’s score. Given a team
-name, you can retrieve its score.
+هش مپها زمانی مفید هستند که بخواهید دادهها را نه با استفاده از یک اندیس، مانند بردارها،
+بلکه با استفاده از یک کلید که میتواند هر نوعی باشد، جستجو کنید. برای مثال، در یک بازی،
+میتوانید امتیاز هر تیم را در یک هش مپ ذخیره کنید که هر کلید نام یک تیم و هر مقدار
+امتیاز آن تیم باشد. با داشتن نام یک تیم، میتوانید امتیاز آن را بازیابی کنید.
-We’ll go over the basic API of hash maps in this section, but many more goodies
-are hiding in the functions defined on `HashMap` by the standard library.
-As always, check the standard library documentation for more information.
+در این بخش، به API اصلی هش مپها میپردازیم، اما امکانات بیشتری در توابع تعریف شده
+روی `HashMap` در کتابخانه استاندارد وجود دارد. مانند همیشه، مستندات کتابخانه
+استاندارد را برای اطلاعات بیشتر بررسی کنید.
-### Creating a New Hash Map
+### ایجاد یک هش مپ جدید
-One way to create an empty hash map is to use `new` and to add elements with
-`insert`. In Listing 8-20, we’re keeping track of the scores of two teams whose
-names are _Blue_ and _Yellow_. The Blue team starts with 10 points, and the
-Yellow team starts with 50.
+یکی از راههای ایجاد یک هش مپ خالی استفاده از `new` و افزودن عناصر با `insert` است.
+در لیست ۸-۲۰، ما امتیازات دو تیم به نامهای _Blue_ و _Yellow_ را پیگیری میکنیم. تیم
+آبی با ۱۰ امتیاز و تیم زرد با ۵۰ امتیاز شروع میکنند.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}}
@@ -32,23 +29,21 @@ Yellow team starts with 50.
-Note that we need to first `use` the `HashMap` from the collections portion of
-the standard library. Of our three common collections, this one is the least
-often used, so it’s not included in the features brought into scope
-automatically in the prelude. Hash maps also have less support from the
-standard library; there’s no built-in macro to construct them, for example.
+توجه داشته باشید که ابتدا باید `HashMap` را از بخش مجموعههای کتابخانه استاندارد `use`
+کنیم. از میان سه مجموعه رایج ما، این یکی کمتر مورد استفاده قرار میگیرد، بنابراین به طور
+پیشفرض در محدوده وارد نمیشود. همچنین، هش مپها از حمایت کمتری از کتابخانه استاندارد
+برخوردارند؛ برای مثال، هیچ ماکروی داخلی برای ساخت آنها وجود ندارد.
-Just like vectors, hash maps store their data on the heap. This `HashMap` has
-keys of type `String` and values of type `i32`. Like vectors, hash maps are
-homogeneous: all of the keys must have the same type, and all of the values
-must have the same type.
+همانند بردارها، هش مپها دادههای خود را روی heap ذخیره میکنند. این `HashMap` دارای
+کلیدهایی از نوع `String` و مقادیری از نوع `i32` است. مانند بردارها، هش مپها همگن هستند:
+تمام کلیدها باید از یک نوع باشند و تمام مقادیر نیز باید از یک نوع باشند.
-### Accessing Values in a Hash Map
+### دسترسی به مقادیر در یک هش مپ
-We can get a value out of the hash map by providing its key to the `get`
-method, as shown in Listing 8-21.
+میتوانیم یک مقدار را با ارائه کلید آن به متد `get` از هش مپ دریافت کنیم، همانطور که
+در لیست ۸-۲۱ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}}
@@ -56,34 +51,33 @@ method, as shown in Listing 8-21.
-Here, `score` will have the value that’s associated with the Blue team, and the
-result will be `10`. The `get` method returns an `Option<&V>`; if there’s no
-value for that key in the hash map, `get` will return `None`. This program
-handles the `Option` by calling `copied` to get an `Option` rather than an
-`Option<&i32>`, then `unwrap_or` to set `score` to zero if `scores` doesn’t
-have an entry for the key.
+اینجا، مقدار `score` برابر با مقداری خواهد بود که به تیم Blue مرتبط است، و نتیجه `10` خواهد بود.
+متد `get` یک `Option<&V>` را بازمیگرداند؛ اگر هیچ مقداری برای آن کلید در هش مپ وجود نداشته
+باشد، `get` مقدار `None` را بازمیگرداند. این برنامه مقدار `Option` را با فراخوانی `copied`
+برای دریافت یک `Option` به جای `Option<&i32>` مدیریت میکند، سپس با استفاده از `unwrap_or`
+مقدار `score` را به صفر تنظیم میکند اگر `scores` یک ورودی برای کلید نداشته باشد.
-We can iterate over each key-value pair in a hash map in a similar manner as we
-do with vectors, using a `for` loop:
+میتوانیم روی هر جفت کلید-مقدار در یک `hash map` بهروشی مشابه با `vector`ها پیمایش کنیم،
+با استفاده از یک حلقهی `for`:
```rust
{{#rustdoc_include ../listings/ch08-common-collections/no-listing-03-iterate-over-hashmap/src/main.rs:here}}
```
-This code will print each pair in an arbitrary order:
+این کد هر جفت را به ترتیب دلخواه چاپ خواهد کرد:
```text
Yellow: 50
Blue: 10
```
-### Hash Maps and Ownership
+### هش مپها و مالکیت
-For types that implement the `Copy` trait, like `i32`, the values are copied
-into the hash map. For owned values like `String`, the values will be moved and
-the hash map will be the owner of those values, as demonstrated in Listing 8-22.
+برای انواعی که ویژگی `Copy` را پیادهسازی میکنند، مانند `i32`، مقادیر درون هش مپ کپی میشوند.
+برای مقادیر مالک مانند `String`، مقادیر منتقل شده و هش مپ مالک آنها خواهد شد، همانطور که در
+لیست ۸-۲۲ نشان داده شده است.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}}
@@ -91,38 +85,35 @@ the hash map will be the owner of those values, as demonstrated in Listing 8-22.
-We aren’t able to use the variables `field_name` and `field_value` after
-they’ve been moved into the hash map with the call to `insert`.
+پس از انتقال متغیرهای `field_name` و `field_value` به هش مپ با فراخوانی `insert`، دیگر نمیتوانیم
+از آنها استفاده کنیم.
-If we insert references to values into the hash map, the values won’t be moved
-into the hash map. The values that the references point to must be valid for at
-least as long as the hash map is valid. We’ll talk more about these issues in
-[“Validating References with
-Lifetimes”][validating-references-with-lifetimes] in Chapter 10.
+اگر رفرنسهایی به مقادیر را درون `hash map` قرار دهیم، آن مقادیر به درون `hash map` منتقل نخواهند شد (moved نمیشوند).
+مقدارهایی که این رفرنسها به آنها اشاره میکنند، باید حداقل تا زمانی معتبر باشند که `hash map` معتبر است.
+در فصل ۱۰، در بخش [“اعتبارسنجی رفرنسها با استفاده از lifetime”][validating-references-with-lifetimes]،
+بیشتر دربارهی این مسائل صحبت خواهیم کرد.
-### Updating a Hash Map
+### بهروزرسانی یک هش مپ
-Although the number of key and value pairs is growable, each unique key can
-only have one value associated with it at a time (but not vice versa: for
-example, both the Blue team and the Yellow team could have the value `10`
-stored in the `scores` hash map).
+اگرچه تعداد جفتهای کلید و مقدار قابل افزایش است، هر کلید یکتا فقط میتواند یک مقدار
+مرتبط داشته باشد (اما نه بالعکس: برای مثال، هر دو تیم Blue و Yellow میتوانند مقدار `10`
+را در هش مپ `scores` ذخیره کنند).
-When you want to change the data in a hash map, you have to decide how to
-handle the case when a key already has a value assigned. You could replace the
-old value with the new value, completely disregarding the old value. You could
-keep the old value and ignore the new value, only adding the new value if the
-key _doesn’t_ already have a value. Or you could combine the old value and the
-new value. Let’s look at how to do each of these!
+وقتی میخواهید دادهها را در یک هش مپ تغییر دهید، باید تصمیم بگیرید چگونه با حالتی که
+یک کلید قبلاً دارای مقدار است برخورد کنید. میتوانید مقدار قدیمی را با مقدار جدید جایگزین کنید
+و مقدار قدیمی را کاملاً نادیده بگیرید. میتوانید مقدار قدیمی را نگه دارید و مقدار جدید را نادیده بگیرید،
+فقط مقدار جدید را اضافه کنید اگر کلید _ندارد_ قبلاً یک مقدار. یا میتوانید مقدار قدیمی و مقدار جدید
+را با هم ترکیب کنید. بیایید ببینیم چگونه هر یک از این کارها را انجام دهیم!
-#### Overwriting a Value
+#### بازنویسی یک مقدار
-If we insert a key and a value into a hash map and then insert that same key
-with a different value, the value associated with that key will be replaced.
-Even though the code in Listing 8-23 calls `insert` twice, the hash map will
-only contain one key-value pair because we’re inserting the value for the Blue
-team’s key both times.
+اگر یک کلید و مقدار را درون یک `hash map` قرار دهیم و سپس همان کلید را با یک مقدار متفاوت دوباره وارد کنیم،
+مقدار مرتبط با آن کلید جایگزین خواهد شد.
+حتی با اینکه کد در لیستینگ 8-23 دوبار تابع `insert` را فراخوانی میکند،
+`hash map` تنها شامل یک جفت کلید-مقدار خواهد بود،
+زیرا هر دو بار مقدار مربوط به کلید تیم Blue را وارد میکنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}}
@@ -130,28 +121,23 @@ team’s key both times.
-This code will print `{"Blue": 25}`. The original value of `10` has been
-overwritten.
-
-
+این کد مقدار `{"Blue": 25}` را چاپ خواهد کرد. مقدار اصلی `10` بازنویسی شده است.
-#### Adding a Key and Value Only If a Key Isn’t Present
+#### اضافه کردن یک کلید و مقدار فقط اگر کلید وجود ندارد
-It’s common to check whether a particular key already exists in the hash map
-with a value and then to take the following actions: if the key does exist in
-the hash map, the existing value should remain the way it is; if the key
-doesn’t exist, insert it and a value for it.
+بررسی اینکه آیا یک کلید خاص در هش مپ دارای مقدار است یا خیر و سپس انجام اقدامات زیر رایج است:
+اگر کلید در هش مپ وجود دارد، مقدار موجود باید همانطور که هست باقی بماند؛ اگر کلید وجود ندارد،
+آن را به همراه یک مقدار وارد کنید.
-Hash maps have a special API for this called `entry` that takes the key you
-want to check as a parameter. The return value of the `entry` method is an enum
-called `Entry` that represents a value that might or might not exist. Let’s say
-we want to check whether the key for the Yellow team has a value associated
-with it. If it doesn’t, we want to insert the value `50`, and the same for the
-Blue team. Using the `entry` API, the code looks like Listing 8-24.
+هش مپها یک API خاص برای این کار دارند که به نام `entry` شناخته میشود و کلیدی که میخواهید بررسی کنید
+را به عنوان پارامتر میگیرد. مقدار بازگشتی متد `entry` یک enum به نام `Entry` است که نشاندهنده مقداری
+است که ممکن است وجود داشته باشد یا نداشته باشد. فرض کنید میخواهیم بررسی کنیم که آیا کلید تیم Yellow
+دارای مقدار مرتبط است یا خیر. اگر ندارد، میخواهیم مقدار `50` را وارد کنیم، و همینطور برای تیم Blue.
+با استفاده از API `entry`، کد به شکل لیست ۸-۲۴ خواهد بود.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}}
@@ -159,28 +145,25 @@ Blue team. Using the `entry` API, the code looks like Listing 8-24.
-The `or_insert` method on `Entry` is defined to return a mutable reference to
-the value for the corresponding `Entry` key if that key exists, and if not, it
-inserts the parameter as the new value for this key and returns a mutable
-reference to the new value. This technique is much cleaner than writing the
-logic ourselves and, in addition, plays more nicely with the borrow checker.
+متد `or_insert` روی `Entry` به گونهای تعریف شده است که یک مرجع قابل تغییر به مقدار مرتبط با کلید
+`Entry` برمیگرداند اگر آن کلید وجود داشته باشد، و اگر نه، پارامتر را به عنوان مقدار جدید برای
+این کلید وارد کرده و یک مرجع قابل تغییر به مقدار جدید بازمیگرداند. این تکنیک بسیار تمیزتر از نوشتن
+منطق به صورت دستی است و علاوه بر این، با بررسیکننده قرض بهتر کار میکند.
+
+اجرای کد در لیست ۸-۲۴ مقدار `{"Yellow": 50, "Blue": 10}` را چاپ خواهد کرد. اولین فراخوانی به `entry`
+کلید تیم Yellow را با مقدار `50` وارد میکند زیرا تیم Yellow قبلاً مقداری ندارد. دومین فراخوانی
+به `entry` هش مپ را تغییر نمیدهد زیرا تیم Blue قبلاً مقدار `10` را دارد.
-Running the code in Listing 8-24 will print `{"Yellow": 50, "Blue": 10}`. The
-first call to `entry` will insert the key for the Yellow team with the value
-`50` because the Yellow team doesn’t have a value already. The second call to
-`entry` will not change the hash map because the Blue team already has the
-value `10`.
-#### Updating a Value Based on the Old Value
+#### بهروزرسانی یک مقدار بر اساس مقدار قدیمی
-Another common use case for hash maps is to look up a key’s value and then
-update it based on the old value. For instance, Listing 8-25 shows code that
-counts how many times each word appears in some text. We use a hash map with
-the words as keys and increment the value to keep track of how many times we’ve
-seen that word. If it’s the first time we’ve seen a word, we’ll first insert
-the value `0`.
+یکی دیگر از موارد استفاده رایج برای هش مپها این است که مقدار یک کلید را جستجو کرده و سپس بر اساس
+مقدار قدیمی آن را بهروزرسانی کنیم. برای مثال، لیست ۸-۲۵ کدی را نشان میدهد که تعداد دفعات ظاهر شدن
+هر کلمه در یک متن را میشمارد. ما از یک هش مپ با کلمات به عنوان کلید و مقدار برای نگهداری تعداد دفعات
+ظاهر شدن هر کلمه استفاده میکنیم. اگر این اولین بار باشد که یک کلمه را مشاهده میکنیم، ابتدا مقدار
+`0` را وارد میکنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}}
@@ -188,60 +171,57 @@ the value `0`.
-This code will print `{"world": 2, "hello": 1, "wonderful": 1}`. You might see
-the same key-value pairs printed in a different order: recall from [“Accessing
-Values in a Hash Map”][access] that iterating over a hash map
-happens in an arbitrary order.
-
-The `split_whitespace` method returns an iterator over subslices, separated by
-whitespace, of the value in `text`. The `or_insert` method returns a mutable
-reference (`&mut V`) to the value for the specified key. Here, we store that
-mutable reference in the `count` variable, so in order to assign to that value,
-we must first dereference `count` using the asterisk (`*`). The mutable
-reference goes out of scope at the end of the `for` loop, so all of these
-changes are safe and allowed by the borrowing rules.
-
-### Hashing Functions
-
-By default, `HashMap` uses a hashing function called _SipHash_ that can provide
-resistance to denial-of-service (DoS) attacks involving hash
-tables[^siphash]. This is not the fastest hashing algorithm
-available, but the trade-off for better security that comes with the drop in
-performance is worth it. If you profile your code and find that the default
-hash function is too slow for your purposes, you can switch to another function
-by specifying a different hasher. A _hasher_ is a type that implements the
-`BuildHasher` trait. We’ll talk about traits and how to implement them in
-[Chapter 10][traits]. You don’t necessarily have to implement
-your own hasher from scratch; [crates.io](https://crates.io/)
-has libraries shared by other Rust users that provide hashers implementing many
-common hashing algorithms.
+این کد مقدار `{"world": 2, "hello": 1, "wonderful": 1}` را چاپ خواهد کرد.
+ممکن است همین جفتهای کلید-مقدار را به ترتیب متفاوتی ببینید:
+به یاد داشته باشید از بخش [“دسترسی به مقادیر در یک Hash Map”][access]
+که پیمایش در یک `hash map` بهصورت ترتیبی دلخواه (arbitrary order) انجام میشود.
+
+متد `split_whitespace` یک iterator بر روی زیررشتههایی که با فضای خالی جدا شدهاند از مقدار موجود
+در `text` بازمیگرداند. متد `or_insert` یک مرجع قابل تغییر (`&mut V`) به مقدار مرتبط با کلید مشخص
+برمیگرداند. اگر آن کلید وجود داشته باشد، مقدار بازگشتی همان مقدار موجود است؛ و اگر نه، پارامتر
+را به عنوان مقدار جدید برای این کلید وارد میکند و مرجع قابل تغییر به مقدار جدید را بازمیگرداند.
+در اینجا، ما این مرجع قابل تغییر را در متغیر `count` ذخیره میکنیم، بنابراین برای تخصیص مقدار
+به آن، باید ابتدا `count` را با استفاده از عملگر ستاره (`*`) dereference کنیم. مرجع قابل تغییر
+در انتهای حلقه `for` از محدوده خارج میشود، بنابراین تمام این تغییرات ایمن هستند و قوانین
+قرضگیری را نقض نمیکنند.
+
+### توابع هش
+
+به طور پیشفرض، `HashMap` از یک تابع هش به نام _SipHash_ استفاده میکند که مقاومت در برابر
+حملات انکار سرویس (DoS) مربوط به جداول هش[^siphash] را فراهم میکند. این سریعترین
+الگوریتم هش موجود نیست، اما مبادله برای امنیت بهتر با کاهش عملکرد ارزشمند است. اگر کد خود را
+پروفایل کنید و متوجه شوید که تابع هش پیشفرض برای اهداف شما بسیار کند است، میتوانید با مشخص کردن
+یک هشکننده دیگر آن را تغییر دهید. یک _هشکننده_ نوعی است که ویژگی `BuildHasher` را پیادهسازی
+میکند. درباره ویژگیها (traits) و نحوه پیادهسازی آنها در [فصل ۱۰][traits] صحبت
+خواهیم کرد. نیازی نیست حتماً هشکننده خود را از ابتدا پیادهسازی کنید؛ در
+[crates.io](https://crates.io/) کتابخانههایی موجود هستند که توسط کاربران Rust به
+اشتراک گذاشته شدهاند و هشکنندههایی با بسیاری از الگوریتمهای هش رایج ارائه میدهند.
[^siphash]: [https://en.wikipedia.org/wiki/SipHash](https://en.wikipedia.org/wiki/SipHash)
-## Summary
-
-Vectors, strings, and hash maps will provide a large amount of functionality
-necessary in programs when you need to store, access, and modify data. Here are
-some exercises you should now be equipped to solve:
-
-1. Given a list of integers, use a vector and return the median (when sorted,
- the value in the middle position) and mode (the value that occurs most
- often; a hash map will be helpful here) of the list.
-1. Convert strings to pig latin. The first consonant of each word is moved to
- the end of the word and _ay_ is added, so _first_ becomes _irst-fay_. Words
- that start with a vowel have _hay_ added to the end instead (_apple_ becomes
- _apple-hay_). Keep in mind the details about UTF-8 encoding!
-1. Using a hash map and vectors, create a text interface to allow a user to add
- employee names to a department in a company; for example, “Add Sally to
- Engineering” or “Add Amir to Sales.” Then let the user retrieve a list of all
- people in a department or all people in the company by department, sorted
- alphabetically.
-
-The standard library API documentation describes methods that vectors, strings,
-and hash maps have that will be helpful for these exercises!
-
-We’re getting into more complex programs in which operations can fail, so it’s
-a perfect time to discuss error handling. We’ll do that next!
+## خلاصه
+
+بردارها، رشتهها، و هش مپها مقدار زیادی از قابلیتهای مورد نیاز برای ذخیره، دسترسی، و
+تغییر دادهها در برنامهها را فراهم میکنند. در اینجا چند تمرین وجود دارد که اکنون باید
+قادر به حل آنها باشید:
+
+1. با داشتن یک لیست از اعداد صحیح، از یک بردار استفاده کرده و میانه (وقتی مرتبسازی شود، مقداری که
+ در موقعیت وسط قرار دارد) و مد (مقداری که بیشترین بار ظاهر میشود؛ یک هش مپ در اینجا مفید خواهد بود)
+ لیست را بازگردانید.
+2. رشتهها را به زبان لاتین خوکی تبدیل کنید. اولین صامت هر کلمه به انتهای کلمه منتقل شده و _ay_
+ به آن اضافه میشود، بنابراین _first_ به _irst-fay_ تبدیل میشود. کلماتی که با یک حرف صدادار
+ شروع میشوند، _hay_ به انتهای آنها اضافه میشود (_apple_ به _apple-hay_ تبدیل میشود). جزئیات
+ مربوط به کدگذاری UTF-8 را در نظر داشته باشید!
+3. با استفاده از یک هش مپ و بردارها، یک رابط متنی ایجاد کنید تا به کاربر امکان اضافه کردن نام کارمندان
+ به یک دپارتمان در شرکت را بدهد؛ برای مثال، "Add Sally to Engineering" یا "Add Amir to Sales".
+ سپس به کاربر اجازه دهید لیستی از تمام افراد در یک دپارتمان یا تمام افراد در شرکت بر اساس
+ دپارتمان، مرتب شده به صورت حروف الفبا، بازیابی کند.
+
+مستندات API کتابخانه استاندارد متدهایی را که بردارها، رشتهها، و هش مپها دارند و برای این تمرینها
+مفید خواهند بود توصیف میکنند!
+
+ما وارد برنامههای پیچیدهتری شدهایم که در آنها عملیات ممکن است با شکست مواجه شوند، بنابراین
+زمان مناسبی است تا درباره مدیریت خطا صحبت کنیم. این کار را در ادامه انجام خواهیم داد!
[validating-references-with-lifetimes]: ch10-03-lifetime-syntax.html#validating-references-with-lifetimes
[access]: #accessing-values-in-a-hash-map
diff --git a/src/ch09-00-error-handling.md b/src/ch09-00-error-handling.md
index ad49b70a86..e29fd5c3bd 100644
--- a/src/ch09-00-error-handling.md
+++ b/src/ch09-00-error-handling.md
@@ -1,24 +1,22 @@
-# Error Handling
+# مدیریت خطاها
-Errors are a fact of life in software, so Rust has a number of features for
-handling situations in which something goes wrong. In many cases, Rust requires
-you to acknowledge the possibility of an error and take some action before your
-code will compile. This requirement makes your program more robust by ensuring
-that you’ll discover errors and handle them appropriately before deploying your
-code to production!
+خطاها بخشی اجتنابناپذیر از زندگی در دنیای نرمافزار هستند،
+و به همین دلیل، Rust ویژگیهای متعددی برای مدیریت موقعیتهایی دارد که در آنها مشکلی پیش میآید.
+در بسیاری از موارد، Rust شما را ملزم میکند که امکان وقوع یک خطا را به رسمیت بشناسید
+و پیش از آنکه کد شما کامپایل شود، اقدامی انجام دهید.
+این الزام باعث میشود برنامهی شما مقاومتر باشد،
+زیرا تضمین میکند که خطاها را پیش از استقرار کد در محیط اجرایی (production) شناسایی کرده
+و بهدرستی مدیریت کردهاید.
-Rust groups errors into two major categories: _recoverable_ and _unrecoverable_
-errors. For a recoverable error, such as a _file not found_ error, we most
-likely just want to report the problem to the user and retry the operation.
-Unrecoverable errors are always symptoms of bugs, such as trying to access a
-location beyond the end of an array, and so we want to immediately stop the
-program.
+Rust خطاها را به دو دسته اصلی تقسیم میکند: خطاهای _قابل بازیابی_ و خطاهای _غیرقابل بازیابی_.
+برای یک خطای قابل بازیابی، مانند خطای _فایل یافت نشد_، احتمالاً میخواهیم مشکل را به کاربر
+گزارش دهیم و عملیات را دوباره انجام دهیم. خطاهای غیرقابل بازیابی همیشه نشانههای باگها هستند،
+مانند تلاش برای دسترسی به مکانی خارج از انتهای یک آرایه، بنابراین میخواهیم بلافاصله برنامه
+را متوقف کنیم.
-Most languages don’t distinguish between these two kinds of errors and handle
-both in the same way, using mechanisms such as exceptions. Rust doesn’t have
-exceptions. Instead, it has the type `Result` for recoverable errors and
-the `panic!` macro that stops execution when the program encounters an
-unrecoverable error. This chapter covers calling `panic!` first and then talks
-about returning `Result` values. Additionally, we’ll explore
-considerations when deciding whether to try to recover from an error or to stop
-execution.
+بیشتر زبانها بین این دو نوع خطا تفاوت قائل نمیشوند و هر دو را به یک شکل مدیریت میکنند،
+با استفاده از مکانیزمهایی مانند استثناها. Rust استثناها ندارد. در عوض، نوع `Result` برای
+خطاهای قابل بازیابی و ماکروی `panic!` که اجرای برنامه را زمانی که با یک خطای غیرقابل بازیابی
+روبرو میشود متوقف میکند، ارائه میدهد. این فصل ابتدا به فراخوانی `panic!` میپردازد و سپس
+در مورد بازگرداندن مقادیر `Result` صحبت میکند. علاوه بر این، ملاحظاتی را هنگام تصمیمگیری
+در مورد اینکه آیا سعی در بازیابی از یک خطا کنیم یا اجرای برنامه را متوقف کنیم، بررسی خواهیم کرد.
diff --git a/src/ch09-01-unrecoverable-errors-with-panic.md b/src/ch09-01-unrecoverable-errors-with-panic.md
index f00bcc9632..a0b7c342f4 100644
--- a/src/ch09-01-unrecoverable-errors-with-panic.md
+++ b/src/ch09-01-unrecoverable-errors-with-panic.md
@@ -1,35 +1,32 @@
-## Unrecoverable Errors with `panic!`
-
-Sometimes bad things happen in your code, and there’s nothing you can do about
-it. In these cases, Rust has the `panic!` macro. There are two ways to cause a
-panic in practice: by taking an action that causes our code to panic (such as
-accessing an array past the end) or by explicitly calling the `panic!` macro.
-In both cases, we cause a panic in our program. By default, these panics will
-print a failure message, unwind, clean up the stack, and quit. Via an
-environment variable, you can also have Rust display the call stack when a
-panic occurs to make it easier to track down the source of the panic.
-
-> ### Unwinding the Stack or Aborting in Response to a Panic
+## خطاهای غیرقابل بازیابی با `panic!`
+
+گاهی اوقات اتفاقات بدی در کد شما رخ میدهد و هیچ کاری نمیتوانید در مورد آن انجام دهید. در این موارد،
+Rust ماکروی `panic!` را ارائه میدهد. دو راه برای ایجاد یک خطا با `panic!` وجود دارد: با انجام عملی
+که باعث ایجاد خطا میشود (مانند دسترسی به یک اندیس (index)خارج از محدوده در یک آرایه) یا با صراحت
+فراخوانی ماکروی `panic!`. در هر دو حالت، ما یک خطا در برنامه خود ایجاد میکنیم. به طور پیشفرض،
+این خطاها یک پیام خطا چاپ میکنند، استک را unwind میکنند، دادهها را پاکسازی میکنند و برنامه
+را متوقف میکنند. با استفاده از یک متغیر محیطی، میتوانید Rust را مجبور کنید هنگام وقوع یک
+`panic`، استک فراخوانی را نمایش دهد تا ردیابی منبع خطا آسانتر شود.
+
+> ### Unwinding the Stack یا متوقف کردن در پاسخ به یک Panic
>
-> By default, when a panic occurs the program starts _unwinding_, which means
-> Rust walks back up the stack and cleans up the data from each function it
-> encounters. However, walking back and cleaning up is a lot of work. Rust,
-> therefore, allows you to choose the alternative of immediately _aborting_,
-> which ends the program without cleaning up.
+> به طور پیشفرض، هنگامی که یک خطا رخ میدهد، برنامه شروع به _unwinding_ میکند، که به معنی این
+> است که Rust استک را به سمت بالا پیمایش میکند و دادهها را از هر تابعی که با آن برخورد میکند
+> پاکسازی میکند. با این حال، پیمایش به بالا و پاکسازی کار زیادی است. بنابراین، Rust به شما اجازه
+> میدهد گزینه جایگزین _abort_ کردن فوری را انتخاب کنید، که برنامه را بدون پاکسازی متوقف میکند.
>
-> Memory that the program was using will then need to be cleaned up by the
-> operating system. If in your project you need to make the resultant binary as
-> small as possible, you can switch from unwinding to aborting upon a panic by
-> adding `panic = 'abort'` to the appropriate `[profile]` sections in your
-> _Cargo.toml_ file. For example, if you want to abort on panic in release mode,
-> add this:
+> حافظهای که برنامه استفاده میکرد نیاز به پاکسازی توسط سیستم عامل خواهد داشت. اگر در پروژه
+> خود نیاز دارید تا فایل باینری حاصل را تا حد ممکن کوچک کنید، میتوانید از unwind به abort در
+> زمان خطا تغییر دهید با اضافه کردن `panic = 'abort'` به بخشهای مناسب `[profile]` در فایل
+> _Cargo.toml_ خود. برای مثال، اگر میخواهید در حالت release در زمان وقوع خطا متوقف شوید،
+> این مورد را اضافه کنید:
>
> ```toml
> [profile.release]
> panic = 'abort'
> ```
-Let’s try calling `panic!` in a simple program:
+بیایید فراخوانی `panic!` را در یک برنامه ساده امتحان کنیم:
@@ -39,35 +36,32 @@ Let’s try calling `panic!` in a simple program:
-When you run the program, you’ll see something like this:
+وقتی برنامه را اجرا کنید، چیزی شبیه به این خواهید دید:
```console
{{#include ../listings/ch09-error-handling/no-listing-01-panic/output.txt}}
```
-The call to `panic!` causes the error message contained in the last two lines.
-The first line shows our panic message and the place in our source code where
-the panic occurred: _src/main.rs:2:5_ indicates that it’s the second line,
-fifth character of our _src/main.rs_ file.
+فراخوانی `panic!` پیام خطای موجود در دو خط آخر را ایجاد میکند. خط اول پیام خطای `panic!` ما
+و مکانی در کد منبع ما که این خطا رخ داده است را نشان میدهد: _src/main.rs:2:5_ نشان میدهد
+که این خط دوم، پنجمین کاراکتر در فایل _src/main.rs_ ما است.
-In this case, the line indicated is part of our code, and if we go to that
-line, we see the `panic!` macro call. In other cases, the `panic!` call might
-be in code that our code calls, and the filename and line number reported by
-the error message will be someone else’s code where the `panic!` macro is
-called, not the line of our code that eventually led to the `panic!` call.
-
-
+در این مورد، خط نشان داده شده بخشی از کد ما است، و اگر به آن خط برویم، فراخوانی ماکروی
+`panic!` را میبینیم. در موارد دیگر، فراخوانی `panic!` ممکن است در کدی باشد که کد ما آن را
+فراخوانی میکند، و نام فایل و شماره خط گزارش شده توسط پیام خطا کدی از دیگران را نشان میدهد
+که در آن ماکروی `panic!` فراخوانی شده است، نه خطی از کد ما که در نهایت منجر به فراخوانی
+`panic!` شد.
-We can use the backtrace of the functions the `panic!` call came from to figure
-out the part of our code that is causing the problem. To understand how to use
-a `panic!` backtrace, let’s look at another example and see what it’s like when
-a `panic!` call comes from a library because of a bug in our code instead of
-from our code calling the macro directly. Listing 9-1 has some code that
-attempts to access an index in a vector beyond the range of valid indexes.
+ما میتوانیم از backtrace توابعی که فراخوانی `panic!` از آنها آمده است استفاده کنیم تا بخش کد
+ما که باعث مشکل شده است را پیدا کنیم. برای درک نحوه استفاده از backtrace یک `panic!`، بیایید
+یک مثال دیگر ببینیم و مشاهده کنیم زمانی که یک فراخوانی `panic!` از یک کتابخانه به دلیل یک باگ
+در کد ما رخ میدهد، به جای اینکه کد ما مستقیماً ماکرو را فراخوانی کند، چگونه است. لیست ۹-۱
+کدی دارد که تلاش میکند به یک اندیس (index)در یک بردار که خارج از محدوده اندیسهای معتبر است
+دسترسی پیدا کند.
-
+
```rust,should_panic,panics
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-01/src/main.rs}}
@@ -75,51 +69,21 @@ attempts to access an index in a vector beyond the range of valid indexes.
-Here, we’re attempting to access the 100th element of our vector (which is at
-index 99 because indexing starts at zero), but the vector has only three
-elements. In this situation, Rust will panic. Using `[]` is supposed to return
-an element, but if you pass an invalid index, there’s no element that Rust
-could return here that would be correct.
+در اینجا، ما سعی داریم به عنصر صدم بردار خود دسترسی پیدا کنیم (که در اندیس (index)۹۹ است زیرا اندیسگذاری از صفر شروع میشود)، اما بردار فقط سه عنصر دارد. در این وضعیت، Rust با یک خطا متوقف میشود. استفاده از `[]` قرار است یک عنصر را بازگرداند، اما اگر یک اندیس (index)نامعتبر را ارائه دهید، هیچ عنصری وجود ندارد که Rust بتواند به درستی بازگرداند.
-In C, attempting to read beyond the end of a data structure is undefined
-behavior. You might get whatever is at the location in memory that would
-correspond to that element in the data structure, even though the memory
-doesn’t belong to that structure. This is called a _buffer overread_ and can
-lead to security vulnerabilities if an attacker is able to manipulate the index
-in such a way as to read data they shouldn’t be allowed to that is stored after
-the data structure.
+در زبان C، تلاش برای خواندن فراتر از انتهای یک ساختار داده رفتاری نامشخص دارد. ممکن است هر چیزی که در مکان حافظهای که با آن عنصر در ساختار داده مطابقت دارد باشد را دریافت کنید، حتی اگر آن حافظه متعلق به آن ساختار نباشد. این به عنوان _buffer overread_ شناخته میشود و میتواند به آسیبپذیریهای امنیتی منجر شود اگر یک مهاجم بتواند اندیس (index)را به گونهای دستکاری کند که دادههایی را بخواند که نباید به آنها دسترسی داشته باشد و پس از ساختار داده ذخیره شدهاند.
-To protect your program from this sort of vulnerability, if you try to read an
-element at an index that doesn’t exist, Rust will stop execution and refuse to
-continue. Let’s try it and see:
+برای محافظت از برنامه شما در برابر این نوع آسیبپذیری، اگر تلاش کنید یک عنصر را در یک اندیسی که وجود ندارد بخوانید، Rust اجرای برنامه را متوقف کرده و از ادامه دادن امتناع میکند. بیایید این موضوع را امتحان کنیم و ببینیم:
```console
{{#include ../listings/ch09-error-handling/listing-09-01/output.txt}}
```
-This error points at line 4 of our _main.rs_ where we attempt to access index
-`99` of the vector in `v`.
-
-The `note:` line tells us that we can set the `RUST_BACKTRACE` environment
-variable to get a backtrace of exactly what happened to cause the error. A
-_backtrace_ is a list of all the functions that have been called to get to this
-point. Backtraces in Rust work as they do in other languages: the key to
-reading the backtrace is to start from the top and read until you see files you
-wrote. That’s the spot where the problem originated. The lines above that spot
-are code that your code has called; the lines below are code that called your
-code. These before-and-after lines might include core Rust code, standard
-library code, or crates that you’re using. Let’s try getting a backtrace by
-setting the `RUST_BACKTRACE` environment variable to any value except `0`.
-Listing 9-2 shows output similar to what you’ll see.
-
-
-
-
+این خطا به خط ۴ فایل _main.rs_ ما اشاره میکند، جایی که سعی داریم به اندیس (index)`99` بردار `v` دسترسی پیدا کنیم.
+
+خط `note:` به ما میگوید که میتوانیم متغیر محیطی `RUST_BACKTRACE` را تنظیم کنیم تا یک backtrace دقیقاً از آنچه باعث خطا شده است دریافت کنیم. یک _backtrace_ لیستی از تمام توابعی است که تا این نقطه فراخوانی شدهاند. backtraceها در Rust مانند زبانهای دیگر کار میکنند: کلید خواندن backtrace این است که از بالا شروع کرده و تا زمانی که فایلهایی که شما نوشتهاید را ببینید، بخوانید. این همان جایی است که مشکل از آنجا منشأ گرفته است. خطوط بالاتر از آن نقطه کدی است که کد شما فراخوانی کرده است؛ خطوط پایینتر کدی است که کد شما را فراخوانی کرده است. این خطوط قبل و بعد ممکن است شامل کد هسته Rust، کد کتابخانه استاندارد، یا جعبه(crate)هایی که استفاده میکنید باشند. بیایید با تنظیم متغیر محیطی `RUST_BACKTRACE` به هر مقداری به غیر از `0` یک backtrace دریافت کنیم. لیست ۹-۲ خروجی مشابه چیزی را که خواهید دید نشان میدهد.
+
+
```console
$ RUST_BACKTRACE=1 cargo run
@@ -147,24 +111,10 @@ note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose bac
-That’s a lot of output! The exact output you see might be different depending
-on your operating system and Rust version. In order to get backtraces with this
-information, debug symbols must be enabled. Debug symbols are enabled by
-default when using `cargo build` or `cargo run` without the `--release` flag,
-as we have here.
-
-In the output in Listing 9-2, line 6 of the backtrace points to the line in our
-project that’s causing the problem: line 4 of _src/main.rs_. If we don’t want
-our program to panic, we should start our investigation at the location pointed
-to by the first line mentioning a file we wrote. In Listing 9-1, where we
-deliberately wrote code that would panic, the way to fix the panic is to not
-request an element beyond the range of the vector indexes. When your code
-panics in the future, you’ll need to figure out what action the code is taking
-with what values to cause the panic and what the code should do instead.
-
-We’ll come back to `panic!` and when we should and should not use `panic!` to
-handle error conditions in the [“To `panic!` or Not to
-`panic!`”][to-panic-or-not-to-panic] section later in this
-chapter. Next, we’ll look at how to recover from an error using `Result`.
+این مقدار زیادی خروجی است! خروجی دقیق ممکن است بسته به سیستم عامل و نسخه Rust شما متفاوت باشد. برای دریافت backtraceها با این اطلاعات، باید نمادهای اشکالزدایی (debug symbols) فعال باشند. نمادهای اشکالزدایی به طور پیشفرض هنگام استفاده از `cargo build` یا `cargo run` بدون فلگ `--release` فعال هستند، همانطور که در اینجا انجام دادیم.
+
+در خروجی لیست ۹-۲، خط ۶ از backtrace به خطی در پروژه ما اشاره میکند که باعث مشکل شده است: خط ۴ فایل _src/main.rs_. اگر نمیخواهیم برنامه ما دچار خطا شود، باید بررسی خود را از مکانی که توسط اولین خطی که اشاره به فایلی که نوشتهایم دارد، آغاز کنیم. در لیست ۹-۱، جایی که به عمد کدی نوشتهایم که باعث خطا شود، راه حل رفع این خطا این است که درخواست یک عنصر فراتر از محدوده اندیسهای بردار نکنیم. زمانی که کد شما در آینده دچار خطا میشود، باید بفهمید که کد با چه مقادیری چه عملی انجام میدهد که باعث خطا میشود و کد چه کاری باید انجام دهد.
+
+ما در بخش [“To `panic!` or Not to `panic!`”][to-panic-or-not-to-panic] که بعداً در این فصل آمده است، دوباره به موضوع `panic!` و زمانی که باید و نباید از `panic!` برای مدیریت شرایط خطا استفاده کنیم بازخواهیم گشت. اکنون، به بررسی نحوه بازیابی از یک خطا با استفاده از `Result` میپردازیم.
[to-panic-or-not-to-panic]: ch09-03-to-panic-or-not-to-panic.html#to-panic-or-not-to-panic
diff --git a/src/ch09-02-recoverable-errors-with-result.md b/src/ch09-02-recoverable-errors-with-result.md
index bfb3c70e4f..69e96b2c53 100644
--- a/src/ch09-02-recoverable-errors-with-result.md
+++ b/src/ch09-02-recoverable-errors-with-result.md
@@ -1,14 +1,12 @@
-## Recoverable Errors with `Result`
+## خطاهای قابل بازیابی با `Result`
-Most errors aren’t serious enough to require the program to stop entirely.
-Sometimes when a function fails it’s for a reason that you can easily interpret
-and respond to. For example, if you try to open a file and that operation fails
-because the file doesn’t exist, you might want to create the file instead of
-terminating the process.
+بیشتر خطاها به اندازهای جدی نیستند که نیاز به توقف کامل برنامه داشته باشند. گاهی اوقات وقتی
+یک تابع با شکست مواجه میشود، دلیلی وجود دارد که میتوانید آن را به راحتی تفسیر کرده و به آن
+پاسخ دهید. برای مثال، اگر بخواهید یک فایل را باز کنید و این عملیات به دلیل وجود نداشتن فایل شکست
+بخورد، ممکن است بخواهید فایل را ایجاد کنید به جای اینکه فرآیند را متوقف کنید.
-Recall from [“Handling Potential Failure with `Result`”][handle_failure] in Chapter 2 that the `Result` enum is defined as having two
-variants, `Ok` and `Err`, as follows:
+به یاد بیاورید از بخش [“Handling Potential Failure with `Result`”][handle_failure]
+در فصل ۲ که `Result` به صورت یک enum تعریف شده که دو حالت دارد، `Ok` و `Err`، به صورت زیر:
```rust
enum Result {
@@ -17,19 +15,16 @@ enum Result {
}
```
-The `T` and `E` are generic type parameters: we’ll discuss generics in more
-detail in Chapter 10. What you need to know right now is that `T` represents
-the type of the value that will be returned in a success case within the `Ok`
-variant, and `E` represents the type of the error that will be returned in a
-failure case within the `Err` variant. Because `Result` has these generic type
-parameters, we can use the `Result` type and the functions defined on it in
-many different situations where the success value and error value we want to
-return may differ.
+`T` و `E` پارامترهای نوع جنریک هستند: ما درباره جنریکها به طور کاملتر در فصل ۱۰ صحبت خواهیم کرد.
+چیزی که اکنون باید بدانید این است که `T` نمایانگر نوع مقداری است که در حالت موفقیت در داخل `Ok`
+بازگردانده میشود، و `E` نمایانگر نوع خطایی است که در حالت شکست در داخل `Err` بازگردانده میشود.
+زیرا `Result` این پارامترهای نوع جنریک را دارد، میتوانیم نوع `Result` و توابع تعریف شده روی آن را
+در بسیاری از شرایط مختلف که مقادیر موفقیت و خطا ممکن است متفاوت باشند، استفاده کنیم.
-Let’s call a function that returns a `Result` value because the function could
-fail. In Listing 9-3 we try to open a file.
+بیایید تابعی را فراخوانی کنیم که یک مقدار `Result` را بازمیگرداند زیرا این تابع ممکن است با شکست
+مواجه شود. در لیست ۹-۳ سعی میکنیم یک فایل را باز کنیم.
-
+
```rust
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-03/src/main.rs}}
@@ -37,29 +32,25 @@ fail. In Listing 9-3 we try to open a file.
-The return type of `File::open` is a `Result`. The generic parameter `T`
-has been filled in by the implementation of `File::open` with the type of the
-success value, `std::fs::File`, which is a file handle. The type of `E` used in
-the error value is `std::io::Error`. This return type means the call to
-`File::open` might succeed and return a file handle that we can read from or
-write to. The function call also might fail: for example, the file might not
-exist, or we might not have permission to access the file. The `File::open`
-function needs to have a way to tell us whether it succeeded or failed and at
-the same time give us either the file handle or error information. This
-information is exactly what the `Result` enum conveys.
-
-In the case where `File::open` succeeds, the value in the variable
-`greeting_file_result` will be an instance of `Ok` that contains a file handle.
-In the case where it fails, the value in `greeting_file_result` will be an
-instance of `Err` that contains more information about the kind of error that
-occurred.
-
-We need to add to the code in Listing 9-3 to take different actions depending
-on the value `File::open` returns. Listing 9-4 shows one way to handle the
-`Result` using a basic tool, the `match` expression that we discussed in
-Chapter 6.
-
-
+نوع بازگشتی `File::open` یک `Result` است. پارامتر نوع جنریک `T` توسط پیادهسازی
+`File::open` با نوع مقدار موفقیت، یعنی `std::fs::File`، که یک فایل هندل است، مقداردهی
+شده است. نوع `E` استفاده شده در مقدار خطا `std::io::Error` است. این نوع بازگشتی به این معنی
+است که فراخوانی `File::open` ممکن است موفقیتآمیز باشد و یک فایل هندل بازگرداند که میتوانیم از
+آن برای خواندن یا نوشتن استفاده کنیم. همچنین ممکن است این فراخوانی با شکست مواجه شود: برای مثال،
+فایل ممکن است وجود نداشته باشد یا ممکن است مجوز دسترسی به فایل را نداشته باشیم. تابع `File::open`
+باید روشی داشته باشد تا به ما بگوید که آیا موفقیتآمیز بود یا شکست خورد و در عین حال فایل هندل یا
+اطلاعات خطا را به ما بدهد. این اطلاعات دقیقاً همان چیزی است که enum `Result` منتقل میکند.
+
+در حالتی که `File::open` موفقیتآمیز باشد، مقدار در متغیر `greeting_file_result` یک نمونه از `Ok`
+خواهد بود که یک فایل هندل را شامل میشود. در حالتی که با شکست مواجه شود، مقدار در
+`greeting_file_result` یک نمونه از `Err` خواهد بود که اطلاعات بیشتری در مورد نوع خطایی که رخ
+داده است را شامل میشود.
+
+باید به کد در لیست ۹-۳ اضافه کنیم تا اقدامات متفاوتی بسته به مقداری که `File::open` بازمیگرداند
+انجام دهیم. لیست ۹-۴ یک روش برای مدیریت `Result` با استفاده از یک ابزار پایه، یعنی عبارت `match`
+که در فصل ۶ مورد بحث قرار گرفت، نشان میدهد.
+
+
```rust,should_panic
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-04/src/main.rs}}
@@ -67,40 +58,23 @@ Chapter 6.
-Note that, like the `Option` enum, the `Result` enum and its variants have been
-brought into scope by the prelude, so we don’t need to specify `Result::`
-before the `Ok` and `Err` variants in the `match` arms.
+توجه داشته باشید که مانند enum `Option`، enum `Result` و حالات آن به وسیله prelude به محدوده آورده شدهاند، بنابراین نیازی نیست قبل از حالات `Ok` و `Err` در بازوهای `match` از `Result::` استفاده کنیم.
-When the result is `Ok`, this code will return the inner `file` value out of
-the `Ok` variant, and we then assign that file handle value to the variable
-`greeting_file`. After the `match`, we can use the file handle for reading or
-writing.
+وقتی نتیجه `Ok` باشد، این کد مقدار داخلی `file` را از حالت `Ok` بازمیگرداند و سپس آن مقدار فایل هندل را به متغیر `greeting_file` اختصاص میدهیم. بعد از `match`، میتوانیم از فایل هندل برای خواندن یا نوشتن استفاده کنیم.
-The other arm of the `match` handles the case where we get an `Err` value from
-`File::open`. In this example, we’ve chosen to call the `panic!` macro. If
-there’s no file named _hello.txt_ in our current directory and we run this
-code, we’ll see the following output from the `panic!` macro:
+بازوی دیگر `match` حالت زمانی را مدیریت میکند که از `File::open` یک مقدار `Err` دریافت میکنیم. در این مثال، تصمیم گرفتهایم ماکروی `panic!` را فراخوانی کنیم. اگر فایل _hello.txt_ در دایرکتوری فعلی ما وجود نداشته باشد و این کد را اجرا کنیم، خروجی زیر را از ماکروی `panic!` خواهیم دید:
```console
{{#include ../listings/ch09-error-handling/listing-09-04/output.txt}}
```
-As usual, this output tells us exactly what has gone wrong.
+مثل همیشه، این خروجی دقیقاً به ما میگوید چه اشتباهی رخ داده است.
-### Matching on Different Errors
+### مطابقت بر اساس خطاهای مختلف
-The code in Listing 9-4 will `panic!` no matter why `File::open` failed.
-However, we want to take different actions for different failure reasons. If
-`File::open` failed because the file doesn’t exist, we want to create the file
-and return the handle to the new file. If `File::open` failed for any other
-reason—for example, because we didn’t have permission to open the file—we still
-want the code to `panic!` in the same way it did in Listing 9-4. For this, we
-add an inner `match` expression, shown in Listing 9-5.
+کد در لیست ۹-۴ در هر صورتی که `File::open` با شکست مواجه شود، ماکروی `panic!` را فراخوانی میکند. با این حال، ما میخواهیم اقدامات متفاوتی برای دلایل مختلف شکست انجام دهیم. اگر `File::open` به دلیل وجود نداشتن فایل شکست بخورد، میخواهیم فایل را ایجاد کنیم و هندل فایل جدید را بازگردانیم. اگر `File::open` به دلایل دیگری شکست بخورد—برای مثال، به دلیل نداشتن مجوز باز کردن فایل—همچنان میخواهیم کد مانند لیست ۹-۴ `panic!` کند. برای این کار، یک عبارت `match` داخلی اضافه میکنیم که در لیست ۹-۵ نشان داده شده است.
-
-
-
+
```rust,ignore
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-05/src/main.rs}}
@@ -108,34 +82,19 @@ tests to fail lol -->
-The type of the value that `File::open` returns inside the `Err` variant is
-`io::Error`, which is a struct provided by the standard library. This struct
-has a method `kind` that we can call to get an `io::ErrorKind` value. The enum
-`io::ErrorKind` is provided by the standard library and has variants
-representing the different kinds of errors that might result from an `io`
-operation. The variant we want to use is `ErrorKind::NotFound`, which indicates
-the file we’re trying to open doesn’t exist yet. So we match on
-`greeting_file_result`, but we also have an inner match on `error.kind()`.
-
-The condition we want to check in the inner match is whether the value returned
-by `error.kind()` is the `NotFound` variant of the `ErrorKind` enum. If it is,
-we try to create the file with `File::create`. However, because `File::create`
-could also fail, we need a second arm in the inner `match` expression. When the
-file can’t be created, a different error message is printed. The second arm of
-the outer `match` stays the same, so the program panics on any error besides
-the missing file error.
-
-> #### Alternatives to Using `match` with `Result`
->
-> That’s a lot of `match`! The `match` expression is very useful but also very
-> much a primitive. In Chapter 13, you’ll learn about closures, which are used
-> with many of the methods defined on `Result`. These methods can be more
-> concise than using `match` when handling `Result` values in your code.
+نوع مقداری که `File::open` درون حالت `Err` بازمیگرداند، `io::Error` است که یک ساختار داده ارائه شده توسط کتابخانه استاندارد است. این ساختار دارای متدی به نام `kind` است که میتوانیم آن را برای دریافت مقدار `io::ErrorKind` فراخوانی کنیم. enum `io::ErrorKind` توسط کتابخانه استاندارد ارائه شده و شامل حالتهایی است که انواع مختلف خطاهای ممکن در یک عملیات `io` را نمایش میدهد. حالتی که میخواهیم از آن استفاده کنیم `ErrorKind::NotFound` است که نشان میدهد فایل مورد نظر برای باز کردن هنوز وجود ندارد. بنابراین، ما بر روی `greeting_file_result` مطابقت میدهیم، اما همچنین یک `match` داخلی بر روی `error.kind()` داریم.
+
+شرطی که میخواهیم در `match` داخلی بررسی کنیم این است که آیا مقدار بازگردانده شده توسط `error.kind()` همان حالت `NotFound` از enum `ErrorKind` است یا خیر. اگر چنین باشد، سعی میکنیم فایل را با `File::create` ایجاد کنیم. با این حال، از آنجایی که `File::create` نیز ممکن است شکست بخورد، به یک بازوی دوم در عبارت `match` داخلی نیاز داریم. هنگامی که فایل نمیتواند ایجاد شود، یک پیام خطای متفاوت چاپ میشود. بازوی دوم `match` بیرونی به همان شکل باقی میماند، بنابراین برنامه برای هر خطایی به جز خطای وجود نداشتن فایل، با خطا متوقف میشود.
+
+> #### جایگزینهایی برای استفاده از `match` با `Result`
>
-> For example, here’s another way to write the same logic as shown in Listing
-> 9-5, this time using closures and the `unwrap_or_else` method:
+> استفاده از `match` زیاد است! عبارت `match` بسیار مفید است اما همچنان ابتدایی محسوب میشود.
+> در فصل ۱۳، درباره closures یاد خواهید گرفت که در بسیاری از متدهایی که روی `Result`
+> تعریف شدهاند استفاده میشوند. این متدها میتوانند هنگام مدیریت مقادیر `Result` در کد شما،
+> مختصرتر از استفاده از `match` باشند.
>
->
+> برای مثال، در اینجا راه دیگری برای نوشتن همان منطق نشان داده شده در لیست ۹-۵ آورده شده است،
+> این بار با استفاده از closures و متد `unwrap_or_else`:
>
> ```rust,ignore
> use std::fs::File;
@@ -154,21 +113,18 @@ the missing file error.
> }
> ```
>
-> Although this code has the same behavior as Listing 9-5, it doesn’t contain
-> any `match` expressions and is cleaner to read. Come back to this example
-> after you’ve read Chapter 13, and look up the `unwrap_or_else` method in the
-> standard library documentation. Many more of these methods can clean up huge
-> nested `match` expressions when you’re dealing with errors.
-
-#### Shortcuts for Panic on Error: `unwrap` and `expect`
-
-Using `match` works well enough, but it can be a bit verbose and doesn’t always
-communicate intent well. The `Result` type has many helper methods
-defined on it to do various, more specific tasks. The `unwrap` method is a
-shortcut method implemented just like the `match` expression we wrote in
-Listing 9-4. If the `Result` value is the `Ok` variant, `unwrap` will return
-the value inside the `Ok`. If the `Result` is the `Err` variant, `unwrap` will
-call the `panic!` macro for us. Here is an example of `unwrap` in action:
+> اگرچه این کد همان رفتار لیست ۹-۵ را دارد، اما شامل هیچ عبارت `match` نیست و خواندن آن تمیزتر است.
+> بعد از خواندن فصل ۱۳، به این مثال بازگردید و متد `unwrap_or_else` را در مستندات کتابخانه استاندارد
+> بررسی کنید. بسیاری از این متدها میتوانند عبارتهای `match` تو در تو را هنگام کار با خطاها ساده کنند.
+
+#### میانبرهایی برای توقف برنامه در صورت خطا: `unwrap` و `expect`
+
+استفاده از `match` به اندازه کافی خوب کار میکند، اما ممکن است کمی طولانی باشد و همیشه به خوبی نیت
+را منتقل نکند. نوع `Result` دارای بسیاری از متدهای کمکی است که برای انجام وظایف خاصتر تعریف
+شدهاند. متد `unwrap` یک روش میانبر است که دقیقاً مانند عبارت `match` که در لیست ۹-۴ نوشتیم،
+پیادهسازی شده است. اگر مقدار `Result` در حالت `Ok` باشد، `unwrap` مقدار داخل `Ok` را بازمیگرداند.
+اگر مقدار `Result` در حالت `Err` باشد، `unwrap` ماکروی `panic!` را برای ما فراخوانی میکند. در اینجا
+یک مثال از استفاده از `unwrap` آورده شده است:
@@ -178,24 +134,17 @@ call the `panic!` macro for us. Here is an example of `unwrap` in action:
-If we run this code without a _hello.txt_ file, we’ll see an error message from
-the `panic!` call that the `unwrap` method makes:
-
-
+اگر این کد را بدون فایل _hello.txt_ اجرا کنیم، یک پیام خطا از فراخوانی `panic!` که متد `unwrap` انجام
+میدهد خواهیم دید:
```text
thread 'main' panicked at src/main.rs:4:49:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
```
-Similarly, the `expect` method lets us also choose the `panic!` error message.
-Using `expect` instead of `unwrap` and providing good error messages can convey
-your intent and make tracking down the source of a panic easier. The syntax of
-`expect` looks like this:
+به همین ترتیب، متد `expect` به ما اجازه میدهد پیام خطای ماکروی `panic!` را نیز انتخاب کنیم. استفاده
+از `expect` به جای `unwrap` و ارائه پیامهای خطای خوب میتواند نیت شما را بهتر منتقل کند و پیگیری منبع
+یک خطا را آسانتر کند. سینتکس `expect` به این شکل است:
@@ -205,45 +154,28 @@ your intent and make tracking down the source of a panic easier. The syntax of
-We use `expect` in the same way as `unwrap`: to return the file handle or call
-the `panic!` macro. The error message used by `expect` in its call to `panic!`
-will be the parameter that we pass to `expect`, rather than the default
-`panic!` message that `unwrap` uses. Here’s what it looks like:
-
-
+ما از `expect` به همان شیوهای استفاده میکنیم که از `unwrap` استفاده میکنیم: برای بازگرداندن فایل هندل یا فراخوانی ماکروی `panic!`. پیام خطایی که توسط `expect` در فراخوانی `panic!` استفاده میشود، پارامتری است که ما به `expect` میدهیم، به جای پیام پیشفرض `panic!` که توسط `unwrap` استفاده میشود. اینجا چیزی است که به نظر میرسد:
```text
thread 'main' panicked at src/main.rs:5:10:
hello.txt should be included in this project: Os { code: 2, kind: NotFound, message: "No such file or directory" }
```
-In production-quality code, most Rustaceans choose `expect` rather than
-`unwrap` and give more context about why the operation is expected to always
-succeed. That way, if your assumptions are ever proven wrong, you have more
-information to use in debugging.
-
-### Propagating Errors
+در کد با کیفیت تولید، بیشتر Rustaceanها `expect` را به جای `unwrap` انتخاب میکنند و اطلاعات بیشتری درباره اینکه چرا عملیات باید همیشه موفقیتآمیز باشد ارائه میدهند. به این ترتیب، اگر فرضیات شما هرگز اشتباه ثابت شوند، اطلاعات بیشتری برای استفاده در اشکالزدایی خواهید داشت.
-When a function’s implementation calls something that might fail, instead of
-handling the error within the function itself, you can return the error to the
-calling code so that it can decide what to do. This is known as _propagating_
-the error and gives more control to the calling code, where there might be more
-information or logic that dictates how the error should be handled than what
-you have available in the context of your code.
+### انتشار خطاها (Propagating Errors)
-For example, Listing 9-6 shows a function that reads a username from a file. If
-the file doesn’t exist or can’t be read, this function will return those errors
-to the code that called the function.
+وقتی پیادهسازی یک تابع چیزی را فراخوانی میکند که ممکن است شکست بخورد،
+بهجای آنکه خطا را درون خود تابع مدیریت کند، میتوانید آن خطا را به کدی که تابع را فراخوانی کرده برگردانید
+تا آن کد تصمیم بگیرد که چه کاری باید انجام شود.
+این روش به *انتقال (propagating)* خطا معروف است
+و کنترل بیشتری را به کد فراخواننده میدهد،
+جایی که ممکن است اطلاعات یا منطق بیشتری برای تصمیمگیری در مورد نحوهی مدیریت خطا وجود داشته باشد
+نسبت به آنچه در زمینهی تابع فعلی در دسترس است.
-
+برای مثال، لیست ۹-۶ یک تابع را نشان میدهد که یک نام کاربری را از یک فایل میخواند. اگر فایل وجود نداشته باشد یا قابل خواندن نباشد، این تابع آن خطاها را به کدی که تابع را فراخوانی کرده بازمیگرداند.
-
+
```rust
{{#include ../listings/ch09-error-handling/listing-09-06/src/main.rs:here}}
@@ -251,68 +183,43 @@ don't want to include it for rustdoc testing purposes. -->
-This function can be written in a much shorter way, but we’re going to start by
-doing a lot of it manually in order to explore error handling; at the end,
-we’ll show the shorter way. Let’s look at the return type of the function
-first: `Result`. This means the function is returning a
-value of the type `Result`, where the generic parameter `T` has been
-filled in with the concrete type `String` and the generic type `E` has been
-filled in with the concrete type `io::Error`.
-
-If this function succeeds without any problems, the code that calls this
-function will receive an `Ok` value that holds a `String`—the `username` that
-this function read from the file. If this function encounters any problems, the
-calling code will receive an `Err` value that holds an instance of `io::Error`
-that contains more information about what the problems were. We chose
-`io::Error` as the return type of this function because that happens to be the
-type of the error value returned from both of the operations we’re calling in
-this function’s body that might fail: the `File::open` function and the
-`read_to_string` method.
-
-The body of the function starts by calling the `File::open` function. Then we
-handle the `Result` value with a `match` similar to the `match` in Listing 9-4.
-If `File::open` succeeds, the file handle in the pattern variable `file`
-becomes the value in the mutable variable `username_file` and the function
-continues. In the `Err` case, instead of calling `panic!`, we use the `return`
-keyword to return early out of the function entirely and pass the error value
-from `File::open`, now in the pattern variable `e`, back to the calling code as
-this function’s error value.
-
-So, if we have a file handle in `username_file`, the function then creates a
-new `String` in variable `username` and calls the `read_to_string` method on
-the file handle in `username_file` to read the contents of the file into
-`username`. The `read_to_string` method also returns a `Result` because it
-might fail, even though `File::open` succeeded. So we need another `match` to
-handle that `Result`: if `read_to_string` succeeds, then our function has
-succeeded, and we return the username from the file that’s now in `username`
-wrapped in an `Ok`. If `read_to_string` fails, we return the error value in the
-same way that we returned the error value in the `match` that handled the
-return value of `File::open`. However, we don’t need to explicitly say
-`return`, because this is the last expression in the function.
-
-The code that calls this code will then handle getting either an `Ok` value
-that contains a username or an `Err` value that contains an `io::Error`. It’s
-up to the calling code to decide what to do with those values. If the calling
-code gets an `Err` value, it could call `panic!` and crash the program, use a
-default username, or look up the username from somewhere other than a file, for
-example. We don’t have enough information on what the calling code is actually
-trying to do, so we propagate all the success or error information upward for
-it to handle appropriately.
-
-This pattern of propagating errors is so common in Rust that Rust provides the
-question mark operator `?` to make this easier.
-
-#### A Shortcut for Propagating Errors: The `?` Operator
-
-Listing 9-7 shows an implementation of `read_username_from_file` that has the
-same functionality as in Listing 9-6, but this implementation uses the `?`
-operator.
-
-