Rust development with emacs (solved)


#1

Hello
I installed rust and rustrc successfully and now I want to run my first rust program in emacs. I use rust-mode in emacs but I have a problem with it. after run this simple program an error message appear.

could not find `Cargo.toml`

Please guide me. What should I do.

More details in picture:


#2

Might be an obvious question, but: did you write a Cargo manifest?


#3

I created a new directory with hello_world name:

cargo new hello_world

and I opened the main.rs file and I added my codes. after running this file a message appeared in emacs:

Symbol’s function definition is void: alist-get

Is there a way to change default location of cargo?


#4

This sounds like an issue with emacs, which I don’t use, so I can’t comment. Sorry. On the bright side, at least we’ve eliminated one issue. :stuck_out_tongue:


#5

Thanks for your reply :slight_smile:
I could run my first program with Geany.
Yes, you’re right, this issue is related to Emacs…


#6

Hello again! I solved this problem with cargo.el emacs package!
If anyone has the same problem, follow the steps below:
Install cargo.el with this command: M-x package-install cargo
When installation is finished, add below command to .emacs.d/init.el:

(add-hook 'rust-mode-hook 'cargo-minor-mode)

Shortcuts to render your Rust program:

 `C-c C-c C-b` ->  build
 `C-c C-c C-r` ->  run
 `C-c C-c C-t` ->  test

P.S) rust-mode enables different features like syntax highlighting and indentation: M-x package-install rust-mode

And now emacs is ready for Rust development :hugs: cheers :wink:


#7

I have this simple setup in my .emacs file to play around with rust.

(use-package racer
  :ensure t
  :init
  (add-hook 'rust-mode-hook #'racer-mode)
  (add-hook 'racer-mode-hook #'eldoc-mode)
  (add-hook 'racer-mode-hook #'company-mode)

  (add-hook 'racer-mode-hook #'company-mode)

  (require 'rust-mode)
  (define-key rust-mode-map (kbd "TAB") #'company-indent-or-complete-common)
  (setq company-tooltip-align-annotations t))

Granted I have the ‘use-package’ package installed as well, but I think you can see what I have going on here. I will probably end up adding a few functions to compile rust code similar to what I have set up for c code. Just not worth setting up for me yet.

;; Functionality of running a quick compile command
;; Changed for windows to use the of using build.bat
;; change compile proc to find makefile in parent directory
(defun desperately-compile ()
“Traveling up the path, find a Makefile and `compile’.”
(interactive)
(when (locate-dominating-file default-directory “build.bat”)
(with-temp-buffer
(cd (locate-dominating-file default-directory “build.bat”))
(compile “build.bat”))))

(global-set-key (kbd “C-x C-m”) 'desperately-compile)
(put 'erase-buffer 'disabled nil)

As a final note. The Racer package requires that you have rust nightly installed on your computer.
Emacs Racer