2014-09-19 2 views
1

Rust Mustache (https://github.com/erickt/rust-mustache) MapBuilder 내부에서 루프를 사용하여 벡터 내용을 채울 수 있습니까? 반 작동 예제를 첨부했습니다. 구조 버전과 함께 작동하지만 빌더 버전에서는 작동하지 않습니다.Rust Mustache MapBuilder에서 루프를 사용할 수 있습니까?

편집 :이 코드는 Paul 덕분에 컴파일됩니다. 나는 두 가지를 놓치고 있었다 : |mut builder|builder = builder.push_map.

문제는 이미지 벡터를 반복하고 특정 조건을 기반으로 항목을 변경해야하므로 구조 버전을 사용하고 싶지 않습니다. 원래 벡터를 변경하고 싶지 않습니다.

여기서는 https://github.com/erickt/rust-mustache/blob/master/src/builder.rs을 보았습니다. 그러나이 코드 스 니펫 중 루프 예제가 표시되지 않았습니다. 가능하지 않거나 잘못된 것을하고 있습니다. 나는 또한 문제를 만들었지 만 에릭 캣은 거기서 매우 활발하게 보이지 않습니다.

감사합니다.

main.rs :

extern crate serialize; 

// [dependencies.rust-mustache] 
// 
// # git = "https://github.com/erickt/rust-mustache.git" 
// git = "https://github.com/tsurai/rust-mustache.git" 
extern crate mustache; 

mod with_struct; 
mod with_builder; 

fn main() { 
    with_struct::go(); 
    with_builder::go(); 
} 

with_builder.rs :

use std::io; 

use mustache; 
use mustache::MapBuilder; 

struct Image { 
    name: String, 
    file: String 
} 

impl Image { 
    fn new(name: &str, file: &str) -> Image { 
     Image { 
      name: String::from_str(name), 
      file: String::from_str(file) 
     } 
    } 
} 

pub fn go() { 
    let images = load_images(); 
    let template = mustache::compile_str(template()); 

    let data = MapBuilder::new() 
     .insert_vec("images", |mut builder| { 
     //      ^~~~~ Need mutableb builder 
      for image in images.iter() { 
       builder = builder.push_map(|builder| { 
       // ^~~~~~~~~~^~~~~ Need to re-assign the builder 
        builder 
         .insert_str("name", image.name.clone()) 
         .insert_str("file", image.file.clone()) 
       }); 
      } 
      builder 
      // ^~~~~ Can now return it 
     }) 
     .build(); 

    let _ = template.render_data(&mut io::stdout(), &data); 
} 

fn template<'a>() -> &'a str { 
    " 
     <ul> 
     {{#images}} 
      <li> 
       <a href=\"{{file}}\">{{name}}</a> 
      </li> 
     {{/images}} 
     </ul> 
    " 
} 

fn load_images() -> Vec<Image> { 
    let mut images = Vec::new(); 

    images.push(Image::new("Picture 1", "picture-1.png")); 
    images.push(Image::new("Picture 2", "picture-2.png")); 
    images.push(Image::new("Picture 3", "picture-3.png")); 

    images 
} 

with_struct.rs :

use std::io; 

use mustache; 

#[deriving(Encodable)] 
struct Image { 
    name: String, 
    file: String 
} 

#[deriving(Encodable)] 
struct TemplateData<'a> { 
    images: &'a Vec<Image> 
} 

impl Image { 
    fn new(name: &str, file: &str) -> Image { 
     Image { 
      name: String::from_str(name), 
      file: String::from_str(file) 
     } 
    } 
} 

pub fn go() { 
    let images = load_images(); 
    let template = mustache::compile_str(template()); 

    let data = TemplateData { 
     images: &images 
    }; 

    let _ = template.render(&mut io::stdout(), &data); 
} 

fn template<'a>() -> &'a str { 
    " 
     <ul> 
     {{#images}} 
      <li> 
       <a href=\"{{file}}\">{{name}}</a> 
      </li> 
     {{/images}} 
     </ul> 
    " 
} 

fn load_images() -> Vec<Image> { 
    let mut images = Vec::new(); 

    images.push(Image::new("Picture 1", "picture-1.png")); 
    images.push(Image::new("Picture 2", "picture-2.png")); 
    images.push(Image::new("Picture 3", "picture-3.png")); 

    images 
} 

답변

1

올리!

마지막 insert_str 이후에 세미콜론을 제거해야합니다 (Rust는 세미콜론을 끝에 붙이면 해당 행을 무시하고 반환 값으로 사용하지 않습니다).

+0

안녕하세요, 감사합니다. 나는 내가 거기에 있었다고 생각한다. 문제는 빌더가 루프 안에서 움직이게된다는 것이다. 'let mut b = & mut builder; '라고 해킹을 시도했지만 작동하지 않았고 빌더가 복제 가능하지 않습니다. – Ollie

+0

그러면 단지 빌더에 대한 가변 변수가 필요하고 "builder = builder.push_map (..)"을 수행하십시오. –

+0

그래, 이럴 수는 없으므로 아마 문제가 될 수 있습니다. ".insert_vec ("images ", | builder | { ... mutall b = & mut builder.clone(); ...} ". – Ollie

관련 문제