Rust 转换readline';将ReadlineBytes转换为字符串

Rust 转换readline';将ReadlineBytes转换为字符串,rust,readline,Rust,Readline,我正在尝试创建一个带有readline命令提示符的程序 我用的是板条箱 这是存储库中的示例。(板条箱页面上有存储库链接。) 我正在尝试将s从readline::common::ReadlineBytes转换为std::string::string,这样我就可以像这样对它进行匹配 while let Ok(s) = readline(&prompt){ let command = str::from_utf8(&s.to_bytes()).unwrap(); mat

我正在尝试创建一个带有readline命令提示符的程序

我用的是板条箱

这是存储库中的示例。(板条箱页面上有存储库链接。)

我正在尝试将
s
readline::common::ReadlineBytes
转换为
std::string::string
,这样我就可以像这样对它进行
匹配

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
,您只是返回对它的引用。

我们中的一些人已经编写了readline包装程序。我的更容易用在普通情况下。@SeanPerry,你是说?这看起来简单多了。谢谢
main.rs:18:9: 22:10 error: match arms have incompatible types:
 expected `fn(i32) -> ! {std::process::exit}`,
    found `()`
(expected fn item,
    found ()) [E0308]
"exit"  => std::process::exit,