DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Live reloading in Rust
Nick Taylor
Nick Taylor

Posted on • Originally published at iamdeveloper.com

Live reloading in Rust

I've been learning Rust on and off since last fall. I'm still not proficient in the language as I haven't dedicated as much time to the language as I'd like to. Still, I find pockets of time, like tonight, to dive in a bit.

A quick Google of "rust hot reloading" introduced me to the rust crate, cargo-watch. I installed it as per their instructions cargo install cargo-watch.

From there, I went into a rust project I'm working on and ran the following from the project's root from the command line, cargo watch -x 'run'.

And that was it! I was able to start up my program, and with every change, it reran automatically!

[Finished running. Exit status: 101]
[Running 'cargo run']
   Compiling rusty v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/rusty`
["tobey maguire", "andrew garfield", "tom holland"]
[Finished running. Exit status: 0]
[Running 'cargo run']
   Compiling rusty v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/rusty`
["tobey maguire", "andrew garfield", "tom holland", ""]
[Finished running. Exit status: 0]
[Running 'cargo run']
   Compiling rusty v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/rusty`
["tobey maguire", "andrew garfield", "tom holland", "pete davidson"]
[Finished running. Exit status: 0]
Enter fullscreen mode Exit fullscreen mode

πŸ¦€

Photo by Mackenzie Cruz on Unsplash

Top comments (5)

Collapse
peerreynders profile image
peerreynders

Hot reloading injects the output of the file that changed into the already running application in an attempt to preserve application state (sometimes with undesirable consequences).

Live reloading rebuilds and restarts the entire application from scratch after any file change.

Seems to me we are live reloading here.

Collapse
nickytonline profile image
Nick Taylor Author • Edited on

@peerreynders, that’s true and thanks for commenting!

The title was incorrect so I’ve fixed it, but I definitely meant rerunning in the post.

And that was it! I was able to start up my program, and with every change, it reran automatically!

I’ve updated the post to reflect that. I think I had React on the brain, so was thinking hot reloading. When I initially wrote the post! πŸ˜…

Collapse
__9d07a772ab072a1066546 profile image
zsmatrix62

I always use watch command to run tests, in order to make everything tidy

Collapse
nickytonline profile image
Nick Taylor Author

Nice!

Nice

Collapse
gklijs profile image
Gerard Klijs

Thanks, was missing this in Rust, bit didn't know it was possible.

🌚 Life is too short to browse without dark mode