2015-09-20 28 views
5

我想調用第三方庫這個功能(xmltree-rs):傳遞字符串的功能以閱讀特質

pub fn parse<R: Read>(r: R) -> Element { 

的預期使用情況是給它一個文件,像這樣:

let e: Element = Element::parse(File::open("tests/data/01.xml").unwrap()); 

但是我有一個String,我想通過,依稀是這樣的:

xmltree::Element::parse("<example>blah</example>".to_owned()); 

然而這當然,給出了一個錯誤:

error: the trait `std::io::Read` is not implemented for the type `collections::string::String` [E0277] 

我該怎麼做,短的寫一個臨時文件? (例如在Python中,我將使用StringIO module將字符串包裝到類似文件的對象中)

回答

10

要了解實現特徵的內容,請轉至the bottom of the page for that trait

在這種情況下,最有希望的前瞻性實施者是&'a [u8]Cursor<Vec<u8>>

你可以通過調用as_bytes(),注意到自動反引用和DerefString處取得從&strString一個&[u8]。或者,如果僅使用ASCII子集,則可以使用字節文字。