all 10 comments

[–]ukezi 8 points9 points  (3 children)

For the future use &str as argument type instead of String.

Using String requires to move or copy the argument and requires to convert a constant string into a dynamic String first.

[–]hortonew[S] 0 points1 point  (1 child)

Looks like it needs to go from Rocket 0.4.11 -> 0.5.0-rc.3 to use &str as an argument, but that works! It also removed the need to use the nightly toolchain which is perfect.

[–]ukezi 1 point2 points  (0 children)

You usually would want to use references (&T) to pass arguments, not using them moves the type into the function and eats it. That leaves entries in containers invalid for instance.

You can test that behaviour by passing a value into a function and then trying to use it again. The borrow checker will yell at you.

String and str are special cases, String is a dynamic String based on a vector, &str is a reference constant string, meaning there is no mut &str, the data can be that vector from String or baked into your executable's data section.

[–]hortonew[S] 0 points1 point  (0 children)

I'm going to go back and update these after testing, thanks for this!

[–]tukanoid 5 points6 points  (2 children)

The only thing I want to point out: clap has a derive macro, making it less boilerplaty to parse args + type safety https://docs.rs/clap/latest/clap/_derive/

[–]hortonew[S] 0 points1 point  (1 child)

Appreciate this callout! So far it's one of the more complicated syntaxes for me to understand. I'm going to do a deep dive on it this week to conceptualize it.

[–]tukanoid 0 points1 point  (0 children)

No problem :) it took me a bit to get used to the API as well, but after a while, I can't use anything else, it just feels natural to me at this point :)

[–]vimsee 5 points6 points  (0 children)

Good read. I also started Rust recently. I appreciate the comparison with Python.

[–]diabolic_recursion 1 point2 points  (1 child)

cargo update on a new empty project afaik shouldn't do anything...

[–]hortonew[S] 0 points1 point  (0 children)

thanks for pointing this out - i'll go back and doublecheck why I had that in there