متغیرها، بخش اساسی برنامه نویسی هستند و به توسعه دهندگان اجازه میدهند، دادهها را در برنامههای خود ذخیره و دستکاری کنند. Rust که یک زبان برنامه نویسی سیستمهای مدرن است، دارای مجموعهای غنی از ویژگیها برای کار با متغیرها میباشد. در این آموزش به بررسی ایجاد، تغییرپذیری و Scope متغیرها در Rust و همچنین مفهوم Shadow میپردازیم.
ایجاد متغیرها در Rust
در Rust، متغیرها را میتوان با استفاده از کلمه کلیدی let و به دنبال آن، نام متغیر و مقدار اختصاص داده شدهی به آن ایجاد کرد. مثلا:
این یک متغیر به نام x را با مقدار 10 ایجاد میکند. Rust یک زبان استاتیکی است؛ به این معنا که متغیرها باید با نوع خود در زمان ایجاد، اعلان کنند. مثلا:
این یک متغیر به نام x را با نوع i32 (یک عدد صحیح امضاء شدهی 32 بیتی) و مقدار 10 ایجاد میکند.
متغیرهای تغییرپذیر (Mutable) و تغییرناپذیر (Immutable) در Rust
در Rust، متغیرها به طور پیش فرض تغییر ناپذیر هستند؛ به این معنا که پس از تخصیص، مقدار آنها قابل تغییر نیست؛ مثلا:
let x = 10;
x = 20; //compile-time error
برای ایجاد یک متغیر قابل تغییر در Rust، از کلمه کلیدی mut استفاده میشود:
متغیرهای تغییرناپذیر در Rust مفید هستند زیرا میتوانند به جلوگیری از خطاهای ناشی از تغییرات ناخواستهی در دادهها کمک کنند. متغیرهای قابل تغییر، برای زمانیکه دادهها نیاز به اصلاح دارند، مانند یک حلقه، مفید هستند.
Scope متغیرها در Rust
متغیرها در Rust دارای دامنه خاصی هستند که توسط curly braces که بیانیه آنها را احاطه کردهاند، تعریف میشود. مثلا
{
let x = 10;
} // این متغیر خارج از این محدوده در دسترس نخواهد بود
در Rust، متغیرهایی که خارج از یک تابع یا بلوک، با استفاده از کلمهی کلیدی static اعلام میشوند، global scope هستند و از هر نقطهای در برنامه قابل دسترسی هستند. با این حال، متغیرهای global در Rust با ملاحظاتی همراه هستند. از آنجائیکه میتوان به آنها از هر جایی در برنامه دسترسی داشت، ردیابی مکان و زمان تغییر متغیر ممکن است دشوار باشد که میتواند رفتار برنامه را چالش برانگیز کند. این امر به ویژه در برنامههای بزرگتر که ممکن است متغیرهای زیادی در آن در حال استفاده باشند، صادق است. علاوه بر این، استفاده از متغیرهای global میتواند آزمایش و اشکالزدایی کد را سختتر کند؛ زیرا وضعیت آنها میتواند توسط هر بخشی از برنامه تغییر کند.
به این دلایل، به طور کلی توصیه میشود که از متغیرهای global به مقدار کم در Rust استفاده کنید. در عوض، اغلب بهتر است از متغیرهایی استفاده شود که در تابع یا بلوکی که در آن مورد استفاده قرار میگیرند، تعریف و scope شدهاند. این مورد میتواند درک رفتار برنامه را آسانتر کند و از عوارض جانبی ناخواسته ناشی از متغیرهای سراسری جلوگیری کند.
Shadowing Variables in Rust
Shadowing یک مفهوم برنامه نویسی است که به شما امکان میدهد یک متغیر را در یک scope، دوبار اعلام کنید و به طور موثر متغیر اصلی را با متغیر جدیدی به همین نام shadow کنید. وقتی متغیری را shadow میکنید، متغیر جدید، متغیر قبلی را در scope داخلی، "shadow" میکند و هر ارجاعی به این متغیر در آن محدوده، به متغیر جدید اشاره میکند.
fn main() {
let x = 5;
println!("The value of x is: {}", x); // خروجی برابر 5 است
let x = "hello";
println!("The value of x is: {}", x); // خروجی برابر hello
}
در این کد، متغیر x دوبار با مقادیر مختلف اعلان میشود؛ اما اعلان دوم، دوباره از کلمهی کلیدی let استفاده میکند و عملاً متغیر اول را shadow میاندازد. این یعنی println دوم، مقدار متغیر دوم را که یک رشته است، به جای متغیر اول که یک عدد صحیح است، خروجی میدهد.
سایه زدن زمانی میتواند مفید باشد که بخواهید مقدار، یا نوع یک متغیر را در یک scope، بدون اینکه نام آن را تغییر دهید. همچنین میتواند کد را با استفادهی مجدد از یک نام متغیر، برای اهداف مختلف خواناتر کند. با این حال، همچنین میتواند کد را پیچیدهتر و درک آن را سختتر کند؛ بنابراین باید با دقت و با دلیل موجه از آن استفاده کنید .