Add an example

This commit is contained in:
Jonas Schievink 2017-07-25 00:42:59 +02:00
parent 1f2fc6455f
commit 09dc89895a
1 changed files with 15 additions and 0 deletions

View File

@ -195,6 +195,21 @@ impl<'lua> String<'lua> {
/// ///
/// The returned slice will not contain the terminating null byte, but will contain any null /// The returned slice will not contain the terminating null byte, but will contain any null
/// bytes embedded into the Lua string. /// bytes embedded into the Lua string.
///
/// # Examples
///
/// ```
/// # extern crate rlua;
/// # use rlua::{Lua, String};
/// # fn main() {
/// let lua = Lua::new();
/// let globals = lua.globals();
///
/// let non_utf8: String = lua.eval(r#" "test\xff" "#, None).unwrap();
/// assert!(non_utf8.to_str().is_err()); // oh no :(
/// assert_eq!(non_utf8.as_bytes(), &b"test\xff"[..]);
/// # }
/// ```
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
let lua = self.0.lua; let lua = self.0.lua;
unsafe { unsafe {