- Boolean
- Unsigned int
- Signed int
- Floating point numbers
- Char
- String types
- Arrays
- Tuples
- Type aliasing
Boolean
در Rust، نوع داده بولین با کلمه کلیدی bool نشان داده میشود. این نوع داده، فقط میتواند دو مقدار داشته باشد: true یا false و معمولاً در دستورات شرطی و حلقهها برای کنترل جریان یک برنامه استفاده میشود.
let is_rust_awesome: bool = true;
Unsigned int
اعداد صحیح بدون علامت در Rust با کلمه کلیدی u و سپس تعداد بیتهایی که عدد صحیح باید داشته باشد، نشان داده میشوند. به عنوان مثال، u8 یک عدد صحیح بدون علامت 8 بیتی را نشان میدهد. محدودهی یک عدد صحیح بدون علامت از 0 تا 2^n - 1 است که n تعداد بیتها است.
let x: u8 = 255;
Signed int
اعداد صحیح علامتدار در Rust با کلمهی کلیدی i و سپس تعداد بیتهایی که عدد صحیح باید داشته باشد، نشان داده میشوند. به عنوان مثال، i32، یک عدد صحیح علامتدار 32 بیتی را نشان میدهد. محدوده یک عدد صحیح علامتدار از -2^(n-1) تا 2^(n-1) - 1 است که n تعداد بیتها است.
let x: i32 = -2147483648;
Floating point numbers
اعداد ممیز شناور در Rust با کلمات کلیدی f32 یا f64 نشان داده میشوند که به ترتیب مخفف اعداد ممیز شناور 32 بیتی و 64 بیتی هستند. این نوع دادهها برای نمایش اعداد واقعی با اعشار استفاده میشوند.
let x: f32 = 3.14;
Char
نوع داده char در Rust، نشان دهنده یک کاراکتر یونیکد است؛ برخلاف رشتههایی که با گیومههای دوتایی (") نشان داده میشوند.
let c: char = 'a';
String types
در Rust دو نوع رشته وجود دارد: String و str. نوع String، یک نوع رشتهای heap-allocated و قابل رشد است؛ در حالیکه str (تلفظ "string slice") یک نوع رشتهای است که به یک برش از یک رشته در حافظه اشاره میکند:
let s1: String = String::from("hello"); let s2: &str = "world";
Arrays
آرایهها در Rust، مجموعههایی با اندازهی ثابت از عناصر از یک نوع هستند. آنها با براکت مربع ([]) و نوع عناصر داخل آرایه نشان داده میشوند.
let arr: [i32; 5] = [1, 2, 3, 4, 5];
Tuples
تاپلها در Rust، مجموعهای از عناصر از انواع مختلف هستند. آنها با پرانتز (()) و انواع عناصر داخل تاپل نشان داده میشوند.
let tup: (i32, f64, u8) = (500, 6.4, 1);
Type aliasing
تایپ aliasing در Rust، به شما امکان میدهد تا نام جدیدی را به یک نوع موجود بدهید. این میتواند برای خوانایی بیشتر کد یا ساده کردن انواع پیچیده مفید باشد.
type Age = u32; let age: Age = 30;