DEV Community

Cover image for Day 15: Crafting Cars in Rust – A Symphony of Geeky Dialogues and Struct Choreography πŸš—πŸ› οΈ
Aniket Botre
Aniket Botre

Posted on

Day 15: Crafting Cars in Rust – A Symphony of Geeky Dialogues and Struct Choreography πŸš—πŸ› οΈ

Ahoy, fellow code weavers! Today, on Day 15 of my #100DaysOfCode Rust escapade, I decided to embark on a code crafting journey, attempting to build a car using user input, a sprinkle of capitalization sorcery, and the dance of structs and enums. Let's dive into the geeky dialogue and struct choreography! πŸŒπŸ’¬

Click to visit my code repo on Github


The Struct Ballad – Where Fields Dance in Harmony πŸ°πŸ’ƒ

In the grand realm of Rust, the Car struct emerges as the master choreographer. Its fields – color, transmission, brand – waltz together, crafting the very essence of our digital vehicle.

struct Car {
    color: String,
    car_type: String,
    transmission: Transmission,
    convertible: bool,
    brand: String,
}

// Enum for Car transmission type
#[derive(PartialEq, Debug)]
enum Transmission {
    Manual,
    SemiAuto,
    Automatic,
}
Enter fullscreen mode Exit fullscreen mode

This elegantly crafted struct defines the essence of a car, blending seamlessly with the symphony of Rustic code.


The Geeky Dialogue – A Conversation with Code Monologues πŸŽ­πŸ’»

No Rustic car would be complete without a user dialogue. The take_user_input function takes on the role of a coding conversationalist, engaging in a tΓͺte-Γ -tΓͺte with users, capturing their dreams and desires for the perfect digital ride.

fn take_user_input(user_input: &mut String) -> String {
    io::stdin()
        .read_line(user_input)
        .expect("Failed to read the input");

    user_input.trim().to_string()
}
Enter fullscreen mode Exit fullscreen mode

This function echoes the whispers of the user, ready to mold their digital dreams into code reality.


The Capitalization Chronicles – String Prestidigitation Unveiled 🌟🎩

In the mystical realm of Rust, the capitalize library takes center stage. Strings transform into eloquent titles, as the colors, brands, and types undergo a spell of capitalization brilliance.

let car_color = car_color.trim().to_string().capitalize();
let car_type = car_type.trim().to_string().capitalize();
Enter fullscreen mode Exit fullscreen mode

The Car Factory Ballet – Struct Choreography in Action πŸŽΆπŸš—

The car_factory function steps onto the stage, the maestro of our digital ballet. It conducts the symphony of user aspirations, weaving them into a tangible creation – the Car struct.

fn car_factory(
    color: String,
    transmission: Transmission,
    convertible: bool,
    brand: String,
    car_type: String,
) -> Car {
    Car {
        color,
        transmission,
        convertible,
        brand,
        car_type,
    }
}
Enter fullscreen mode Exit fullscreen mode

This function symbolizes the dance of the digital artisans, molding user aspirations into a structured masterpiece.


The Grand Finale – A Digital Car Unveiling with Pizzazz πŸš€πŸŽ‰

As the final curtain rises, the crafted car takes center stage. Its unique attributes – brand, type, color, transmission, and convertible allure – are revealed in a dazzling spectacle.

println!(
    "Your car has been built with the following specifications:\nCar brand: {}, Type: {}, Color: {}, Transmission: {:?}, Convertible: {}",
    car.brand, car.car_type, car.color, car.transmission, car.convertible
);
Enter fullscreen mode Exit fullscreen mode

Complete code:

use capitalize::Capitalize;
use std::io;

struct Car {
    color: String,
    car_type: String,
    transmission: Transmission,
    convertible: bool,
    brand: String,
}

#[derive(PartialEq, Debug)]
// Declare enum for Car transmission type
enum Transmission {
    Manual,
    SemiAuto,
    Automatic,
}

fn main() {
    let mut car_color = String::new();
    let mut car_transmission = String::new();
    let mut car_brand = String::new();
    let mut convertible = String::new();
    let mut car_type = String::new();

    println!("Enter the car color:");
    take_user_input(&mut car_color);

    println!("Enter the car transmission: (Manual, SemiAuto or Automatic))");
    take_user_input(&mut car_transmission);

    println!("Enter the car brand:");
    take_user_input(&mut car_brand);

    println!("Do you want to have a convertible? (yes or no)");
    take_user_input(&mut convertible);

    println!("Enter the car type:");
    take_user_input(&mut car_type);

    let car_color = car_color.trim().to_string().capitalize();
    let car_transmission = car_transmission.trim().to_lowercase();
    let car_brand = car_brand.trim().to_string();
    let convertible = convertible.trim().to_lowercase();
    let car_type = car_type.trim().to_string().capitalize();

    let car_transmission = match car_transmission.as_str() {
        "manual" => Transmission::Manual,
        "semiAuto" => Transmission::SemiAuto,
        "automatic" => Transmission::Automatic,
        _ => panic!("The car can only be one of the three options: Manual, SemiAuto or Automatic"),
    };

    let convertible = match convertible.as_str() {
        "yes" => true,
        "no" => false,
        _ => panic!("The car can either be true or false"),
    };

    let car = car_factory(
        car_color,
        car_transmission,
        convertible,
        car_brand,
        car_type,
    );
    println!("Your car has been built with the following specifications:");
    println!(
        "Car brand: {}, Type: {}, Color: {}, Transmission: {:?}, Convertible: {}",
        car.brand, car.car_type, car.color, car.transmission, car.convertible
    );
}

fn car_factory(
    color: String,
    transmission: Transmission,
    convertible: bool,
    brand: String,
    car_type: String,
) -> Car {
    // All new cars always have zero mileage
    Car {
        color,
        transmission,
        convertible,
        brand,
        car_type,
    }
}

fn take_user_input(user_input: &mut String) -> String {
    io::stdin()
        .read_line(user_input)
        .expect("Failed to read the input");

    user_input.to_string()
}
Enter fullscreen mode Exit fullscreen mode

The Never-Ending Geek Odyssey – A Structured Ballet in Progress πŸ“˜πŸŽ΅

As we lower the digital curtains on this balletic adventure, let's savor the fact that Rustic coding is a never-ending dance. Each line of code, every user interaction, and the construction of our virtual car contribute to a structured tale.

May your coding endeavors continue to pirouette through the vast landscapes of syntax! πŸš—πŸ’»βœ¨

RustLang #CodeBallet #CarCrafting #GeekyDialogue #Day15

Top comments (0)