Skip to content

Commit

Permalink
[BOT] Updating 91f3522 content
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jul 16, 2024
1 parent 91f3522 commit c7783f4
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 132 deletions.
122 changes: 61 additions & 61 deletions book/ch12-06-writing-to-stderr-instead-of-stdout.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,48 +176,48 @@ <h1 class="menu-title">Rust dasturlash tili</h1>

<div id="content" class="content">
<main>
<h2 id="writing-error-messages-to-standard-error-instead-of-standard-output"><a class="header" href="#writing-error-messages-to-standard-error-instead-of-standard-output">Writing Error Messages to Standard Error Instead of Standard Output</a></h2>
<p>At the moment, we’re writing all of our output to the terminal using the
<code>println!</code> macro. In most terminals, there are two kinds of output: <em>standard
output</em> (<code>stdout</code>) for general information and <em>standard error</em> (<code>stderr</code>) for
error messages. This distinction enables users to choose to direct the
successful output of a program to a file but still print error messages to the
screen.</p>
<p>The <code>println!</code> macro is only capable of printing to standard output, so we
have to use something else to print to standard error.</p>
<h3 id="checking-where-errors-are-written"><a class="header" href="#checking-where-errors-are-written">Checking Where Errors Are Written</a></h3>
<p>First, let’s observe how the content printed by <code>minigrep</code> is currently being
written to standard output, including any error messages we want to write to
standard error instead. We’ll do that by redirecting the standard output stream
to a file while intentionally causing an error. We won’t redirect the standard
error stream, so any content sent to standard error will continue to display on
the screen.</p>
<p>Command line programs are expected to send error messages to the standard error
stream so we can still see error messages on the screen even if we redirect the
standard output stream to a file. Our program is not currently well-behaved:
we’re about to see that it saves the error message output to a file instead!</p>
<p>To demonstrate this behavior, we’ll run the program with <code>&gt;</code> and the file path,
<em>output.txt</em>, that we want to redirect the standard output stream to. We won’t
pass any arguments, which should cause an error:</p>
<h2 id="xato-xabarlarini-standart-chiqish-oqimi-orniga-xato-oqimiga-yozish"><a class="header" href="#xato-xabarlarini-standart-chiqish-oqimi-orniga-xato-oqimiga-yozish">Xato xabarlarini standart chiqish oqimi o'rniga xato oqimiga yozish</a></h2>
<p>Ayni paytda biz <code>println!</code> funksiyasidan foydalangan holda terminalga barcha
chiqishlarimizni yozib olamiz. Ko'pgina terminallarda ikkita chiqish turi
mavjud: umumiy ma'lumot uchun standart chiqish oqimi ( <code>stdout</code> )
va xato xabarlari uchun standart xato oqimi (<code>stderr</code>).
Ushbu farq foydalanuvchilarga dasturning muvaffaqiyatli chiqishini faylga yo'naltirishni
tanlashga imkon beradi, ammo shu bilan birga xato
xabarlarini ekranga chiqaradi.</p>
<p><code>println!</code> funktsiyasi (makrosi) faqat standart chiqishda chop etish mumkin,
shuning uchun biz standart xatolar oqimiga chop etish uchun boshqa misollarda
ko'rib chiqaylik.</p>
<h3 id="xatolar-yozilgan-joyni-tekshirish"><a class="header" href="#xatolar-yozilgan-joyni-tekshirish">Xatolar yozilgan joyni tekshirish</a></h3>
<p>Birinchidan, keling, <code>minigrep</code>-dan chop etilgan kontent hozirda standart chiqishga
qanday yozilishini, shu jumladan biz standart xato oqimiga yozmoqchi bo'lgan har qanday
xato xabarlarini ko'rib chiqaylik. Biz buni standart chiqish oqimini faylga yo'naltirish
va ataylab xatoga yo'l qo'yish orqali qilamiz. Biz standart xatolar oqimiga yo'naltirmaganimiz uchun,
standart xatolar oqimiga yuborilgan har qanday tarkib ekranda paydo bo'lishda davom etadi.</p>
<p>Buyruq qatorining (cmd) dasturlari xato xabarlarini standart xato oqimiga yuborishi kutilmoqda,
shuning uchun biz standart chiqish oqimini faylga yo'naltirsak ham, ekrandagi xato xabarlarini
ko'rishimiz mumkin. Bizning dasturimiz hozirda to'g'ri ishlamayapti:
biz xato xabari chiqishini faylga saqlayotganini ko'ramiz.</p>
<p>Ushbu xatti-harakatni namoyish qilish uchun biz dasturni <code>&gt;</code> va <em>output.txt</em> fayl nomi
bilan ishga tushiramiz. Biz standart chiqish oqimini yo'naltirishni xohlaymiz.
Biz hech qanday dalil (argument) keltirmaymiz, bu xatoga olib kelishi kerak:</p>
<pre><code class="language-console">$ cargo run &gt; output.txt
</code></pre>
<p>The <code>&gt;</code> syntax tells the shell to write the contents of standard output to
<em>output.txt</em> instead of the screen. We didn’t see the error message we were
expecting printed to the screen, so that means it must have ended up in the
file. This is what <em>output.txt</em> contains:</p>
<p><code>&gt;</code> sintaksisi qobiqqa (shellga) <em>output.txt</em>-ga ekran o'rniga standart chiqish (standard output) tarkibini yozishni buyuradi.
Biz ekranda ko'rishni kutgan xato xabarini ko'rmadik, shuning uchun u faylda bo'lishi kerak.
Yuqorida keltirilgan <em>output.txt</em>-ning holati:</p>
<pre><code class="language-text">Problem parsing arguments: not enough arguments
</code></pre>
<p>Yup, our error message is being printed to standard output. It’s much more
useful for error messages like this to be printed to standard error so only
data from a successful run ends up in the file. We’ll change that.</p>
<h3 id="printing-errors-to-standard-error"><a class="header" href="#printing-errors-to-standard-error">Printing Errors to Standard Error</a></h3>
<p>We’ll use the code in Listing 12-24 to change how error messages are printed.
Because of the refactoring we did earlier in this chapter, all the code that
prints error messages is in one function, <code>main</code>. The standard library provides
the <code>eprintln!</code> macro that prints to the standard error stream, so let’s change
the two places we were calling <code>println!</code> to print errors to use <code>eprintln!</code>
instead.</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<p>Ha, bizning xato xabarimiz standart chiqishda (standard outputga) ko'rsatiladi.
Bunday xato xabarlarini standart xatolar oqimiga kiritish ancha foydali,
shuning uchun faqat muvaffaqiyatli ishga tushirish ma'lumotlari faylga kiradi.
Biz buni keyinchalik o'zgartiramiz.</p>
<h3 id="xatolarni-standard-xato-oqimiga-chop-etish-print-qilish"><a class="header" href="#xatolarni-standard-xato-oqimiga-chop-etish-print-qilish">Xatolarni standard xato oqimiga chop etish (print qilish)</a></h3>
<p>Xato xabarlarini chiqarish usulini o'zgartirish uchun biz 12-24-ro'yxatdagi koddan
foydalanamiz. Ushbu bobda ilgari qilgan refaktoring tufayli xato xabarlarini chop
etadigan barcha kodlar bitta <code>main</code> funktsiyada joylashgan. Rust standart kutubxonasi <code>eprintln!</code> makrosini
taqdim etadi va bu makro standart xato oqimiga kiradi, shuning uchun <code>println!</code> bilan chaqirilgan joyda,
keling uning o'rniga <code>eprintln!</code> makrosini ishlatamiz.</p>
<p><span class="filename">Fayl: src/main.rs</span></p>
<pre><code class="language-rust ignore"><span class="boring">use std::env;
</span><span class="boring">use std::process;
</span><span class="boring">
Expand All @@ -236,37 +236,37 @@ <h3 id="printing-errors-to-standard-error"><a class="header" href="#printing-err
process::exit(1);
}
}</code></pre>
<p><span class="caption">Listing 12-24: Writing error messages to standard error
instead of standard output using <code>eprintln!</code></span></p>
<p>Let’s now run the program again in the same way, without any arguments and
redirecting standard output with <code>&gt;</code>:</p>
<p><span class="caption">Ro'yxat 12-24: <code>println!</code> yordamida Standard Output o'rniga Standard Error - da xato xabarlarini yozish!</span></p>
<p>Keling, dasturni xuddi shu tarzda, hech qanday dalilsiz (argumentsiz) qayta ishga
tushiramiz va standart chiqishni (outputni) <code>&gt;</code> bilan qayta yo'naltiramiz:</p>
<pre><code class="language-console">$ cargo run &gt; output.txt
Problem parsing arguments: not enough arguments
</code></pre>
<p>Now we see the error onscreen and <em>output.txt</em> contains nothing, which is the
behavior we expect of command line programs.</p>
<p>Let’s run the program again with arguments that don’t cause an error but still
redirect standard output to a file, like so:</p>
<p>Endi biz ekranda xatoni ko'rishimiz mumkin va <em>output.txt</em> esa bo'sh.
Bu esa aynan buyruq qatoridan (cmd-dan) biz kutgan holat.</p>
<p>Keling, dasturni xatoga olib kelmaydigan argumentlar bilan qayta ishga tushiramiz,
ammo baribir standart chiqish faylga yo'naltiradi, misol:</p>
<pre><code class="language-console">$ cargo run -- to poem.txt &gt; output.txt
</code></pre>
<p>We won’t see any output to the terminal, and <em>output.txt</em> will contain our
results:</p>
<p><span class="filename">Filename: output.txt</span></p>
<p>Biz terminalga hech qanday chiqishni ko'rmaymiz. Va <em>output.txt</em> esa
quydagi natijalarni o'z ichiga oladi:</p>
<p><span class="filename">Fayl: output.txt</span></p>
<pre><code class="language-text">Are you nobody, too?
How dreary to be somebody!
</code></pre>
<p>This demonstrates that we’re now using standard output for successful output
and standard error for error output as appropriate.</p>
<h2 id="summary"><a class="header" href="#summary">Summary</a></h2>
<p>This chapter recapped some of the major concepts you’ve learned so far and
covered how to perform common I/O operations in Rust. By using command line
arguments, files, environment variables, and the <code>eprintln!</code> macro for printing
errors, you’re now prepared to write command line applications. Combined with
the concepts in previous chapters, your code will be well organized, store data
effectively in the appropriate data structures, handle errors nicely, and be
well tested.</p>
<p>Next, we’ll explore some Rust features that were influenced by functional
languages: closures and iterators.</p>
<p>Yani, biz vaziyatga qarab, muvaffaqiyatli chiqish (output) uchun
standart chiqish oqimidan va xatolarni chiqarish uchun standart xato oqimidan foydalanamiz.</p>
<h2 id="xulosa"><a class="header" href="#xulosa">Xulosa</a></h2>
<p>Ushbu bobda siz hozirgacha o'rgangan ba'zi asosiy tushunchalar takrorlangan va
Rustda muntazam I/O operatsiyalarini qanday bajarish kerakligi aytilgan.
Buyruq qatori argumentlari (command line argumentlari), fayllar,
atrof-muhit o'zgaruvchilari va <code>println!</code> makrosi yordamida xatolarni
ishlatgan holda, endi siz buyruq qatori (CLI) dasturlarini yozishga tayyormiz.
Oldingi boblardagi tushunchalar bilan birgalikda sizning kodingiz yaxshi tartibga solinadi,
ma'lumotlarni tegishli tuzilmalarda samarali saqlaydi, xatolarni yaxshi qayta ishlaydi va yaxshi
sinovdan o'tkaziladi.</p>
<p>Keyinchalik, biz funktsional tillar ta'sirida bo'lgan Rust-ning
ba'zi xususiyatlarini ko'rib chiqamiz: yopilishlar (closures) va iteratorlar.</p>

</main>

Expand Down
Loading

0 comments on commit c7783f4

Please sign in to comment.