State of named function parameters in Rust?

⚓ Rust    📅 2026-04-11    👤 surdeus    👁️ 4      

surdeus

I just reached Item 29 in Item 29: Listen to Clippy - Effective Rust

They have the following nice example:

pub fn circle_area(radius: f64) -> f64 {
    let pi = 3.14;
    pi * radius * radius
}

So what will happen, when user code calls that function with a diameter argument instead of the intended radius? A careful code audit might catch this error, but I think it still can happen and might remain hidden until our moon lander crashed. Using unit or integration tests would not really help catching that user code error. Well, using a circle struct instead of the float parameter would help. But when we use plain parameters, a named call like circle_area(radius = 2.0) or sin(degree_angle=45.0) might help?

[EDIT]

Well, I assume using Enum parameters might help?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed