DEV Community

Cover image for 30 Days of Rust - Day 11

Posted on

30 Days of Rust - Day 11

Hi folks, today's blog will be short as I have have a game of Padel planned this evening as well as walk through Berlin's Mauerpark to see the Sakura trees 🎾 🌸

Yesterday's questions answered

  • What does the ? operator do?
  • Why would I use the to_owner method?

Today's open questions

  • I was able to set up the debugger following the vs code docs. But I don't seem to get any useful output from the terminal. I'll have to investigate this further
  • Like a language learner too nervous to speak due to fear of making mistakes, perhaps I don't want to write Rust code because I know deep down that it will be bad Rust code. That shouldn't be a reason not to do my own project. In fact, what would make sense would be to compare my own design to those show in tutorials and see where I can improve.
  • On the one hand, tutorials are a fun way to write code and feel good about it. On the other hand, in outsourcing system design to the tutorial righter, you're not going to be making design decisions. Getting used to doing this in Rust will be important for my development, so I'll continue with my own project regardless of how long it takes me.
  • In your rustdoc strings, you may want to include a code example. doctest will test these code snippets to ensure your examples are up-to-date.
  • Return types for unit tests enable you to test using the ? operator. What exactly this does I am not sure, but it looks similar to JavaScript's conditional chain operator.
  • As with an if statement, you can assign the result of a match statement to a variable.

Adding the timing component

This morning I added a rudimentary generic timer function. This introduced the SystemTime module from the time crate. Creating a delta is easy and the Duration struct provides a bunch of helpful methods to get a duration in different time formats.

What's next?

Now that we have a simple way to run tests, I need to start thinking about the CLI module. The CLI will serve not only as a possible entry point into the program. It will also help me understand what kind of data interface I'll need to get back from my timer and subprocess functions. Right now I'm really struggling to visualise how these will be used upstream.

Similarly, within the scope of a function, questions of ownership don't seem particularly relevant. But as soon as you have functions interacting with other functions via some higher level function, or perhaps structs, then who owns what and when can become tricky.

Scope and ownership

Having battled with the compiler to get very simple functions to work, I realise that scope and ownership are concepts that are still very vague to me. In the next few days, I'll take time to read up on those areas in the rust book.

Top comments (0)