Add example to `LuaString::to_str`

This commit is contained in:
Jonas Schievink 2017-06-22 10:49:18 +02:00
parent f75385305c
commit 979e1d1675
1 changed files with 17 additions and 0 deletions

View File

@ -168,6 +168,23 @@ pub struct LuaString<'lua>(LuaRef<'lua>);
impl<'lua> LuaString<'lua> { impl<'lua> LuaString<'lua> {
/// Get a `&str` slice if the Lua string is valid UTF-8. /// 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> { pub fn to_str(&self) -> LuaResult<&str> {
let lua = self.0.lua; let lua = self.0.lua;
unsafe { unsafe {