ReadlineBytes转换为

ReadlineBytes转换为

我正在尝试创建一个由readline驱动的命令提示符的程序。

我正在使用this crate 。

readline = "0.0.11"

这是存储库中的示例。 (到存储库的链接在 crate 的页面上。)
#![cfg(not(test))]

extern crate readline;

use readline::readline;

use std::ffi::CString;
use std::io::Write;
use std::io;

fn main() {
    let prompt = CString::new("user> ").unwrap();
    let mut stdout = io::stdout();
    while let Ok(s) = readline(&prompt) {
        stdout.write_all(s.to_bytes()).unwrap();
        stdout.write_all(b"\n").unwrap();
    }
    stdout.write_all(b"\n").unwrap();
}

我正在尝试将sreadline::common::ReadlineBytes转换为std::string::String,因此我可以像这样在其上进行match
while let Ok(s) = readline(&prompt){
    let command = str::from_utf8(&s.to_bytes()).unwrap();
    match command {
        "exit"  => std::process::exit,
        "again" => break,
        _ => println!("error")
    }
    println!("{}", command);
}

但我不断收到此错误:
main.rs:18:9: 22:10 error: match arms have incompatible types:
 expected `fn(i32) -> ! {std::process::exit}`,
    found `()`
(expected fn item,
    found ()) [E0308]
main.rs:18         match command {
main.rs:19             "exit"  => std::process::exit,
main.rs:20             "again" => break,
main.rs:21             _ => println!("error")
main.rs:22         }
note: in expansion of while let expansion
main.rs:16:5: 24:6 note: expansion site
main.rs:18:9: 22:10 help: run `rustc --explain E0308` to see a detailed explanation
main.rs:21:18: 21:35 note: match arm with an incompatible type
main.rs:21             _ => println!("error")
                            ^~~~~~~~~~~~~~~~~
note: in expansion of while let expansion
main.rs:16:5: 24:6 note: expansion site
error: aborting due to previous error

最佳答案

比赛武器都必须返回相同的类型。再次查看您的错误消息:

main.rs:18:9: 22:10 error: match arms have incompatible types:
 expected `fn(i32) -> ! {std::process::exit}`,
    found `()`
(expected fn item,
    found ()) [E0308]

您的一个匹配项返回(),另一个返回类型fn(i32) -> ! {std::process::exit} -一个函数。

查看您的代码:
"exit"  => std::process::exit,

您不是在调用exit,而是在返回对它的引用。

关于rust - 将readline的ReadlineBytes转换为字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31798354/

10-09 19:23