From 979e1d16756449b6a04e623b4f55561a363aaf2f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 22 Jun 2017 10:49:18 +0200 Subject: [PATCH] Add example to `LuaString::to_str` --- src/lua.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index 4a7bec3..d949cbc 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -168,6 +168,23 @@ pub struct LuaString<'lua>(LuaRef<'lua>); impl<'lua> LuaString<'lua> { /// Get a `&str` slice if the Lua string is valid UTF-8. + /// + /// # Example + /// + /// ``` + /// # extern crate rlua; + /// # use rlua::*; + /// # fn main() { + /// let lua = Lua::new(); + /// let globals = lua.globals().unwrap(); + /// + /// let version: LuaString = globals.get("_VERSION").unwrap(); + /// assert!(version.to_str().unwrap().contains("Lua")); + /// + /// let non_utf8: LuaString = lua.eval(r#" "test\xff" "#).unwrap(); + /// assert!(non_utf8.to_str().is_err()); + /// # } + /// ``` pub fn to_str(&self) -> LuaResult<&str> { let lua = self.0.lua; unsafe {