Skip to content

Commit 6b72eec

Browse files
committed
syntax: add Hir::literal example for char
The example shows a succinct way of creating an HIR literal from a `char` value by first encoding it to UTF-8. Closes #1114
1 parent 20b5317 commit 6b72eec

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

regex-syntax/src/hir/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ impl Hir {
322322
/// let expected = HirKind::Literal(Literal(Box::from("☃".as_bytes())));
323323
/// assert_eq!(&expected, concat.kind());
324324
/// ```
325+
///
326+
/// # Example: building a literal from a `char`
327+
///
328+
/// This example shows how to build a single `Hir` literal from a `char`
329+
/// value. Since a [`Literal`] is just bytes, we just need to UTF-8
330+
/// encode a `char` value:
331+
///
332+
/// ```
333+
/// use regex_syntax::hir::{Hir, HirKind, Literal};
334+
///
335+
/// let ch = '☃';
336+
/// let got = Hir::literal(ch.encode_utf8(&mut [0; 4]).as_bytes());
337+
///
338+
/// let expected = HirKind::Literal(Literal(Box::from("☃".as_bytes())));
339+
/// assert_eq!(&expected, got.kind());
340+
/// ```
325341
#[inline]
326342
pub fn literal<B: Into<Box<[u8]>>>(lit: B) -> Hir {
327343
let bytes = lit.into();

0 commit comments

Comments
 (0)