nidomiro

Software developer stories

Rust-04: More operators and loops

In the last post we improved the simple program to be extendable with other operators. In this post we will implement more operators and a loop.

Let’s start with the simple task: adding more operators. To support more operators we have to change two parts of the calculator, the parsing and the evaluation. First let’s look into changing the parser. For this we simply add the new operators (-*/) to the regex inside Operation::from_string. The resulting regex will look like this: ^\s*(\d+)\s*([+\-*/])\s*(\d+)\s*$. Now the parser accepts the new operators; first part done.

Continue reading

Rust-03: Improving the simple program

In the last post we created a simple program that would accept an input like 5+5 and calculates the result. In this post we will improve this program.

To prepare for adding more operators we need to improve the input-parsing. Currently, we just split the input-string at the plus-sign. If we want to know the operator, we have to choose another approach.

In this case I chose regex:

#[derive(Debug)]
struct Operation {
    left: i32,
    operator: char,
    right: i32,
}

impl Operation {
    fn from_string(input: &String) -> Option<Operation> {
        let regex = Regex::new(r"^\s*(\d+)\s*([+])\s*(\d+)\s*$").unwrap();

        regex.captures(&input).map(|capture| Operation {
            left: (&capture[1]).parse().unwrap(),
            operator: (&capture[2]).parse().unwrap(),
            right: (&capture[3]).parse().unwrap(),
        })
    }
}

As you can see we have a new thing called struct in the code. If you are coming from a language like Java or Typescript, a struct can be seen as a class without any methods. This struct is called Operation and as the name suggests it holds an operation. The fields left and right are both typed as an i32 or in other words a “signed 32-bit integer”. The field operator is a char since we only use one character for an operator. In rust strings and chars are utf-8, therefore a char can contain one unicode codepoint, not just a byte.

Continue reading

Rust-02: The beginning

At first, I didn’t like Rust. Managing memory myself is something I don’t want to do. After a first look at Rust and setting it aside for a few weeks, it appeared back on my radar. I thought: “This time I’m looking deeper into it”. So I started by reading the Rust Book; and I’m still reading it and find new concepts that are really clever.

One of the first things I discovered is that I do not need to manage memory myself. The majority of the management will be done by the Rust compiler; I just have to follow some Rules. These Rules, called “Ownership”, are a bit confusing to start with, but make more sense, the more you think about them, and the more you use them. In combination with the strict differentiation between mutable and immutable structures, you can write code that is easy to read, easy to think about and resource efficient - a combination, I thought never possible.

Continue reading

Rust-01: Why am I learning Rust?

This is a good question, a very good question. Why am I learning Rust?

To begin with, I programmed in a lot of languages so far: C++, Python, Swift, Java, Kotlin and TypeScript to name a few. All languages have some features I missed when programming in other languages.

Currently, my favorite languages are Kotlin and TypeScript; with Kotlin having the lead. These languages are expressive, and really nice to write. I can express my thoughts in code without having to convert them too much in order to make the compiler happy.

Continue reading

Qt Signals & Slots: How they work

The one thing that confuses the most people in the beginning is the Signal & Slot mechanism of Qt. But it’s actually not that difficult to understand. In general Signals & Slots are used to loosely connect classes. Illustrated by the keyword emit, Signals are used to broadcast a message to all connected Slots. If no Slots are connected, the message “is lost in the wild”. So a connection between Signals & Slots is like a TCP/IP connection with a few exceptions, but this metaphor will help you to get the principle. A Signal is an outgoing port and a Slot is an input only port and a Signal can be connected to multiple Slots.

Continue reading

Should I use Qt containers or the std ones?

If you come from plain vanilla C++, you only know the C++ Standard Library with its containers like std::vector. If you know how to use them, you can accomplish your tasks pretty fast. But if you’re coming from another language, the naming might be a bit odd. The Qt containers offer both, C++ style and Java style naming of methods. So coming from another language the Qt one’s might be easier to use.

Continue reading

Why I love the Qt framework

Everyone that knows me, knows that I love the Qt framework. Before I started programming in C++, Java was my primary programming language. I love the generics (yes, some of you will hate me for that opinion right now) and reflection. During my Java-time I used them very often to increase reusability. But while studying we had to learn C++ and I hated it in the beginning. It felt so old and so stiff compared to Java. But in one lecture we used Qt, and it was even more terrible - in the beginning. Signals, Slots, QWidgets, … it was too much. After working with it for some time it felt better. What I really liked about Qt from the beginning was the Qt Designer. For the first time I was able to have fancy ui’s in C++ and don’t have to bother with a C api. In Java, I had a graphical editor for Swing. So creating ui’s was now as easy as in Java, maybe a bit easier. But the results with Qt are much better and fancier. After some reading and experimentation I began to understand how Signals and Slots work. From now on I really liked Qt. It was awesome. It was so easy to accomplish tasks, and it still is. If you build an application with an ui and some heavy stuff to calculate, or simply said two threads to communicate, it is very easy with Qt. Just use Signals and Slots to communicate between the threads and the Qt framework will deal with all the synchronization stuff (If you use them properly). You can also use Signals and Slots to loosely couple objects. The sender-object that emitted the Signal, does not know where it is going and the receiver-object does not know where it came from. So you easily exchange components, just like with dependency injection. If I write an application and use the C++ standard library and Qt, I can be sure that it will compile and run on all the mayor platforms. A few months ago I switched from Windows to Linux. The reason why I did that can be found here Warum ich zu Linux wechsle if you can read German. I was so excited when I opened the project file of my Qt project. I developed the whole project in Windows and with a click of the “Compile & Run” button it was running under Linux. But the portability is not the only advantage. You have a huge Library with many classes solving different problems. If you want to use for example Network, no problem, just use it. The same goes for JSON support, XML, SQL, Bluetooth, OpenGL and many more. It’s just so convenient, you don’t have to search for a library, try to compile it and link it to your project, Qt does all that for you. So you don’t have to reinvent the wheel every time. At the beginning I mentioned reflection in Java. You can use reflection in Qt with the help of moc (Meta Object Compiler). So every class that inherits from QObject knows what class it is and other information at runtime.

Continue reading
Older posts