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?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
Below is one approach for Windows. If you're in a rust project, it'll run
cargo run. Otherwise, it'll use
(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
(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.