Looking from a code review for Vending Machine CLI implimented in Rust

TLDR; Looking for a code review. I'm new to Rust and coming from Java, Js and Python. I'm curious about the correct way to import code between files, structure my project, and incorporate test driven design.

heres the link to my repo


I'm currently xmas break in a Java bootcamp. The first project we did was a Vending Machine app that I built in Java. While on break I decided to rebuild it in Rust.

I'm also trying to incorporate tests as a part of my coding process in order to drive the design of my code.

I've started building out the first features last night while tinkering with the structs and enums. I have a basic understanding of ownership and how rust handles references, but im also having a hard time organizing and testing my code.

So I'm wondering if someone could look at my repo and give a few notes on my project. Some of the things I want to know is, whats the right way to import "classes" and modules into another file.

For example, in order to use my structs and enums in a separate file for testing, i had to add a bunch of annotations and i dont even know what they do. I just do it because the compiler told me to. I assume its just part of the whole borrowing process.

also when im making modules i have to create a bunch of folders and nest the hell out of my modules. I'm pretty sure I'm doing it wrong though.

And it seems its discouraged to create a separate tests folder so all the tests are included in the module itself. Coming from a Java background, I'm curious if its just a different way of doing OOP.

If anyone wouldnt mind taking the time to give it a look over and offer some notes I'd greatly appreciate it

link to my repo

This looks confusing:

impl Inventory {
    fn new() -> BTreeMap<Slot, Item> {

I am used to that if a struct defines a new function, the function always returns a fresh new copy of the struct. But here the function returns a field of a new struct. In addition, it makes me wonder why we need the struct Inventory if it has no more fields and its new returns just a BTreeMap. In this case, you can write a standalone function like fn newInventory() -> BtreeMap<Slot, Item>.

Edit: typos

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.