Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lexicographic comparison in Rust

Tags:

rust

I am trying to find the smallest lexicographic string in a vector of strings in rust (rust equivalent of python min(strings)). However, I am not sure how I would be able to compare strings lexicographically with rust in the first place. How would I go about doing so?

It seems like comparison with the < or > works, but I am not sure if that is actually true lexicographic comparison.

like image 375
Andrew Avatar asked Sep 12 '25 04:09

Andrew


1 Answers

impl Ord for str

Strings are ordered lexicographically by their byte values. This orders Unicode code points based on their positions in the code charts. This is not necessarily the same as “alphabetical” order, which varies by language and locale. Sorting strings according to culturally-accepted standards requires locale-specific data that is outside the scope of the str type.

like image 94
Alexey Romanov Avatar answered Sep 13 '25 19:09

Alexey Romanov