Skip to content

Review the Rust that your AI agent wrote

For developers with many years of Java or .NET who now review Rust that an AI wrote.

This site teaches you to read Rust, find the usual mistakes in AI-generated Rust, and tell your agent how to fix them.

Your Java Optional chain has a direct Rust equivalent. In Rust, ? means “if there is no value, return now”.

Java

Optional<String> customerEmail(long orderId) {
return findOrder(orderId)
.flatMap(order -> findCustomer(order.customerId()))
.map(Customer::email);
}

Rust

pub fn customer_email(&self, order_id: u64) -> Option<&str> {
let order = self.find_order(order_id)?;
let customer = self.find_customer(order.customer_id)?;
Some(&customer.email)
}

The Rust version has no null and no exception. The return type Option<&str> tells you that there can be no email, and it borrows the email instead of copying it.