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

πŸ†

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more