From 4f793a3569ad7242609d35888ad4d9c03f7d6147 Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Tue, 20 Jun 2023 18:47:27 +0200 Subject: [PATCH] Added comments --- src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.rs b/src/main.rs index 1b921f3..7303dc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,12 +11,14 @@ struct Playlist{ } impl Playlist{ + // Empty constructor fn new() -> Playlist { Playlist{ songs: Vec::new(), } } + // Generates iterator from playlist fn iter(&self) -> PlaylistIterator{ PlaylistIterator{ playlist: self, @@ -24,6 +26,7 @@ impl Playlist{ } } + // Adds a song to playlist fn add_song(&mut self, song: Song){ self.songs.push(song); } @@ -38,15 +41,22 @@ impl<'a> Iterator for PlaylistIterator<'a>{ type Item = &'a Song; fn next(&mut self) -> Option { + // Attempts to get current song let song = self.playlist.songs.get(self.current_index as usize); + + // Moves id to next song self.current_index += 1; + + // Returns song or nothing song } } fn main() { + // Creating a playlist let mut playlist = Playlist::new(); + // Adding songs to a playlist playlist.add_song( Song{ name: String::from("s1"), @@ -69,6 +79,7 @@ fn main() { } ); + // Iterating over playlist and printing song information for song in playlist.iter(){ println!("{:?}", song); }