diff --git a/README.md b/README.md index dfb943b..51719d3 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,21 @@ and non-ASCII codepoints with better performance, 2–10× faster than
+## Example + +```rust +fn is_identifier(value: &str) -> bool { + use unicode_ident::{is_xid_start, is_xid_continue}; + + let mut chars = value.chars(); + let Some(first_char) = chars.next() else { return false }; + is_xid_start(first_char) && chars.all(|c| is_xid_continue(c)) +} + +assert_eq!(is_identifier("cat1"), true); +assert_eq!(is_identifier("42"), false); +``` + ## Comparison of performance The following table shows a comparison between five Unicode identifier diff --git a/src/lib.rs b/src/lib.rs index b20ecc4..dee5114 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,22 @@ //! //!
//! +//! ## Example +//! +//! ```rust +//! fn is_identifier(value: &str) -> bool { +//! use unicode_ident::{is_xid_start, is_xid_continue}; +//! +//! let mut chars = value.chars(); +//! let Some(first_char) = chars.next() else { return false }; +//! is_xid_start(first_char) && chars.all(|c| is_xid_continue(c)) +//! } +//! +//! +//! assert_eq!(is_identifier("cat1"), true); +//! assert_eq!(is_identifier("42"), false); +//! ``` +//! //! ## Comparison of performance //! //! The following table shows a comparison between five Unicode identifier