简体   繁体   中英

How do you display a character with ncurses in Rust?

I'm trying to just add a character into the terminal by simply doing

use ncurses::*;
fn main()
{
    initscr();
    addch('#');
    endwin();
}

but I'm getting the following error:

error[E0308]: mismatched types
  --> src/main.rs:15:11
   |
15 |     addch('#');
   |           ^^^ expected `u32`, found `char`

I checked the documentation and it says that it takes a chtype , so I figuered that was just a character? I'm not sure how I am supposed to change a character to a u32 . What is the difference between a char , and a chtype ?

By looking at the source code chtype defined as an alias to u64 (for 64bit platforms) and u32 (for 32 bit platforms) .

#[cfg(feature="wide_chtype")]
pub type chtype = u64;
#[cfg(not(feature="wide_chtype"))]
pub type chtype = u32;

In order to to solve the error, you can type cast # to chtype .

fn main()
{
    initscr();
    addch('#' as chtype);
    endwin();
}

chtype holds more than a character, as described in the ncurses manual page :

      ncurses
           the "normal" library,  which  handles  8-bit  characters.   The
           normal   (8-bit)   library   stores  characters  combined  with
           attributes in chtype data.

           Attributes alone (no corresponding character) may be stored  in
           chtype or the equivalent attr_t data.  In either case, the data
           is stored in something like an integer.

For more information, the waddch manual page elaborates:

   Video attributes can be combined with a character  argument  passed  to
   addch  or  related  functions by logical-ORing them into the character.
   (Thus, text, including attributes, can be  copied  from  one  place  to
   another  using  inch(3x)  and  addch.)   See the curs_attr(3x) page for
   values of predefined video attribute constants  that  can  be  usefully
   OR'ed into characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM