Skip to content
The Chat Archive
gemini-pro-dev-api· Google Arena Human Preference 55K

Write me in rust a small hello world application that uses an enum to define collected…

  1. User

    Write me in rust a small hello world application that uses an enum to define collected data and a struct to store the data, an implementation for each get/set of the data in the struct, And it should ask "What is your name?, What is your age? and where are you from" in three seperate inputs. The return should then be "Hello , you are old and from the city "

  2. Assistant
    // Define an enum to represent the collected data.
    enum CollectedData {
        Name(String),
        Age(u8),
        City(String),
    }
    
    // Define a struct to store the data.
    struct UserData {
        name: String,
        age: u8,
        city: String,
    }
    
    // Implement the get/set methods for the data in the struct.
    impl UserData {
        fn get_name(&self) -> &str {
            &self.name
        }
    
        fn set_name(&mut self, name: String) {
            self.name = name;
        }
    
        fn get_age(&self) -> u8 {
            self.age
        }
    
        fn set_age(&mut self, age: u8) {
            self.age = age;
        }
    
        fn get_city(&self) -> &str {
            &self.city
        }
    
        fn set_city(&mut self, city: String) {
            self.city = city;
        }
    }
    
    // Define a function to collect the user data.
    fn collect_user_data() -> UserData {
        // Create a new instance of the UserData struct.
        let mut user_data = UserData {
            name: String::new(),
            age: 0,
            city: String::new(),
        };
    
        // Prompt the user for their name.
        println!("What is your name?");
        let name = read_line();
    
        // Set the user's name in the UserData struct.
        user_data.set_name(name);
    
        // Prompt the user for their age.
        println!("What is your age?");
        let age = read_line().parse::<u8>().unwrap();
    
        // Set the user's age in the UserData struct.
        user_data.set_age(age);
    
        // Prompt the user for their city.
        println!("Where are you from?");
        let city = read_line();
    
        // Set the user's city in the UserData struct.
        user_data.set_city(city);
    
        // Return the UserData struct.
        user_data
    }
    
    // Define a function to read a line of input from the user.
    fn read_line() -> String {
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        input.trim().to_string()
    }
    
    // Define a function to greet the user.
    fn greet_user(user_data: UserData) {
        println!(
            "Hello {}, you are {} old and from the city {}",
            user_data.get_name(),
            user_data.get_age(),
            user_data.get_city()
        );
    }
    
    // Define the main function.
    fn main() {
        // Collect the user data.
        let user_data = collect_user_data();
    
        // Greet the user.
        greet_user(user_data);
    }
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.