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]
🦀
Photo by Mackenzie Cruz on Unsplash
Top comments (5)
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.
@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.
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! 😅
I always use watch command to run tests, in order to make everything tidy
Nice!
Thanks, was missing this in Rust, bit didn't know it was possible.