1

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?

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.