Skip to content

Documentation for abs() on signed integers doesn't mention unsigned_abs() #124152

Closed
@Architector4

Description

@Architector4

Location

https://doc.rust-lang.org/std/primitive.i8.html#method.abs
https://doc.rust-lang.org/std/primitive.i16.html#method.abs
https://doc.rust-lang.org/std/primitive.i32.html#method.abs
https://doc.rust-lang.org/std/primitive.i64.html#method.abs
https://doc.rust-lang.org/std/primitive.i128.html#method.abs

Summary

The documentation mentions that the abs method could cause undesired behavior in case a MIN value of the type is passed.

It could use a mention of the unsigned_abs method after this, which can fit all possible return values at cost of converting the value to an unsigned integer instead, which may have been the behavior one wanted when looking for this function in the documentation.

It doesn't help that alphabetically unsigned_abs is sorted all the way down, while abs sits all the way up. A programmer could end up seeing only abs, and assume that they need to either specially handle MIN value, or use the immediately visible abs_diff function below that, with second parameter set to 0 to achieve the wanted effect. (the latter could use a clippy lint? lol)

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions