• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Variable Type Declaration - Rust

 
Ranch Hand
Posts: 70
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats Ken Youens-Clark on your new book Command-Line Rust: A Project-Based Primer for Writing Rust CLIs.

With some languages it's necessary (or best practice) to define a variable's Type. (e.g. String, Numeric, Integer, etc.)

With Rust CLIs, is it best practice (or required) to specifically define a variable's Type?

Sometimes it's easier to read code if a variable Type is specifically defined initially, and this can help prevent potential confusion or debugging issues.

Using PHP as an example, you can specify a Type of Variable instead of allowing PHP to set this automatically based on how the variable is used. (I have heard people say PHP is a weak typed language, because you are not required to declare variable type)

What are best practices regarding Variable Type Declaration in Rust CLIs?
 
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Geoff,

I am not the author, from whom you want a reply, but I wanted to add a little comment about best practices.
Regarding types display, will want to download and use Rust Analyzer. It is very good at deducting the types you are using and helping you in your programs.

I attached a screenshot to show what I mean  

Of course, those are easy types. An array with u8, i32 types, a String and a slice. But it can be very helpful in more convoluted types.
Screenshot-from-2022-07-15-13-16-38.png
[Thumbnail for Screenshot-from-2022-07-15-13-16-38.png]
 
Geoff McKay
Ranch Hand
Posts: 70
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D.J. Quavern wrote:...  I attached a screenshot to show what I mean    ...



Thanks D.J.
The screenshot helps a lot.
Appreciated.
 
D.J. Quavern
Rancher
Posts: 317
16
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Took the liberty to share another screenshot where type annotation is extremely useful, if it is ok  

(please ignore the colors, I have notably terrible taste)

You can see that the expected return type for example is `<I2C as I2c<u8>>::WriteFuture` (this is an async driver for a magnetometer using I2C protocol) or a `Result<(), CommE>`.

In the first case, it shows that an I2C type is coerced to a WriteFuture, and in the second case that the code will return either a Result with the unit type`()` that all is well, or a `CommE`, a communication error defined by the programmer.
I forked a blocking driver to convert it to async and those types annotations were extremely useful to me as I was not sure of all what was happening in the magnetometer (just needed to make an async version).

Edit: my screenshot is a bit too big, sorry about that.


type.png
screenshot of type annotations with rust analyzer
screenshot of type annotations with rust analyzer
 
Geoff McKay
Ranch Hand
Posts: 70
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D.J. Quavern wrote:Took the liberty to share another screenshot where type annotation is extremely useful, if it is ok   ...



Thanks again D.J.
I have always preferred well structured and well documented code, and I like your format.

Appreciated again!
 
Author
Posts: 22
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geoff McKay wrote:Congrats Ken Youens-Clark on your new book Command-Line Rust: A Project-Based Primer for Writing Rust CLIs.

With some languages it's necessary (or best practice) to define a variable's Type. (e.g. String, Numeric, Integer, etc.)

With Rust CLIs, is it best practice (or required) to specifically define a variable's Type?

Sometimes it's easier to read code if a variable Type is specifically defined initially, and this can help prevent potential confusion or debugging issues.

Using PHP as an example, you can specify a Type of Variable instead of allowing PHP to set this automatically based on how the variable is used. (I have heard people say PHP is a weak typed language, because you are not required to declare variable type)

What are best practices regarding Variable Type Declaration in Rust CLIs?



Sorry it took me so long to find the questions!

In Rust, you can sometimes leave off the type declaration because the compiler can figure it out, but it is sometimes necessary to be explicit as the compiler may choose a type you don't want (e.g., list vs vector). I would say it's best practice to use type annotations as much as possible. Function signatures do require all the type annotations, and I find this makes them easier to understand.

I code a lot in Python, and type declaration is relatively new there. Some people find it abominable to declare types in Python, but I find it far easier to read and use tools like pylint/flake8/mypy to check my code. This means, of course, that it's my responsibility to write and run my tests and those code profilers to ensure I'm doing everything correctly. I prefer that the Rust compiler does all this for me.
 
Geoff McKay
Ranch Hand
Posts: 70
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ken Youens-Clark wrote:... I would say it's best practice to use type annotations as much as possible... Some people find it abominable to declare types in Python, but I find it far easier to read and use tools like pylint/flake8/mypy to check my code....


Thanks Ken,
You confirmed what I was thinking, and I prefer to use type annotations as much as possible for better readability.
 
I like tacos! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic