In this answer it is mentioned that for a string doing
s.chars().count()
to get the number of characters is an O(n) operation.
For simple ASCII strings getting the number of bytes using s.len()
works as well. When using a check to make sure that all those bytes are actually ASCII this is probably safe.
I want to know what the complexity of that operation is. It might still have to find the end of the string like in C and be O(n).
I tried to look it up and found the documentation of the std::string::String
,
which applies for appropriate s
. However it doesn't state its complexity. Looking at the source it just does this self.vec.len()
. So we go to look at the vector docs and find that this simply returns a stored length self.len
meaning that this is indeed an O(1) operation.
That was a lot of work though. Now what about the case that s
is a std::str? I tried to do the same but got stuck in this mess.
Is there a more easily accessible ressource for operation complexities in Rust?
Something like this list for Python would be great.