       ((font-lock-ensure-fn (if (fboundp 'font-lock-ensure) 'font-lock-ensure 'font-lock-fontify-buffer)))
     (funcall font-lock-ensure-fn))
   )
+
+(defun rust--before-save-hook ()
+  (when rust-format-on-save (rust-format-buffer)))
 
 ;; Issue #6887: Rather than inheriting the 'gnu compilation error
 ;; regexp (which is broken on a few edge cases), add our own 'rust

  (setq-local parse-sexp-lookup-properties t)
  (setq-local electric-pair-inhibit-predicate 'rust-electric-pair-inhibit-predicate-wrap)
  (add-hook 'after-revert-hook 'rust--after-revert-hook 'LOCAL)

  (when rust-format-on-save
    (rust-enable-format-on-save)))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))

(defun rust-enable-format-on-save ()
  "Enable formatting using rustfmt when saving buffer."
  (interactive)
  (setq-local rust-format-on-save t))

(defun rust-disable-format-on-save ()
  "Disable formatting using rustfmt when saving buffer."
  (interactive)
  (setq-local rust-format-on-save nil))

;; For compatibility with Emacs < 24, derive conditionally
(defalias 'rust-parent-mode
