From 02cddce1f50dfa2febc545055c6dce9ca5b15d93 Mon Sep 17 00:00:00 2001 From: kangalioo Date: Sun, 21 May 2023 10:21:47 +0200 Subject: [PATCH] Add example --- README.md | 15 +++++++++++++++ src/lib.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) 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