atc0005

atc0005

Hands-on Rust: "Structs can also be tuples" (page 150, B2.0)

Notice that the syntax is different here. Structs can also be tuples. If you only have one piece of information to store inside a struct, you can replace the named fields with (pub type)—in this case, String. You can then access the contents of the structure just like a tuple - mystruct.0 contains the string.

Aside from less typing, what is the benefit of using this syntax? I’m guessing that it’s supported by Rust, so you’re introducing it here so that the reader is familiar with the approach?

Most Liked

herbert

herbert

Author of Hands-on Rust

Oddly, I didn’t get a notification for this one and just found it in my email! Anyway - you’re exactly right. There’s no internal benefit to the syntax, I wanted to make sure it was included so that readers wouldn’t be surprised when the ran into it elsewhere.

You do sometimes find it combined with methods to provide a somewhat seamless use of types to represent units - with conversions between them. This can be extended using the repr_transparent feature ( https://doc.rust-lang.org/1.26.2/unstable-book/language-features/repr-transparent.html ) to be seamless, but that’s a bit beyond the scope of the book. Once that feature is stabilized into the main language, I think it’ll see a bit more use.

Popular Pragmatic Bookshelf topics Top

yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Latest in Hands-on Rust

Hands-on Rust Portal