Oddiy o’zgaruvchilar Rust da “{}” ning o’zi bilan ham “println!” macrosi orqali chop etilsa bo’ladi. Lekin ba’zi o’zgaruvchilarni esa unday qilib bo’lmaydi va siz shop etishni debuglashingiz kerak bo’ladi. Debug orqali chop etish bu asosan dasturchilar uchun, sababi bunday usulda odatda ko’proq ma’lumot ko’rsatiladi. Ba’zida, Debug orqali chop etilganda uncha chiroyli yokida tushunarli ko’rinmasligi mumkin, sababi qo’shimcha ma’lumotlar ko’pligi uchun.
Xo’sh, siz qanday qilib “{} yokida “{:?}” ni ishlatishingiz kerakligini bilib olasiz ? Misol uchun:
fn main() {
let doesnt_print = ();
println!("Bu hech narsa chop etmaydi: {}", doesnt_print); // ⚠️
}
Biz bu kodni ishlatishga harakat qilganimizda, kompilyator quyidagi natijani beradi:
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> src\main.rs:3:41
|
3 | println!("This will not print: {}", doesnt_print);
| ^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
Bu yerda juda ham ko’p ma’lumot bor, lekin biz e’tibor qaratishimiz kerak bo’lgan qismi bu “you may be able to use {:?} (or {:#?} for pretty-print) instead“. Buning ma’nosi shuki, biz “{:?}” yokida “{:#?}” orqali chop etsak bo’ladi va bu odatda chiroyli chop etish(pretty-print) deb nomlanadi.
Displayning ma’nosi “{}” orqali chop etish, Debug da esa “{:?}”
Agar siz hamma chop etilshi kerak bo’lgan o’zgaruvchilarni bitta qatorga chop etilishini hoxlasangiz unda “print!” macrosidan foydalanasiz.
fn main() {
print!("This will not print a new line");
println!(" so this will be on the same line");
}
Natjia quyidagicha: This will not print a new line so this will be on the same line.
Eng kichkina va eng katta raqamlar
Agar siz eng kichik va eng katta sonni chop etmoqchi bo’lsangiz, siz MIN yokida MAX funksiyalarini, ma’lumot turidan so’ng yozib ishlatsangiz bo’ladi.
fn main() {
println!("The smallest i8 is {} and the biggest i8 is {}.", i8::MIN, i8::MAX); // hint: std::i8::MIN ma'nosi bu "i8 ma'lumot turidagi eng kichin bo'lgan sonni chop etishdir"
println!("The smallest u8 is {} and the biggest u8 is {}.", u8::MIN, u8::MAX);
println!("The smallest i16 is {} and the biggest i16 is {}.", i16::MIN, i16::MAX);
println!("The smallest u16 is {} and the biggest u16 is {}.", u16::MIN, u16::MAX);
println!("The smallest i32 is {} and the biggest i32 is {}.", i32::MIN, i32::MAX);
println!("The smallest u32 is {} and the biggest u32 is {}.", u32::MIN, u32::MAX);
println!("The smallest i64 is {} and the biggest i64 is {}.", i64::MIN, i64::MAX);
println!("The smallest u64 is {} and the biggest u64 is {}.", u64::MIN, u64::MAX);
println!("The smallest i128 is {} and the biggest i128 is {}.", i128::MIN, i128::MAX);
println!("The smallest u128 is {} and the biggest u128 is {}.", u128::MIN, u128::MAX);
}
Buning natijasi esa:
The smallest i8 is -128 and the biggest i8 is 127.
The smallest u8 is 0 and the biggest u8 is 255.
The smallest i16 is -32768 and the biggest i16 is 32767.
The smallest u16 is 0 and the biggest u16 is 65535.
The smallest i32 is -2147483648 and the biggest i32 is 2147483647.
The smallest u32 is 0 and the biggest u32 is 4294967295.
The smallest i64 is -9223372036854775808 and the biggest i64 is 9223372036854775807.
The smallest u64 is 0 and the biggest u64 is 18446744073709551615.
The smallest i128 is -170141183460469231731687303715884105728 and the biggest i128 is 170141183460469231731687303715884105727.
The smallest u128 is 0 and the biggest u128 is 340282366920938463463374607431768211455.
Leave a Reply