| 1 | # errno [](https://github.com/lambda-fairy/rust-errno/actions/workflows/main.yml) [](https://crates.io/crates/errno) |
| 2 | |
| 3 | Cross-platform interface to the [`errno`][errno] variable. Works on Rust 1.56 or newer. |
| 4 | |
| 5 | Documentation is available at <https://docs.rs/errno>. |
| 6 | |
| 7 | [errno]: https://en.wikipedia.org/wiki/Errno.h |
| 8 | |
| 9 | |
| 10 | ## Dependency |
| 11 | |
| 12 | Add to your `Cargo.toml`: |
| 13 | |
| 14 | ```toml |
| 15 | [dependencies] |
| 16 | errno = "0.3" |
| 17 | ``` |
| 18 | |
| 19 | |
| 20 | ## Comparison with `std::io::Error` |
| 21 | |
| 22 | The standard library provides [`Error::last_os_error`][last_os_error] which fetches `errno` in the same way. |
| 23 | |
| 24 | This crate provides these extra features: |
| 25 | |
| 26 | - No heap allocations |
| 27 | - Optional `#![no_std]` support |
| 28 | - A `set_errno` function |
| 29 | |
| 30 | [last_os_error]: https://doc.rust-lang.org/std/io/struct.Error.html#method.last_os_error |
| 31 | |
| 32 | |
| 33 | ## Examples |
| 34 | |
| 35 | ```rust |
| 36 | extern crate errno; |
| 37 | use errno::{Errno, errno, set_errno}; |
| 38 | |
| 39 | // Get the current value of errno |
| 40 | let e = errno(); |
| 41 | |
| 42 | // Set the current value of errno |
| 43 | set_errno(e); |
| 44 | |
| 45 | // Extract the error code as an i32 |
| 46 | let code = e.0; |
| 47 | |
| 48 | // Display a human-friendly error message |
| 49 | println!("Error {}: {}", code, e); |
| 50 | ``` |
| 51 | |
| 52 | |
| 53 | ## `#![no_std]` |
| 54 | |
| 55 | Enable `#![no_std]` support by disabling the default `std` feature: |
| 56 | |
| 57 | ```toml |
| 58 | [dependencies] |
| 59 | errno = { version = "0.3", default-features = false } |
| 60 | ``` |
| 61 | |
| 62 | The `Error` impl will be unavailable. |