pho ramen soba

Friday, 29 March 2024

rust code snippet — referencing toml values within a rust application

I have been working with Rust for a few years now, primarily during my retirement. I've authored a few personal tools with it, and I've come to prefer working with it rather than the latest C++ compilers from GCC or Clang/LLVM. I find it expressive i…
Read on blog or Reader
Site logo image Arcane Science Lab Read on blog or Reader

rust code snippet — referencing toml values within a rust application

whbeebe

March 29

Rust code snippet
Listing 1: Rust code referencing some TOML data

I have been working with Rust for a few years now, primarily during my retirement. I've authored a few personal tools with it, and I've come to prefer working with it rather than the latest C++ compilers from GCC or Clang/LLVM. I find it expressive in ways that C++ isn't. But Rust, like C++, isn't perfect and it does have its quirks.

TOML CODE
Listing 2: Opening TOML section

One of those quirks I first encountered involved an easy way to access elements in the Cargo.toml file within the Rust application itself. As I moved deeper into Rust I turned to a number of books on the language. One of those books showed how Rust worked by re-writing certain Unix/Linux command line tools in Rust. A subset of that book was command line argument processing, such as passing a version flag to the utility to check the version of the tool. I quickly noticed that the example Rust code duplicated information that was also defined in the toml file, such as the application's name, version, author(s), and description. From decades of prior software experience the last thing you want to do is duplicate information/data between different files. Instead you want to define data in one location, in one file, and then reference that everywhere in the application and system where you need it. After a bit of searching I found one way to reference that toml information; see listing 1 above referencing the first four definitions in listing 2's [package] section.

There's probably a better way to reference toml information, but I used Rust's built-in environment macros to reference this data because I learned that when you run a Rust application, that toml information is placed in the running Rust application's environment as environmental variables. For me this is good enough.

By the way, this post marks my use of the Visual Studio Code plugin CodeSnap to create a visual snapshot of code. I've grown tired of trying to use WordPress' built-in code support. The reference page for how to use it has disappeared, and even when I have used it, I can't get decent code syntax highlighting. So this is probably what I'll use from now on.

Links

Rust Environmental Variables -- https://doc.rust-lang.org/cargo/reference/environment-variables.html

Comment
Like
You can also reply to this email to leave a comment.

Arcane Science Lab © 2024. Manage your email settings or unsubscribe.

WordPress.com and Jetpack Logos

Get the Jetpack app

Subscribe, bookmark, and get real-time notifications - all from one app!

Download Jetpack on Google Play Download Jetpack from the App Store
WordPress.com Logo and Wordmark title=

Automattic, Inc. - 60 29th St. #343, San Francisco, CA 94110  

at March 29, 2024
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Must-Try July Recipes ✨

Fan favorite and seasonal recipes to try this month: smoothie bowl, summer pasta, noodle salad, veggie-rich soup, no-bake bars & a blue...

  • Flourless pancakes
    Ingredients: 2 large ripe bananas 4 large eggs 1/2 teaspoon baking pow...
  • I’m listening…..
    ...
  • Get Epicurious + Bon Appétit for just $30
      Brought to you by Bon Appétit ...

Search This Blog

  • Home

About Me

phoo, ramen, soba
View my complete profile

Report Abuse

Blog Archive

  • July 2026 (7)
  • June 2026 (18)
  • May 2026 (18)
  • April 2026 (20)
  • March 2026 (16)
  • February 2026 (13)
  • January 2026 (15)
  • December 2025 (17)
  • November 2025 (18)
  • October 2025 (21)
  • September 2025 (19)
  • August 2025 (28)
  • July 2025 (25)
  • June 2025 (28)
  • May 2025 (34)
  • April 2025 (36)
  • March 2025 (39)
  • February 2025 (36)
  • January 2025 (43)
  • December 2024 (46)
  • November 2024 (51)
  • October 2024 (44)
  • September 2024 (1172)
  • August 2024 (1572)
  • July 2024 (1413)
  • June 2024 (1289)
  • May 2024 (1362)
  • April 2024 (1472)
  • March 2024 (1827)
  • February 2024 (2413)
  • January 2024 (2936)
  • December 2023 (2135)
  • November 2023 (1639)
  • October 2023 (1285)
  • September 2023 (918)
  • August 2023 (864)
  • July 2023 (795)
  • June 2023 (800)
  • May 2023 (796)
  • April 2023 (754)
  • March 2023 (649)
  • February 2023 (736)
  • January 2023 (1159)
  • December 2022 (968)
  • November 2022 (921)
  • October 2022 (852)
  • September 2022 (708)
  • August 2022 (766)
  • July 2022 (877)
  • June 2022 (684)
  • May 2022 (716)
  • April 2022 (698)
  • March 2022 (781)
  • February 2022 (734)
  • January 2022 (955)
  • December 2021 (1387)
  • November 2021 (3002)
  • October 2021 (3213)
  • September 2021 (3188)
  • August 2021 (3232)
  • July 2021 (1697)
Powered by Blogger.