0

so heres my small application:

use std::io;

struct Character {
    name: String,
}

fn build_character(name: String) -> Character {
    Character {
        name: name
    }
}

fn lauch_adventure(adventure: String) {
    match adventure {
        "a" => launch_dark_harvest(),
        _ => println!("Invalid input.")
    }
}

fn launch_dark_harvest() {
    println!("Welcome to Dark Harvest!");
}

fn main() {
    println!("Whats your name?");

    let mut name = String::new();

    io::stdin()
        .read_line(&mut name)
        .expect("What?");

    let character = build_character(name);

    println!("Welcome {}", character.name);
    println!("====[ Adventures ]====");

    println!("a) Dark Heart");
    println!("\nPlease Choose by typing the letter beside the name: ");

    let mut choice = String::new();

    io::stdin()
        .read_line(&mut choice)
        .expect("What?");

    lauch_adventure(choice);
}

the issue is in the launch_adventure method, where it fails to compile because of:

error[E0308]: mismatched types
  --> src/main.rs:15:9
   |
14 |     match adventure {
   |           --------- this expression has type `std::string::String`
15 |         "a" => launch_dark_harvest(),
   |         ^^^ expected struct `std::string::String`, found `&str`

I have tried everything on the internet to no avail, what am I missing? How do I match a user input string to given acceptable params?

SeekingTruth
  • 1,044
  • 2
  • 15
  • 23
  • 1
    Does this answer your question? [How to match a String against string literals in Rust?](https://stackoverflow.com/questions/25383488/how-to-match-a-string-against-string-literals-in-rust) – michaeldel Jul 15 '20 at 03:37
  • no I have tried every one of these, for even doing `.as_str()` compiles, but never matches. @michaeldel – SeekingTruth Jul 15 '20 at 03:38
  • 4
    The fact that is does not matches is another problem. It very probably comes from the fact that [`read_line`](https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line) includes the newline or `EOF` character, you'll have _e.g._ `choice.to_str() == "a\n"`, instead of `"a"`. – michaeldel Jul 15 '20 at 03:44

0 Answers0