4

Using the Emacs mode for Cargo, I'm able to build and run Rust code inside Emacs. However, the terminal that opens when executing cargo-process-run does not accept user input.

For example, when running

use std::io;
fn main() {
  let mut input = String::new();

  io::stdin()
      .read_line(&mut input)
      .expect("Failed to read line.");
}

the terminal will hang without being capable of receiving keyboard input.

I've read that a similar problem can occur when executing compile, but this could somehow be resolved by starting in comint-mode. Unfortunately, I have not found a way to translate that solution to this scenario.

How can I resolve this?

Safron
  • 171
  • 7

2 Answers2

3

After some research, I was able to solve it by adding this to my init.el:

(with-eval-after-load 'rust-mode
  (define-key rust-mode-map (kbd "C-r") 'my-cargo-run))
(defun my-cargo-run ()
  "Build and run Rust code."
  (interactive)
  (cargo-process-run)
  (let (
      (orig-win (selected-window))
      (run-win (display-buffer (get-buffer "*Cargo Run*") nil 'visible))
    )
    (select-window run-win)
    (comint-mode)
    (read-only-mode 0)
    (select-window orig-win)
  )
)

The function my-cargo-run calls cargo-process-run, enables comint-mode and disables the read-only mode of the terminal. The first two lines bind the function to C-r.

I've asked the author of cargo.el to include this in the package.

Safron
  • 171
  • 7
1

Another possibility:

(defun rust-compile-send-input ()
  "Read string from minibuffer and send it to the rust process of the current
buffer."
  (interactive)
  (let ((input (read-from-minibuffer "Send input to rust process: "))
        (proc (get-buffer-process (current-buffer)))
        (inhibit-read-only t))
    (process-send-string proc (concat input "\n"))))
bertfred
  • 1,699
  • 1
  • 11
  • 23