[SOLVED] Match with array incompatible


#1

Hello,

I’m trying to use match with an array of str but i’ve got a nice error “match arms have incompatible types

http://hastebin.com/safajuyica.php

I don’t understand why

    if word == &menu[pos] {
        attron(A_REVERSE());
        printw(word);
        attroff(A_REVERSE());
    } else {
        printw(word);
    }

works and not

    match &menu[pos] {
        word => {
            attron(A_REVERSE());
            printw(word);
            attroff(A_REVERSE());
        },
        _ => printw(word),
    }

The same for

match menu[*pos] {
“Exit” => process::exit(0),
“Hangeul” => mvprintw(10, 10, “Hangeul”),
“Vocabulary” => mvprintw(20, 20, “Vocabulary”),
“Grammar” => mvprintw(30, 30, “Grammar”),
_ => {},
}

“Exit”, “Hangeul”, “Vocabulary” and "Grammar are str.
And my array stock str.

expected type i32 found type ()

Why did I expect an i32 variable ?

I’m sorry, I feel dumb to block on a match haha.


#2

looks like printw and mvprintw return i32, and other branches return (). So you need something like

 match &menu[pos] {
        word => {
            attron(A_REVERSE());
            printw(word);
            attroff(A_REVERSE());
        },
        _ => { printw(word); }
    }

The same issue will happen with if, if you don’t add ;:

    if word == &menu[pos] {
        attron(A_REVERSE());
        printw(word);
        attroff(A_REVERSE());
    } else { 
        // Does not compile
        printw(word)
    }

#3

Thanks :slight_smile: I was not seeking in the right place my error.

I don’t understand a thing, why does it need a semi-colon but the process::exit(0) don’t need it ? The same for *pos = 3, and others.


#4

The signature of process exit is

pub fn exit(code: i32) -> !

This ! means that the function never returns, so it can be substituted for any type:

let x: String = ::std::process::exit();

The same works for panic! and unimplemented!() macros.

For assignments like *pos = 3 it’s simpler, because the value of an assignment is just ():

let mut x = 0;
let y: () = (x = 92);

#5

OH I SEE !
Thanks for the explanation, very useful :smiley: