DEV Community

Andrea Giacobino
Andrea Giacobino

Posted on

1

Get the upstream distro name in Rust

The other day we were chatting with a colleague of mine about retrieving the OS name of the distribution. I don't recall exactly the details, but it was something about always getting the actual distribution name and not the upstream one, and he could not sort that out (I have my suspicions that he didn't try very hard).

Fast forward 2 weeks, I'm sipping coffee and installing updates on my laptop and suddenly this appears on the screen:

Alt Text

Oh, that's interesting 🤔, let's google "rust distribution id_like"? LMGTFY

BOOM! first result: https://docs.rs/os-release/0.1.0/os_release/struct.OsRelease.html

Looks good, let's give it a try:

> cargo new guess_os
> cd guess_os 
> cargo add os-release
> vim src/main.go
Enter fullscreen mode Exit fullscreen mode

very well now let's add the code

extern crate os_release;

use os_release::OsRelease;
use std::io;

pub fn main() -> io::Result<()> {
    let release = OsRelease::new()?;
    println!("You say '{}', I say '{}'", release.name, release.id_like);
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

And now the moment of truth 🥁

asciicast

🏆

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay