Compare commits
No commits in common. "4f793a3569ad7242609d35888ad4d9c03f7d6147" and "f8281d894bf2cfcb63297bcf0a443a4868446d18" have entirely different histories.
4f793a3569
...
f8281d894b
13
opis.txt
13
opis.txt
@ -1,13 +0,0 @@
|
|||||||
Wybrany wzorzec: Iterator
|
|
||||||
|
|
||||||
Kod zawiera klasy:
|
|
||||||
Song - zawiera informacje o nazwie piosenki, nazwie artysty i długości
|
|
||||||
Playlist - zawiera zbiór piosenek oraz funkcje generowania dla niej iteratora
|
|
||||||
PlaylistIter - iterator klasy Playlist pozwalający na pobieranie kolejnych piosenek
|
|
||||||
|
|
||||||
Funkcja main zawiera kod który:
|
|
||||||
- tworzy plaliste
|
|
||||||
- dodaje piosenki do playlisty
|
|
||||||
- wykonuje pętle for na iteratorze stworzonym z Playlist'y i wypisuje informacje o każdej piosence
|
|
||||||
|
|
||||||
Link do repozytorium: https://git.dawidpietrykowski.com/UMK/io_2
|
|
11
src/main.rs
11
src/main.rs
@ -11,14 +11,12 @@ struct Playlist{
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Playlist{
|
impl Playlist{
|
||||||
// Empty constructor
|
|
||||||
fn new() -> Playlist {
|
fn new() -> Playlist {
|
||||||
Playlist{
|
Playlist{
|
||||||
songs: Vec::new(),
|
songs: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates iterator from playlist
|
|
||||||
fn iter(&self) -> PlaylistIterator{
|
fn iter(&self) -> PlaylistIterator{
|
||||||
PlaylistIterator{
|
PlaylistIterator{
|
||||||
playlist: self,
|
playlist: self,
|
||||||
@ -26,7 +24,6 @@ impl Playlist{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a song to playlist
|
|
||||||
fn add_song(&mut self, song: Song){
|
fn add_song(&mut self, song: Song){
|
||||||
self.songs.push(song);
|
self.songs.push(song);
|
||||||
}
|
}
|
||||||
@ -41,22 +38,15 @@ impl<'a> Iterator for PlaylistIterator<'a>{
|
|||||||
type Item = &'a Song;
|
type Item = &'a Song;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
// Attempts to get current song
|
|
||||||
let song = self.playlist.songs.get(self.current_index as usize);
|
let song = self.playlist.songs.get(self.current_index as usize);
|
||||||
|
|
||||||
// Moves id to next song
|
|
||||||
self.current_index += 1;
|
self.current_index += 1;
|
||||||
|
|
||||||
// Returns song or nothing
|
|
||||||
song
|
song
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Creating a playlist
|
|
||||||
let mut playlist = Playlist::new();
|
let mut playlist = Playlist::new();
|
||||||
|
|
||||||
// Adding songs to a playlist
|
|
||||||
playlist.add_song(
|
playlist.add_song(
|
||||||
Song{
|
Song{
|
||||||
name: String::from("s1"),
|
name: String::from("s1"),
|
||||||
@ -79,7 +69,6 @@ fn main() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Iterating over playlist and printing song information
|
|
||||||
for song in playlist.iter(){
|
for song in playlist.iter(){
|
||||||
println!("{:?}", song);
|
println!("{:?}", song);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user