0Pricing
Learn Rust Coding · درس

إنشاء المتجهات وملؤها

أنشئ Vecs وأضف العناصر باستخدام push

إنشاء المتجهات وملؤها درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What Is a Vector?

A vector is a growable list of values, all of the same type. Unlike a fixed-size array, a vector can shrink or grow while your program runs.

In Rust the type is written Vec<T>, where T is the type of element it holds, like Vec<i32> for integers.

An Empty Vector

You can make a fresh, empty vector with Vec::new(). Because it has no values yet, Rust cannot guess the element type, so you usually annotate it.

Here we tell Rust this vector will hold i32 integers.

fn main() {
    let v: Vec<i32> = Vec::new();
    println!("len = {}", v.len());
}

The vec! Macro

The quickest way to create a vector with starting values is the vec! macro. List the values inside square brackets.

Rust looks at the values to infer the element type, so no annotation is needed here.

fn main() {
    let nums = vec![10, 20, 30];
    println!("{:?}", nums);
}

Printing a Vector

A whole vector is printed with the debug formatter {:?}, not the normal {}. The debug form shows the values inside square brackets.

Use {:#?} for a pretty, multi-line layout when a vector is large.

fn main() {
    let names = vec!["Ann", "Bo", "Cy"];
    println!("{:?}", names);
}

Pushing Values

To add a value to the end of a vector, call push. The vector must be declared mut because pushing changes it.

Each push appends one item, growing the length by one.

fn main() {
    let mut v = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    println!("{:?}", v);
}

Type From the First Push

When you start with Vec::new() and no annotation, Rust waits for the first push to learn the element type.

Below, pushing 3.5 tells Rust this is a Vec<f64>. All later values must match that type.

fn main() {
    let mut prices = Vec::new();
    prices.push(3.5);
    prices.push(9.0);
    println!("{:?}", prices);
}

Filling With Repeats

The vec! macro can repeat a value. Write vec![value; count] to build a vector of that value repeated count times.

This is handy for setting up a list of zeros or default values.

fn main() {
    let zeros = vec![0; 5];
    println!("{:?}", zeros);
}

Filling in a Loop

You can fill a vector by pushing inside a loop. Here we add the squares of numbers 1 through 4.

Starting empty and pushing as you go is a common pattern when the values are computed.

fn main() {
    let mut squares = Vec::new();
    for n in 1..=4 {
        squares.push(n * n);
    }
    println!("{:?}", squares);
}

Capacity vs Length

Length is how many items a vector holds now. Capacity is how much room it has reserved before it needs to grow its memory.

If you know roughly how many items you will add, Vec::with_capacity(n) reserves space up front and avoids repeated reallocation.

fn main() {
    let mut v = Vec::with_capacity(10);
    v.push(1);
    println!("len {}, cap {}", v.len(), v.capacity());
}

From an Array

You can turn an array into a vector. One simple way is .to_vec(), which copies the array's elements into a new owned vector.

This is useful when you start with fixed data but need it to grow later.

fn main() {
    let arr = [1, 2, 3];
    let v = arr.to_vec();
    println!("{:?}", v);
}

Checking If Empty

Use is_empty() to check whether a vector has no elements. It returns a bool, which is clearer than comparing the length to zero.

This is a good guard before reading the first element.

fn main() {
    let v: Vec<i32> = Vec::new();
    if v.is_empty() {
        println!("nothing here yet");
    }
}

Quick Check

Test your understanding of creating and filling vectors.

Recap

You learned to create vectors with Vec::new() and the vec! macro, and to fill them using push, repeats, loops, and to_vec().

You also saw length versus capacity and how to check emptiness. Next you will read and loop over vector values.

الأسئلة الشائعة

هل درس «إنشاء المتجهات وملؤها» مجاني؟

نعم — نص درس «إنشاء المتجهات وملؤها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

ماذا ستتعلم في «إنشاء المتجهات وملؤها»؟

أنشئ Vecs وأضف العناصر باستخدام push تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟

لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «إنشاء المتجهات وملؤها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟

نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنشاء المتجهات وملؤها
  2. الفهرسة والتكرار
  3. التوسيع والتقليص
  4. متجهات من Structs
← العودة إلى Learn Rust Coding