www.ncameron.org
...Pattern matching
The goal with the pattern matching API is to allow procedural macros to operate on tokens in the same way as macros-by-example. The pattern language is thus the same as that for macros-by-example.
There is a single macro, which I propose calling matches. Its...
News & Blog Posts
2016-01-25
~16 min read
llogiq.github.io
...Call into the element
chain recursively to remove it if found
Simply removing the existing code and coding up a match block with the three
cases led to much easier code and a performance win. Apparently this time the
compiler didn’t see through the second match and the extra...
News & Blog Posts
2018-08-07
~4 min read
rust-analyzer.github.io
#9180 fix some IDE functionality inside attribute macros.
#9239 fix coercion in match with expected type.
#9182 don’t complete derive macros as function-like macros.
#9187 fix edge case in import granularity guessing.
#9186 prefer attr macros in "Expand macro recursively".
#9191 don’t descend into MacroCall TokenTree delimiters...
Project/Tooling Updates
2021-06-16
~1 min read
doc.rust-lang.org
pub fn get<Q>(&self, value: &Q) -> Option<&T>
...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 set = BTreeSet::from([1, 2, 3]);
assert_eq!(set.get(&2), Some(&2));
assert_eq!(set...
method
alloc
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
...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::from([1, 2, 3]);
assert_eq!(set.take(&2), Some(2));
assert_eq...
method
alloc
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub trait Neg
...use std::ops::Neg;
#[derive(Debug, PartialEq)]
enum Sign {
Negative,
Zero,
Positive,
}
impl Neg for Sign {
type Output = Self;
fn neg(self) -> Self::Output {
match self {
Sign::Negative => Sign::Positive,
Sign::Zero => Sign::Zero,
Sign::Positive => Sign::Negative,
}
}
}
// A negative positive is a negative.
assert_eq!(-Sign::Positive, Sign::Negative...
trait
core
Stable since 1.0.0
Version 1.100.0-nightly
cloudhead.io
...sources.wait(&mut events)?;
for (key, event) in events.iter() {
match key {
Source::Listener => loop {
// Accept as many connections as we can.
let (conn, addr) = match listener.accept() {
Ok((conn, addr)) => (conn, addr),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
Err(e) => return Err(e),
};
// Register the new...
News & Blog Posts
2020-07-21
~6 min read
docs.rs
...use konst::{ eq_str, option, result::unwrap_ctx, }; #[derive(Debug, PartialEq)] enum Direction { Forward, Backward, Left, Right, } impl Direction { const fn try_parse(input: &str) -> Result<Self, ParseDirectionError> { // As of Rust 1.65.0, string patterns don't work in const contexts match () { _ if eq_str(input, "forward") => Ok(Direction...
Crate
v0.3.17
2026-04-16
github.com
...Since we already allow recursion
via const fn and termination of said recursion via `if` or `match`, all code
enabled by const recursion is already legal now. Some algorithms are better
expressed as imperative loops and a lot of Rust code uses loops instead of
recursion. Allowing loops in constants...
RFC 2344
RFC
2018-02-18
~2 min read
doc.rust-lang.org
pub fn get<Q>(&self, value: &Q) -> Option<&T>
...The value may be any borrowed form of the set's value type, but Hash and Eq on the borrowed form must match those for the value type.
Examples
use std::collections::HashSet;
let set = HashSet::from([1, 2, 3]);
assert_eq!(set.get(&2), Some(&2));
assert_eq!(set...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
doc.rust-lang.org
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
...The value may be any borrowed form of the set's value type, but Hash and Eq on the borrowed form must match those for the value type.
Examples
use std::collections::HashSet;
let mut set = HashSet::from([1, 2, 3]);
assert_eq!(set.take(&2), Some(2));
assert_eq...
method
std
Stable since 1.9.0
Version 1.100.0-nightly
docs.rs
...Making API calls The following will execute a POST request to /_search?allow_no_indices=true with a JSON body of {"query":{"match_all":{}}} use elasticsearch::{Elasticsearch, Error, SearchParts}; use serde_json::{json, Value}; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let client = Elasticsearch::default(); // make...
Crate
v8.19.0-alpha.1
2025-08-08
mortenvistisen.com
...Tera = {
10 let mut tera = match Tera::new("templates/**/*.html") {
11 Ok(t) => t,
12 Err(e) => {
13 println!("Parsing error(s): {}", e);
14 ::std::process::exit(1);
15 }
16 };
17 tera.autoescape_on(vec![".html", ".sql"]);
18 tera
19 };
20}
21
22pub fn start_blog(listener: TcpListener) -> Result<Server...
Rust Walkthroughs
2022-09-07
~24 min read
doc.rust-lang.org
...In the following example, surrounding the matcher with $(...),+ will match one or more expression, separated by commas. Also note that the semicolon is optional on the last case.
// `find_min!` will calculate the minimum of any number of arguments.
macro_rules! find_min {
// Base case:
($x:expr) => ($x);
// `$x` followed...
Rust by Example
Book
2024-01-01
~1 min read
www.ntietz.com
...It's just a big old match statement.
We check the byte that's passed in and we return the appropriate value.
pub fn parse_system_realtime(status_byte: u8) -> SystemRealtime {
match status_byte {
0xf8 => SystemRealtime::Clock,
0xf9 => SystemRealtime::Tick,
0xfa => SystemRealtime::Start,
0xfb => SystemRealtime::Continue,
0xfc => SystemRealtime::Stop,
0xfe...
Rust Walkthroughs
2024-12-11
~16 min read
ochagavia.nl
...the code used is_some() followed by unwrap(), instead of pattern matching, to extract the option’s inner value. Maybe the code was written before pattern matching was even introduced to the language, and I was the first one to notice it could be improved (language changes were routine in...
Observations/Thoughts
2024-03-13
~2 min read
priver.dev
...Serialize>(t: &T) {
let serialized = serde_json::to_string(t).unwrap();
println!("{}", serialized);
}
pub async fn new(db_url: &str) -> Result<Box<dyn DatabaseDriver>> {
if db_url.starts_with("libsql") {
let token = match config::database_token() {
Ok(t) => t,
Err(err) => bail!("{}", err),
};
let client = match libsql::LibSQLDriver::new(db...
Rust Walkthroughs
2023-12-20
~8 min read
rust-analyzer.github.io
...UnevaluatedConst before trait solving.
#14890 use ::core instead of $crate in option_env! expansion.
#14893 fix need-mut false positive in closure capture of match scrutinee.
#14874 change how #![cfg(FALSE)] behaves on crate root.
#14895 don’t try to determine type of token inside macro calls.
#14904 render size...
Project/Tooling Updates
2023-05-31
~1 min read
rust-analyzer.github.io
...left braces.
#16822 improve resolution for inlay hints targeting the same position.
#16871, #16886 skip problematic cyclic dev-dependencies.
#16885 improve parser recovery for match arms.
#16812 fix "Go to implementation" for impls inside blocks.
#16909 keep the Attr::Literal spans.
#16911 fix hang on projects depending on rustc_private.
Project/Tooling Updates
2024-03-27
~1 min read
doc.rust-lang.org
pub struct SystemTimeError
...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()),
}
struct
std
Stable since 1.8.0
Version 1.100.0-nightly