Similar to Visual Studio, when working with Rust programs, I'd like to press F5 to save the current file, compile it, and run it.
What's a good way to do this?
Similar to Visual Studio, when working with Rust programs, I'd like to press F5 to save the current file, compile it, and run it.
What's a good way to do this?
Below is one approach for Windows. If you're in a rust project, it'll run cargo run
. Otherwise, it'll use rustc
.
(defun rust-save-compile-and-run ()
(interactive)
(save-buffer)
(if (locate-dominating-file (buffer-file-name) "Cargo.toml")
(compile "cargo run")
(compile
(format "rustc %s & %s"
(buffer-file-name)
(file-name-sans-extension (buffer-file-name))))))
(add-hook 'rust-mode-hook
(lambda ()
(define-key rust-mode-map (kbd "<f5>") 'rust-save-compile-and-run)))
Here my version of compile
:
(global-set-key (kbd "<f5>") (lambda ()
(interactive)
(save-buffer)
(setq-local compilation-read-command nil)
(call-interactively 'compile)))
Basically it reuses previously executed compile
command; if you supply with a prefix argument, you are prompted for a new compile
command for future reuse.