doc.rust-lang.org
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
...The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert...
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn var<K>(key: K) -> Result<String, VarError>
...Examples
use std::env;
let key = "HOME";
match env::var(key) {
Ok(val) => println!("{key}: {val:?}"),
Err(e) => println!("couldn't interpret {key}: {e}"),
}
function
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub const fn unwrap(self) -> T
...Instead, prefer to use pattern matching and handle the None case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default. In functions returning Option, you can use the ? (try) operator.
Panics
Panics if the self value equals None.
Examples
let x = Some("air");
assert_eq!(x.unwrap...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub const fn map<U, F>(self, op: F) -> Result<U, E>
...let line = "1\n2\n3\n4\n";
for num in line.lines() {
match num.parse::<i32>().map(|i| i * 2) {
Ok(n) => println!("{n}"),
Err(..) => {}
}
}
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn duration(&self) -> Duration
...Examples
use std::thread::sleep;
use std::time::{Duration, SystemTime};
let sys_time = SystemTime::now();
sleep(Duration::from_secs(1));
let new_sys_time = SystemTime::now();
match sys_time.duration_since(new_sys_time) {
Ok(_) => {}
Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
}
method
std
Stable since 1.8.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn remove<Q>(&mut self, value: &Q) -> bool
...The value may be any borrowed form of the set's element type, but the ordering on the borrowed form must match the ordering on the element type.
Examples
use std::collections::BTreeSet;
let mut set = BTreeSet::new();
set.insert(2);
assert_eq!(set.remove(&2), true);
assert_eq!(set...
method
alloc
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
...The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1...
method
std
Stable since 1.27.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
...The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.
Examples
use std::collections::BTreeMap;
let mut map = BTreeMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1...
method
alloc
Stable since 1.45.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn unwrap(self) -> T
...Instead, prefer to use the ? (try) operator, or pattern matching to handle the Err case explicitly, or call unwrap_or, unwrap_or_else, or unwrap_or_default.
Panics
Panics if the value is an Err, with a panic message provided by the Err's value.
Examples
Basic usage:
let x...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn split_paths<T>(unparsed: &T) -> SplitPaths<'_>
...Examples
use std::env;
let key = "PATH";
match env::var_os(key) {
Some(paths) => {
for path in env::split_paths(&paths) {
println!("'{}'", path.display());
}
}
None => println!("{key} is not defined in the environment.")
}
function
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn incoming(&self) -> Incoming<'_>
...TcpStream) {
//...
}
fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:80")?;
for stream in listener.incoming() {
match stream {
Ok(stream) => {
handle_connection(stream);
}
Err(e) => { /* connection failed */ }
}
}
Ok(())
}
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
fn source(&self) -> Option<&dyn Error + 'static>
...SuperErrorSideKick })
}
fn main() {
match get_super_error() {
Err(e) => {
println!("Error: {e}");
println!("Caused by: {}", e.source().unwrap());
}
_ => println!("No error"),
}
}
method
core
Stable since 1.30.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct IntoInnerError<W>
...let stream = match stream.into_inner() {
Ok(s) => s,
Err(e) => {
// Here, e is an IntoInnerError
panic!("An error occurred");
}
};
struct
alloc
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct WriterPanicked
...io::Result<()> { panic!() }
}
let mut stream = BufWriter::new(PanickingWriter);
write!(stream, "some data").unwrap();
let result = catch_unwind(AssertUnwindSafe(|| {
stream.flush().unwrap()
}));
assert!(result.is_err());
let (recovered_writer, buffered_data) = stream.into_parts();
assert!(matches!(recovered_writer, PanickingWriter));
assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");
struct
alloc
Stable since 1.56.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct PoisonError<T>
...Examples
use std::sync::{Arc, Mutex};
use std::thread;
let mutex = Arc::new(Mutex::new(1));
// poison the mutex
let c_mutex = Arc::clone(&mutex);
let _ = thread::spawn(move || {
let mut data = c_mutex.lock().unwrap();
*data = 2;
panic!();
}).join();
match mutex.lock() {
Ok(_) => unreachable!(),
Err(p_err) => {
let data...
struct
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn var_os<K>(key: K) -> Option<crate::ffi::OsString>
...Examples
use std::env;
let key = "HOME";
match env::var_os(key) {
Some(val) => println!("{key}: {val:?}"),
None => println!("{key} is not defined in the environment.")
}
If expecting a delimited variable (such as PATH), split_paths can be used to separate items.
function
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct Utf8Error
...FnMut(&str) {
loop {
match std::str::from_utf8(input) {
Ok(valid) => {
push(valid);
break
}
Err(error) => {
let (valid, after_valid) = input.split_at(error.valid_up_to());
unsafe {
push(std::str::from_utf8_unchecked(valid))
}
push("\u{FFFD}");
if let Some(invalid_sequence_length) = error.error_len() {
input = &after_valid...
struct
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn code(&self) -> Option<i32>
...Examples
use std::process::Command;
let status = Command::new("mkdir")
.arg("projects")
.status()
.expect("mkdir command should execute successfully");
match status.code() {
Some(code) => println!("Exited with status code: {code}"),
None => println!("Process terminated by signal")
}
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
primitive char
...The gap in valid char values is understood by the compiler, so in the below example the two ranges are understood to cover the whole range of possible char values and there is no error for a non-exhaustive match.
let c: char = 'a';
match c {
'\0' ..= '\u{D7FF}' => false...
primitive
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
primitive char
...The gap in valid char values is understood by the compiler, so in the below example the two ranges are understood to cover the whole range of possible char values and there is no error for a non-exhaustive match.
let c: char = 'a';
match c {
'\0' ..= '\u{D7FF}' => false...
primitive
std
Stable since 1.0.0
Version 1.100.0-nightly