我尝试在rust中执行this tutorial,到目前为止,我在将C库连接到Rust中时遇到很多问题。

C等效代码:

#include <stdio.h>
#include <stdlib.h>

#include <editline/readline.h>
#include <editline/history.h>

int main(int argc, char** argv) {

  /* Print Version and Exit Information */
  puts("Lispy Version 0.0.0.0.1");
  puts("Press Ctrl+c to Exit\n");

  /* In a never ending loop */
  while (1) {

    /* Output our prompt and get input */
    char* input = readline("lispy> ");

    /* Add input to history */
    add_history(input);

    /* Echo input back to user */
    printf("No you're a %s\n", input);

    /* Free retrived input */
    free(input);

  }

  return 0;
}

到目前为止,我得到了:
extern crate libc;

use std::c_str;

#[link(name = "readline")]
extern {
    fn readline (p: *const libc::c_char) -> *const libc::c_char;
}

fn rust_readline (prompt: &str) -> Option<Box<str>> {
    let cprmt = prompt.to_c_str();
    cprmt.with_ref(|c_buf| {
        unsafe {
            let ret = c_str::CString::new (readline (c_buf), true);
            ret.as_str().map(|ret| ret.to_owned())
        }
    })
}

fn main() {
    println!("Lispy Version 0.0.1");
    println!("Press Ctrl+c to Exit.\n");

// I want to have "history" in Linux of this:
//
//  loop {
//      print!("lispy> ");
//      let input = io::stdin().read_line().unwrap();
//      print!("No you're a {}", input);
//  }

    loop {
        let val = rust_readline ("lispy> ");
        match val {
            None => { break }
            _ => {
                let input = val.unwrap();
                println!("No you're a {}", input);
            }
        }
    }
}

具体地说,我在rust_readline函数中遇到问题,我不太了解内部的工作。

最佳答案

编辑Cargo.toml,输入以下内容:

[dependencies.readline]

git = "https://github.com/shaleh/rust-readline"

代码已更正:
extern crate readline;

fn main() {
    println!("Lispy Version 0.0.1");
    println!("Press Ctrl+c to Exit.\n");

    loop {
        let input = readline::readline("lispy> ").unwrap();
        readline::add_history(input.as_str());
        println!("No you're a {}", input);
    }
}

开心的Lisping :-)。

10-07 20:27