Take parameters by value instead of reference #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @dpeachpeach,
This PR demonstrates several optimizations for the
login
function you might consider. I can extend them to other functions in the library too if you like.The first is to replace
username: &str, password: &str
withusername: String, password: String
since the function requires ownership of the strings anyway. In most cases, this will reduce the number of string allocations by one. Consider:This allocates the username and password strings twice (once from the environment variables and then again in the function call). Basically, it puts the onus of cloning
username
andpassword
onto the caller of the function, so they caller gets to decide if they need to be cloned or not.The other optimization changes
login_url: &str
tologin_url: String
. Again, it reduces the number of string allocations by one. We construct aString
with the call toformat!
but then pass a reference with.post(login_url)
. Thenreqwest
has to make a copy. If we construct aString
and then pass that instead,reqwest
does not need to make a copy.These are admittedly micro-optimizations but I think it's good practice for a library.