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,
}
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()
}
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();
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,
}
}
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
);
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()
}
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! ππ»β¨
Top comments (0)