In Rust, there are several ways to turn a value into a string. In this article, we will explore a few different ways and discuss what are the most idi

How to to_string in Rust

submited by
Style Pass
2021-05-26 19:30:04

In Rust, there are several ways to turn a value into a string. In this article, we will explore a few different ways and discuss what are the most idiomatic approaches depending on the context you are currently working on.

Personally, I have been quite confused for a while on what’s the best way to implement a “to string” functionality for a given Rust struct. The reason why this has been confusing to me is that there are indeed many ways to do that and they all have different purposes.

I finally decided to do a bit of research to try and demystify this topic a bit and, in this post, I want to share what I learned!

Coming from other languages and not having a deep knowledge of the most idiomatic Rust approaches, the first thing that I generally tend to do when facing a problem is “let’s just make this work for now”.

So, in the spirit of just making things work, the first thing that we can do is to just implement our own to_string() method on a given struct.

Leave a Comment