doc.rust-lang.org
pub fn read_line(&self, buf: &mut String) -> io::Result<usize>
...Examples
use std::io;
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
println!("{n} bytes read");
println!("{input}");
}
Err(error) => println!("error: {error}"),
}
You can run the example one of two ways:
• Pipe some text to it, e.g., printf foo | path/to/executable...
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
fn from_abstract_name<N>(name: N) -> crate::io::Result<SocketAddr>
...Examples
use std::os::unix::net::{UnixListener, SocketAddr};
#[cfg(target_os = "linux")]
use std::os::linux::net::SocketAddrExt;
#[cfg(target_os = "android")]
use std::os::android::net::SocketAddrExt;
fn main() -> std::io::Result<()> {
let addr = SocketAddr::from_abstract_name(b"hidden")?;
let listener = match UnixListener::bind_addr(&addr) {
Ok(sock...
associated_function
std
Stable since 1.70.0
Version 1.100.0-nightly
doc.rust-lang.org
pub trait Index<Idx>
...Nucleotide) -> &Self::Output {
match nucleotide {
Nucleotide::A => &self.a,
Nucleotide::C => &self.c,
Nucleotide::G => &self.g,
Nucleotide::T => &self.t,
}
}
}
let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
assert_eq!(nucleotide_count[Nucleotide::A], 14);
assert_eq!(nucleotide_count[Nucleotide::C], 9);
assert_eq!(nucleotide...
trait
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub const UNIX_EPOCH: SystemTime
...Examples
use std::time::{SystemTime, UNIX_EPOCH};
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
constant
std
Stable since 1.8.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>>
...Examples
use std::process::Command;
let mut child = Command::new("ls").spawn()?;
match child.try_wait() {
Ok(Some(status)) => println!("exited with: {status}"),
Ok(None) => {
println!("status not ready yet, let's really wait");
let res = child.wait();
println!("result: {res:?}");
}
Err(e) => println!("error attempting to wait: {e}"),
}
method
std
Stable since 1.18.0
Version 1.100.0-nightly
doc.rust-lang.org
const UNIX_EPOCH: SystemTime
...Examples
use std::time::SystemTime;
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
associated_constant
std
Stable since 1.28.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn send_to<A>(&self, buf: &[u8], addr: A) -> io::Result<usize>
...This will return an error when the IP version of the local socket does not match that returned from ToSocketAddrs.
See Issue #34202 for more details.
Examples
use std::net::UdpSocket;
let socket = UdpSocket::bind("127.0.0.1:34254").expect("bind should succeed");
socket.send_to(&[0; 10], "127...
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub enum Prefix<'a>
...Examples
use std::path::{Component, Path, Prefix};
use std::path::Prefix::*;
use std::ffi::OsStr;
fn get_path_prefix(s: &str) -> Prefix<'_> {
let path = Path::new(s);
match path.components().next().unwrap() {
Component::Prefix(prefix_component) => prefix_component.kind(),
_ => panic!(),
}
}
assert_eq!(Verbatim(OsStr::new("pictures")),
get_path_prefix(r...
enum
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize>
...Examples
use std::net::UdpSocket;
let socket = UdpSocket::bind("127.0.0.1:34254").expect("bind should succeed");
socket.connect("127.0.0.1:8080").expect("connect should succeed");
let mut buf = [0; 10];
match socket.recv(&mut buf) {
Ok(received) => println!("received {received} bytes {:?}", &buf[..received]),
Err(e) => println...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()>
...use std::io;
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
listener.set_nonblocking(true).expect("set_nonblocking should succeed");
for stream in listener.incoming() {
match stream {
Ok(s) => {
// do something with the TcpStream
handle_connection(s);
}
Err(ref e) if e.kind() == io...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn into_utf8_lossy(self) -> String
...Vec<u8> = b"Hello \xF0\x90\x80World".into();
let (output, had_invalid_utf8) = match String::from_utf8(input) {
Ok(output) => (output, false),
Err(error) => {
// The bytes were not valid UTF-8, but we can still recover a string.
(error.into_utf8_lossy(), true)
}
};
assert_eq!(String::from("Hello �World"), output...
method
alloc
Stable since 1.100.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn splitn<P>(&self, n: usize, pat: P) -> SplitN<'_, P>
...The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.
Iterator behavior
The returned iterator will not be double ended, because it is not efficient to support.
If the pattern allows a reverse search, the rsplitn method can...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn expect(self, msg: &str) -> T
...Instead, prefer to use pattern matching and 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 including the passed message, and the content of the Err.
Examples
let x: Result<u32...
method
core
Stable since 1.4.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn rsplitn<P>(&self, n: usize, pat: P) -> RSplitN<'_, P>
...The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.
Iterator behavior
The returned iterator will not be double ended, because it is not efficient to support.
For splitting from the front, the splitn method can be used...
method
core
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()>
...use std::io::{self, Read};
use std::net::TcpStream;
let mut stream = TcpStream::connect("127.0.0.1:7878")
.expect("Couldn't connect to the server...");
stream.set_nonblocking(true).expect("set_nonblocking should succeed");
let mut buf = vec![];
loop {
match stream.read_to_end(&mut buf) {
Ok(_) => break,
Err...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a>
...format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
In this more complex example, we use format_args! and .debug_set() to build a list of match arms:
use std::fmt;
struct Arm<'a, L, R>(&'a (L, R));
struct Table<'a, K, V>(&'a [(K, V)], V);
impl<'a, L, R...
method
core
Stable since 1.2.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()>
...use std::io;
use std::net::UdpSocket;
let socket = UdpSocket::bind("127.0.0.1:7878").unwrap();
socket.set_nonblocking(true).unwrap();
let mut buf = [0; 10];
let (num_bytes_read, _) = loop {
match socket.recv_from(&mut buf) {
Ok(n) => break n,
Err(ref e) if e.kind() == io::ErrorKind...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub struct VaList<'a>
...A VaList can be used across an FFI boundary, and fully matches the platform's va_list in terms of layout and ABI.
struct
core
Stable since 1.100.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)>
...Examples
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
match listener.accept() {
Ok((_socket, addr)) => println!("new client: {addr:?}"),
Err(e) => println!("couldn't get client: {e:?}"),
}
method
std
Stable since 1.0.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn char_indices(&self) -> CharIndices<'_>
...Some((5, 'y')), char_indices.next());
assert_eq!(Some((6, 'e')), char_indices.next());
assert_eq!(None, char_indices.next());
Remember, chars might not match your intuition about characters:
let yes = "y̆es";
let mut char_indices = yes.char_indices();
assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆...
method
core
Stable since 1.0.0
Version 1.100.0-nightly