One thing
In Rust, each value has one owner. Passing a String into a function moves ownership unless you pass a reference with &.
For your CLI, this means you should borrow text when you only need to read it. Own it when you need to store it or change it.
Why this matters
A CLI reads arguments, file paths, task names, notes, or amounts. These are often Strings. If you move them too early, you cannot use them later for printing, matching, or saving.
This is not Rust being difficult. It is Rust making memory rules visible. Once you see moves and borrows, compiler errors become useful directions.
Move versus borrow
This version moves name into print_name. After that, main cannot use name again.
fn print_name(name: String) {
println!("Project: {}", name);
}
fn main() {
let name = String::from("fees-tracker");
print_name(name);
println!("Running {}", name); // error: name was moved
}Borrow instead
If the function only reads the value, take &str or &String. Prefer &str for text input because it works with String and string literals.
fn print_name(name: &str) {
println!("Project: {}", name);
}
fn main() {
let name = String::from("fees-tracker");
print_name(&name);
println!("Running {}", name); // works
}Choose the parameter
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Cloning too early
Beginners often add .clone() everywhere to silence errors. It works, but it hides the real ownership issue and can waste memory.
Clone only when you truly need two owned copies. In most CLI code, a reference is enough for printing, checking, parsing, or matching.
let command = String::from("add");
// Usually avoid this:
show_command(command.clone());
// Prefer this when show_command only reads:
show_command(&command);Use AI carefully
When an AI tool gives you Rust code, ask it: "Which functions should take &str instead of String, and why?" Then check the answer against the compiler.
Do not accept fixes that only add clone everywhere. Ask for the smallest ownership change that keeps the value usable later.
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done