Result has no method called “unwrap()”?

If you read the documentation for Result::unwrap, you’ll note that it’s under a little section called: impl<T, E> Result<T, E> where E: Debug This means the methods in that section only exist so long as the given constraints are satisfied. The only reason unwrap wouldn’t exist is that Error doesn’t implement Debug.

Should Cargo.lock be committed when the crate is both a rust library and an executable?

Yes, crates that depend on your library will ignore your Cargo.lock. The Cargo FAQ provides more details: Why do binaries have Cargo.lock in version control, but not libraries? The purpose of a Cargo.lock is to describe the state of the world at the time of a successful build. It is then used to provide deterministic … Read more

How to benchmark memory usage of a function?

You can use the jemalloc allocator to print the allocation statistics. For example, Cargo.toml: [package] name = “stackoverflow-30869007” version = “0.1.0” edition = “2018” [dependencies] jemallocator = “0.5” jemalloc-sys = {version = “0.5”, features = [“stats”]} libc = “0.2” src/main.rs: use libc::{c_char, c_void}; use std::ptr::{null, null_mut}; #[global_allocator] static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; extern “C” fn … Read more

How can an arbitrary json structure be deserialized with reqwest get in Rust?

You can use serde_json::Value. #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let resp = reqwest::get(“https://httpbin.org/ip”) .await? .json::<serde_json::Value>() .await?; println!(“{:#?}”, resp); Ok(()) } You will have to add serde_json to your Cargo.toml file. [dependencies] … serde_json = “1”

Why is type conversion from u64 to usize allowed using `as` but not `From`?

as casts are fundamentally different from From conversions. From conversions are “simple and safe” whereas as casts are purely “safe”. When considering numeric types, From conversions exist only when the output is guaranteed to be the same, i.e. there is no loss of information (no truncation or flooring or loss of precision). as casts, however, … Read more

How do I find the path to the home directory for Linux?

From the home crate. The definition of home_dir provided by the standard library is incorrect because it relies on the $HOME environment variable which has basically no meaning in Windows. This causes surprising situations where a Rust program will behave differently depending on whether it is run under a Unix emulation environment. Neither Cargo nor … Read more

tech