Convert String to SocketAddr

from_str was renamed to parse and is now a method you can call on strings: use std::net::SocketAddr; fn main() { let server_details = “127.0.0.1:80”; let server: SocketAddr = server_details .parse() .expect(“Unable to parse socket address”); println!(“{:?}”, server); } If you’d like to be able to resolve DNS entries to IPv{4,6} addresses, you may want to … Read more

What is the recommended directory structure for a Rust project?

Cargo, the official package manager for Rust, defines some conventions regarding the layout of a Rust crate: . ├── Cargo.lock ├── Cargo.toml ├── benches │ └── large-input.rs ├── examples │ └── simple.rs ├── src │ ├── bin │ │ └── another_executable.rs │ ├── lib.rs │ └── main.rs └── tests └── some-integration-tests.rs Cargo.toml and Cargo.lock are … Read more

What’s the most efficient way to insert an element into a sorted vector?

The task consists of two steps: finding the insert-position with binary_search and inserting with Vec::insert(): match v.binary_search(&new_elem) { Ok(pos) => {} // element already in vector @ `pos` Err(pos) => v.insert(pos, new_elem), } If you want to allow duplicate elements in your vector and thus want to insert already existing elements, you can write it … Read more

Rust Chrono parse date String, ParseError(NotEnough) and ParseError(TooShort)

When converting a String into a Chrono object you have to know what parts the input format of the string has. The parts are: Date, Time, TimeZone Examples: “2020-04-12” => Date = NaiveDate “22:10” => Time = NaiveTime “2020-04-12 22:10:57” => Date + Time = NaiveDateTime “2020-04-12 22:10:57+02:00” => Date + Time + TimeZone = … Read more

What does “manifest path is a virtual manifest, but this command requires running against an actual package” mean?

Your Cargo.toml is a virtual manifest. In workspace manifests, if the package table is present, the workspace root crate will be treated as a normal package, as well as a workspace. If the package table is not present in a workspace manifest, it is called a virtual manifest. When working with virtual manifests, package-related cargo … Read more