Menu
Coddy logo textTech

Recap - Media Player

Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 34 of 61.

challenge icon

Challenge

Easy

Let's build a simple media player system that demonstrates how different media types can share a common interface through traits!

You'll organize your code across three files:

  • playable.rs: Define a public Playable trait with a play(&self) method that prints output when called. This trait represents the shared capability that all media types will have.
  • media.rs: Create two public structs that implement your trait:
    • Audio — with a public title field (String) that prints Playing audio: {title} when played
    • Video — with a public title field (String) that prints Playing video: {title} when played
  • main.rs: Bring your modules together and create instances of both media types using the inputs provided. Call the play method on each to demonstrate how the same trait produces different outputs based on the media type.

Your output should follow this format:

Playing audio: {audio_title}
Playing video: {video_title}

For example, with inputs Bohemian Rhapsody and The Matrix:

Playing audio: Bohemian Rhapsody
Playing video: The Matrix

You will receive two inputs: an audio title and a video title.

Try it yourself

mod playable;
mod media;

use media::{Audio, Video};
use playable::Playable;

fn main() {
    // Read input
    let mut audio_title = String::new();
    std::io::stdin().read_line(&mut audio_title).expect("Failed to read line");
    let audio_title = audio_title.trim().to_string();
    
    let mut video_title = String::new();
    std::io::stdin().read_line(&mut video_title).expect("Failed to read line");
    let video_title = video_title.trim().to_string();
    
    // TODO: Create an Audio instance with audio_title
    
    // TODO: Create a Video instance with video_title
    
    // TODO: Call play() on both instances
}

All lessons in Object Oriented Programming