Building a guessing cli game

Hello guys, here me with this error.
What should i have done?

use std::io;
use rand::Rng;
use std::cmp::Ordering;

struct Player {
    name: String::new(),
    score_point: i32,
}

const WIN_POINT: u8 = 5;
const LOSS_POINT: u8 = 5;

fn main() {
    let mut user =  Player{name: "", score_point: 0};
    println!("Hello! Welcome to guess the number cli game");
    println!("Enter your username: ");
    io::stdin().read_line(&mut user.name).expect("Failed to read line");
    let secret_number = rand::thread_rng().gen_range(1,100);
    println!("The secret number is: {}", secret_number);
    println!("Guess the number!");
    loop {
        println!("Please input your guess.");

        let mut guess_number = String::new();

        io::stdin().read_line(&mut guess_number)
        .expect("Failed to read line");
        
        let guess_number: u32 = match guess_number.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("You guess: {}",guess_number);

        match guess_number.cmp(&secret_number) {
            Ordering::Less => println!("Your guess is too small!"),
            Ordering::Greater => println!("Your guess is too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}

Compiler error:

error: parenthesized parameters may only be used with a trait
 --> src\main.rs:6:22
  |
6 |     name: String::new(),
  |                      ^^
  |
  = note: #[deny(parenthesized_params_in_types_and_modules)] on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>

error[E0223]: ambiguous associated type
 --> src\main.rs:6:11
  |
6 |     name: String::new(),
  |           ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<std::string::String as Trait>::new`

You must give a type for each field of the struct. However, String::new() is a function call, not a type. You should replace it with plain String for it to work.

3 Likes

Alright thanks that works.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.