Happy Friday folks, I've got some time to myself this morning to make some progress on my CLI project. I'm currently en route to Munich from Berlin to attend my company [at]'s spring party. Don't worry I'll make sure to not overdo it so that I can post again tomorrow.
?provides a short-hand way of handling
Option. If a function or method returns a
Option, then chaining that call with
?will cause the function to return if an
Noneis yielded, respectively. These are both wrapped in their respective containers. If the
Optionyields a value, then the function will continue to execute.
to_ownedis a required method of the
ToOwnedtrait. It is very similar to
clone. It basically takes a borrowed value and turns it into an owned one. I'm not sure what the implications for your program would be in this case. I guess I really need to look more at ownership 😀
- What does
- What exactly is an implementation type?
- What are Send and Sync?
derivesimilar to class inheritance?
As you may have noticed from my last couple of posts, I've been struggling to make proper progress on my project. This is in part due to my inability to code quickly in Rust. Now as I reflect, however, I also realise that it would have probably been wiser to start with the CLI tool.
The CLI is the point in our program where we define our data models that will be received from and fed back to the user. Initially I feared that starting with the CLI tool could lead to a coupling between the different parts of my code. I have worked on projects where third-party CLI tools were so deeply embedded in a system that you could only run and test Python code from through the CLI (I know,
pytest is a CLI tool but that's not what I mean here). I think I was overcompensating here and ran into another problem: designing code without a clearly defined data model at hand. Consider this extract from the CLI section of the Rust book:
"In Rust, it is common to structure programs around the data they handle, so this way of looking at CLI arguments fits very well."
I was able to setup a very simple CLI model that accepts a command string as an argument. I used the
derive approach from the
clap library. This worked well to get something simple working. Where I'm struggling right now is understanding how I can split my command string into CLI arguments. The shlex library should solve this problem, but it doesn't support powershell parsing. One solution could be to use the shell when running on Windows and an argument approach when running on unix.