CodeBlanks Logo Icon CodeBlanks

Adding

Write a function that appends a new task to Vec<Todo>.

Adding

Let's gather the tasks in a Liste struct. To give new tasks increasing ids, we also keep the last id used:

struct Liste {
    gorevler: Vec<Gorev>,
    sonraki_id: u32,
}

impl Liste {
    fn ekle(&mut self, metin: &str) -> u32 {
        self.sonraki_id += 1;
        self.gorevler.push(Gorev {
            id: self.sonraki_id,
            metin: metin.to_string(),
            bitti: false,
        });
        self.sonraki_id
    }
}

ekle takes &mut self because it changes the list, and returns the new task's id.

Constructor

To make creating an empty list easy, a new associated function can be written:

impl Liste {
    fn new() -> Self {
        Self { gorevler: Vec::new(), sonraki_id: 0 }
    }
}

let mut liste = Liste::new();
let id = liste.ekle("Süt al");   // 1

Since the list will be changed, the variable is declared with let mut.

Practice Add tasks

Write fn new() -> Self and fn ekle(&mut self, metin: &str) -> u32 for Liste. Add the tasks "Süt al" and "Kod yaz"; print the number of tasks in the list and the last id added, separated by a space.

Starter code

main.rs
struct Gorev {
    id: u32,
    metin: String,
    bitti: bool,
}

struct Liste {
    gorevler: Vec<Gorev>,
    sonraki_id: u32,
}

fn main() {
    // buraya yaz
}

Sample solution

main.rs
struct Gorev {
    id: u32,
    metin: String,
    bitti: bool,
}

struct Liste {
    gorevler: Vec<Gorev>,
    sonraki_id: u32,
}

impl Liste {
    fn new() -> Self {
        Self {
            gorevler: Vec::new(),
            sonraki_id: 0,
        }
    }
    fn ekle(&mut self, metin: &str) -> u32 {
        self.sonraki_id += 1;
        self.gorevler.push(Gorev {
            id: self.sonraki_id,
            metin: metin.to_string(),
            bitti: false,
        });
        self.sonraki_id
    }
}

fn main() {
    let mut liste = Liste::new();
    liste.ekle("Süt al");
    let son = liste.ekle("Kod yaz");
    println!("{} {}", liste.gorevler.len(), son);
}

Expected output

2 2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why does <code>ekle</code> take <code>metin: &amp;str</code> and call <code>to_string()</code> inside?

  • to_string is mandatory
  • So the caller can pass either a literal or a String, while the task owns its own copy Correct answer
  • &str prints faster
  • A String parameter can't be taken

Explanation: A flexible &str in the parameter, an owned String when storing: a common Rust pattern.

2 Push to the vector

Question
self.gorevler.____(Gorev {
    id: self.sonraki_id,
    metin: metin.to_string(),
    bitti: false,
});
Answer
self.gorevler.push(Gorev {
    id: self.sonraki_id,
    metin: metin.to_string(),
    bitti: false,
});

Explanation: push is used to add an element to the end of a Vec.

3 Piece and role

Term Meaning
Vec<Gorev> Holds the tasks
sonraki_id Last id
&mut self Changes the list
Liste::new() Empty list

All lessons in Beginner