Lesson 9.3 · 10 min
Ship Chatterbox
The program works on your machine, from inside the project folder. Shipping means three more things: running the tests that prove the important parts still work, a single file you can run from anywhere, and a few lines of text so someone else knows how. Then v1.0 is done: the whole program, tested and installed.
Adds to ChatterboxVersion 1.0: a README and cargo install.
Eleven tests, one command
Since lesson 7.3, Chatterbox has carried its own tests, run with cargo test, and since lesson 7.4 clippy and rustfmt have kept it tidy. Before you hand a program to anyone, run them one last time. Chatterbox v1.0 has eleven, each a #[test] function inside a #[cfg(test)] mod tests block, so none of them is in the program you ship:
- Six in
command.rspin down whatparsepromises: plain text is chat,/helphas the short form/h,/sayneeds text,/last twois not a count,/danceis unknown, and/remembertakes a name and a fact. - Four in
replier.rspin downEchoandPretend, including theErrfor an empty history from lesson 9.2. - One in
main.rspins downlast_nfrom lesson 8.3, with numbers and with text, because it is generic.
The real model is left out on purpose: a test that needs the internet and a key fails for reasons that have nothing to do with your code. Here is the run:
$ cargo test
Compiling libc v0.2.191
… 112 more lines like this one, one per crate …
Compiling chatterbox v1.0.0 (~/chatterbox)
Finished `test` profile [unoptimized + debuginfo] target(s) in 31.96s
Running unittests src/main.rs (target/debug/deps/chatterbox-d0bf917cd8e453db)
running 11 tests
test command::tests::help_has_a_short_form ... ok
test command::tests::last_needs_a_number ... ok
test command::tests::remember_takes_a_name_and_a_fact ... ok
test command::tests::plain_text_is_chat ... ok
test command::tests::say_needs_text ... ok
test command::tests::unknown_commands_are_errors ... ok
test replier::tests::echo_refuses_long_messages ... ok
test replier::tests::echo_repeats_you ... ok
test replier::tests::nothing_to_reply_to_is_an_error ... ok
test replier::tests::pretend_reads_the_whole_history ... ok
test tests::last_n_takes_from_the_end ... ok
test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sThe tests run in any order (cargo runs them side by side), which is why the list above is not in file order. Keep running cargo test after every change. When you add a /clear command next week and accidentally break /last, a test tells you in a second, before anyone else finds out.
Debug build, release build
Until now you used cargo run. That makes a debug build: it compiles fast and runs slowly, with extra checks switched on, and the file lives in target/debug/. When you are ready to hand the program to someone, you ask for the release build, which takes longer to compile and runs faster. Either way the result is a binary: the finished program as a single file the computer can run.
$ cargo build --release Compiling libc v0.2.191 Compiling proc-macro2 v1.0.107 Compiling quote v1.0.47 Compiling unicode-ident v1.0.27 … 107 more lines like these, one per crate … Compiling hyper-rustls v0.27.10 Compiling reqwest v0.13.5 Compiling chatterbox v1.0.0 (~/chatterbox) Finished `release` profile [optimized] target(s) in 53.61s $ ls -lh target/release/chatterbox -rwxr-xr-x@ 1 you wheel 6.4M Oct 21 15:28 target/release/chatterbox
That one file, target/release/chatterbox, is the whole program. You can copy it to another machine of the same kind and run it there with nothing else installed: no Rust, no cargo, no crates. It is a few megabytes because the HTTP client, the code for secure connections and the JSON code are all inside it. Those Compiling lines are the crates Chatterbox uses and the crates they use, each compiled once.
The v1.0.0 in the output is the version field in Cargo.toml. The "v1.0" Chatterbox prints in its banner is a separate string in main. This lesson changes both, and keeping them in step is your job: nothing checks it.
Install it
Running ./target/release/chatterbox works, but only from this folder. cargo install --path . --locked builds the release binary and copies it into cargo's own bin folder, ~/.cargo/bin, which rustup already added to your PATH (the list of folders your terminal searches when you type a command). After that, chatterbox is a command like any other, in any folder. The --locked tells cargo to use the exact crate versions written in Cargo.lock, the ones your tests just passed with, instead of looking up the newest ones again. Without it, cargo install ignores that file and may build with crate versions your tests never saw.
The session below adds --root ../inst, which installs into a scratch folder instead, so the recording did not touch a real ~/.cargo/bin. That is also why cargo warns about PATH and why the program is run by its path. Without --root, you would type just chatterbox.
$ cargo install --path . --locked --root ../inst Installing chatterbox v1.0.0 (~/chatterbox) Updating crates.io index Finished `release` profile [optimized] target(s) in 0.35s Installing ~/inst/bin/chatterbox Installed package `chatterbox v1.0.0 (~/chatterbox)` (executable `chatterbox`) warning: be sure to add `~/inst/bin` to your PATH to be able to run the installed binaries $ cd ../elsewhere $ printf 'Sam\nhello!\n/quit\n' | ../inst/bin/chatterbox Chatterbox v1.0 (replies by: echo) What should I call you? Hi Sam! I'm Chatterbox. Ask me anything. bot: you said: hello! bot: bye, Sam! messages so far: 1 saved 2 messages to chatterbox.json $ ls chatterbox.json
The install took under a second because the release build from the step before was reused: same code, same crate versions, so there was nothing to compile. And notice where chatterbox.json landed: in the folder the program was run from, as store.rs promised. Each folder you run it in has its own history.
Write it down
A program nobody knows how to use is half shipped. A README is a plain text file named README.md in the project folder. It answers: what is it, how do I run it, what can it do, and what does it need? Chatterbox's is README.md in the listing further down (all green, because it is new). It has a title and one sentence on what the program is, then five short sections:
- Run it:
cargo run, and that it asks your name, then echoes you if no model is set. - Commands: one line per command, the same list
HELPprints, and where the history is saved (chatterbox.json, in the folder you run it from). - Talk to a real model: the three
exportlines from lesson 9.2. - Install it and Test it: the commands from this page.
In the file, the # lines are headings and the indented lines are shown as code: that is the Markdown format, which sites like GitHub display nicely. The README is also where you keep a promise you cannot check with a test: if you add a command, add its line here.
Chatterbox v1.0
Here is Chatterbox v1.0, every file exactly as the download has it; files this lesson did not touch are folded. The green lines are this lesson’s changes: the version in Cargo.toml (0.9.0 to 1.0.0), the VERSION pair in main.rs and the new README.md. No code changed: v1.0 is the program from lesson 9.2, tested, documented and installed.
Press Play with it yourself to run these same files in the page, compiled to WebAssembly. One swap: a page cannot open a network connection, so a stand-in replaces reqwest and /model real reports the missing setting. /quit saves chatterbox.json in the page, so the next run remembers you.
src/command.rsunchanged
src/message.rsunchanged
src/replier.rsunchanged
src/store.rsunchanged
Sam hello! /remember pizza extra cheese /recall pizza /model pretend can you hear me? /stats /history /quit
Chatterbox v1.0 (replies by: echo) What should I call you? Hi Sam! I'm Chatterbox. Ask me anything. bot: you said: hello! bot: got it: pizza is extra cheese bot: pizza is extra cheese bot: replies now come from pretend bot: (pretend model) I read all 3 messages. You last said: can you hear me? bot: your top words: can (1), hear (1), hello! (1) 1. you: hello! 2. bot: you said: hello! 3. you: can you hear me? 4. bot: (pretend model) I read all 3 messages. You last said: can you hear me? bot: bye, Sam! messages so far: 2 saved 4 messages to chatterbox.json
Download this version (zip)Unzip, then cargo run inside the folder.
Five files, one job each:
message.rs: theMessagestruct from chapter 5, with its methods, and the serde derive from lesson 9.1.command.rs: what a line of input means (Command, an enum from chapter 5, with the/h,/qand/exitaliases from lesson 5.5) and what can be wrong with it (CommandErrorand?, chapter 7), plus six tests.replier.rs: who answers. TheRepliertrait from lesson 8.4, the echo, the pretend model and the real one with its HTTP call from lesson 9.2, plus four tests.store.rs: the history on disk (lesson 8.2), as JSON (lesson 9.1).main.rs: the conversation. It reads a line, asksparsewhat it means, and does it, from remembering facts in aHashMapto asking the replier. When the loop ends it saves.
Swap the replier and nothing else moves. Add a command and only command.rs, one arm in main, HELP and the README change. That is the point of everything since chapter 4: small pieces with clear owners and clear promises.
Next: chapter 10
That is the end of chapter 9, and v1.0 is shipped. Use it with a real model for a while and two things start to grate: a long answer appears all at once, after a wait with nothing on screen, and the only way to stop one is Ctrl-C, which ends the whole program without saving. Chapter 10 fixes both: answers that stream in as the model writes them, and a Ctrl-C that stops the answer instead of Chatterbox.
Say it out loud, then reveal
What is the difference between <code>cargo run</code> and <code>cargo build --release</code>?Reveal
What does <code>cargo install --path . --locked</code> do?Reveal
Which part of the final program would change if you wanted a different model provider?Reveal
Model (URL, model name and key) change, as long as the provider speaks chat completions.What does <code>#[cfg(test)]</code> do?Reveal
cargo test, so the tests are not part of the program you ship.That is lesson 9.3. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
How do I build a Rust program for release?
Run cargo build --release in the project folder. The optimised binary is written to target/release/.
How do I install my own Rust program?
Run cargo install --path . --locked in the project folder. The program is built with the crate versions in Cargo.lock and copied into the cargo bin folder, which is normally already on your PATH.
How do I run the tests in a Rust project?
Run cargo test in the project folder. It builds a test version of the program and runs every function marked #[test], then prints which passed and which failed.
Can I give the binary to someone else?
Yes, as long as they use the same kind of machine (same operating system and processor). The file needs no Rust installation to run.
