Answer#
An assistant's reply is a proposal. Rust's compiler checks memory safety and types for you, which removes a whole class of mistakes, but it cannot check that the logic does what you meant. Review in layers, from cheapest to most careful.
How to do it#
- Build and test.
cargo buildandcargo test(in Ruststudio: Run > Build, Run > Test: Run All Tests). If the assistant changed behaviour, an existing test should fail; if none does, add one first. - Run Clippy. It flags unidiomatic and suspicious code. Add
-W clippy::unwrap_usedto flag everyunwrap():
fn main() {
let text = std::env::args().nth(1).unwrap();
let n: i32 = text.parse().unwrap();
println!("{n}");
}> cargo clippy -- -W clippy::unwrap_used
warning: used `unwrap()` on an `Option` value
--> src\main.rs:2:16
|
2 | let text = std::env::args().nth(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is `None`, it will panic
= help: consider using `expect()` to provide a better panic message
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#unwrap_used
= note: requested on the command line with `-W clippy::unwrap-used`
warning: used `unwrap()` on a `Result` value
--> src\main.rs:3:18
|
3 | let n: i32 = text.parse().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: if this value is an `Err`, it will panic
= help: consider using `expect()` to provide a better panic message
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#unwrap_used
warning: `rev` (bin "rev") generated 2 warnings- Search for the risky words in the changed files:
unsafe,unwrap,expect,panic!,todo!,unimplemented!, andascasts between number types. In Ruststudio use Edit > Find in Files. - Forbid unsafe where you do not need it. Put
#![forbid(unsafe_code)]at the top ofmain.rsorlib.rs. Anyunsafeblock, including one an assistant adds later, then fails to compile:
#![forbid(unsafe_code)]
fn main() {
let x = 5;
let p = &x as *const i32;
let y = unsafe { *p };
println!("{y}");
}error: usage of an `unsafe` block
--> forbid.rs:6:13
|
6 | let y = unsafe { *p };
| ^^^^^^^^^^^^^
|
note: the lint level is defined here
--> forbid.rs:1:11
|
1 | #![forbid(unsafe_code)]
| ^^^^^^^^^^^
error: aborting due to 1 previous error- Look at
Cargo.toml. Every new dependency is code you now run and trust. Check that the crate exists under that name and is the one you mean, that the version is current, and what it pulls in (cargo tree). Runcargo audit(Tools > Cargo: Security Audit) to check known vulnerabilities; it needscargo-auditinstalled. - Read the logic. Compare edge cases with the specification: empty input, large input, errors. Be careful with anything that touches the file system, the network, passwords or SQL; do not accept it until you can say why it is safe.
Common mistakes#
- Trusting code because it compiles. It may compile and still delete the wrong file.
- Assuming an API exists because the assistant used it. Models can invent functions and crate features; the compiler will then complain, but a nonexistent crate name is also a way to pull in an unintended package.
- Skipping the review of tests the assistant wrote along with the code. They share the same misunderstanding.