dtolnay / unicode-ident

Determine whether characters have the XID_Start or XID_Continue properties
Apache License 2.0
74 stars 12 forks source link

Put example in Readme #22

Open stefnotch opened 1 year ago

stefnotch commented 1 year ago

The comparison of performance part has me convinced.

Now how do I use this lovely crate? Right, I head over to docs.rs and read through the performance comparison again.

Ooooh, down there are the two functions.

Would it be possible to have a minimal example at the top of the readme that showcases the really sleek API? Something like

let is_identifier_start = is_xid_start('c');
let is_identifier_continue = is_xid_continue('a');
let is_identifier_continue = is_xid_continue('t');

Also, thank you very much for all your awesome work on those lovely Rust projects!

dtolnay commented 1 year ago

I don't think the code you've suggested would add any value in making sense of the API.

stefnotch commented 1 year ago

IMO it'd still be worthwhile to mention which functions the API actually provides, without having to search quite as much.

stefnotch commented 1 year ago

But yes, the example isn't that well chosen. Here'd be a possibly more useful example. (Untested though)

fn is_identifier(value: &str) {
  let mut chars = value.chars();
  chars.next().filter(|c| is_xid_start(*c)).is_some() && chars.all(|c| is_xid_continue(c))
}

is_identifier("cat1"); // returns true
is_identifier("42"); // returns false