Rust不能将不可变的局部变量借用为可变的。但是,它被定义为可变的

时间:2015-07-19 15:25:38

标签: rust

我正在使用xml-rs从tiled读取xml输出,这是用于游戏的二维级别/平铺地图编辑器。

我有我的解析方法,一切都开始了:

NameError uninitialized constant FormsController::ApplicationForm

解析器变量创建为可变,并传递给next_tileset。还没有投诉。它崩溃的地方是next_tileset方法:

pub fn parse(path: String) {
    let file = File::open(path).unwrap();
    let file = BufReader::new(file);
    let mut parser = EventReader::new(file);
    let mut tilesets: Vec<TileSet> = vec![];

    let mut tag_attributes: Vec<OwnedAttribute> = make_attributes();
    let mut tag_name = String::from_str("");

    {
        let event = parser.next();
        match event {
            XmlEvent::StartElement { name, attributes, .. } => {
                tag_name = name.local_name;
                tag_attributes = attributes;
            }
            XmlEvent::EndElement {name, .. } => {
            }
            XmlEvent::Error(e) => {
                println!("Error: {}", e);
            }
            _ => {}
        }
    }

    if tag_name == "tileset" {
        let tileset = next_tileset(&mut parser, &tag_attributes);
        tilesets.push(tileset);
    }

    for tileset in tilesets {
        println!("{}", tileset);
    }
}

你会在循环中看到匹配,它会将解析器的可变引用传递给fn next_tileset(parser: &mut EventReader<BufReader<File>>, attributes: &Vec<OwnedAttribute>) -> TileSet { let mut tileset = TileSet{ name: "".to_string(), imagesource: "".to_string(), imageheight: 0, imagewidth: 0, margin: 0, spacing: 0, tilewidth: 0, tileheight: 0, tiles: vec![] }; for attribute in attributes { match attribute.name.local_name.as_ref() { "name" => { tileset.name = attribute.value; }, "margin" => { tileset.margin = attribute_as_usize(&attribute.value); }, "spacing" => { tileset.spacing = attribute_as_usize(&attribute.value); }, "tilewidth" => { tileset.tilewidth = attribute_as_usize(&attribute.value); }, "tileheight" => { tileset.tileheight = attribute_as_usize(&attribute.value); }, _ => {} } } loop { let mut tag_name = String::from_str(""); let mut tag_type: Tag; let mut tag_attributes: Vec<OwnedAttribute> = Vec::<OwnedAttribute>::new(); { let event = parser.next(); match event { XmlEvent::StartElement { name, attributes, .. } => { tag_type = Tag::Open; tag_name = name.local_name; tag_attributes = attributes; }, XmlEvent::EndElement { name, .. } => { tag_type = Tag::Close; tag_name = name.local_name; }, _ => { tag_type = Tag::Neither; } } } match tag_type { Tag::Open => { if tag_name == "image" { parse_image(&mut tileset, &tag_attributes); } else if tag_name == "tile" { let tile = next_tile(&mut parser, &tag_attributes); tileset.tiles.push(tile); } }, Tag::Close => { if tag_name == "tileset" { break; } else { continue; } }, Tag::Neither => { continue; } } } return tileset; } 。所以这就是它给出错误的地方。想知道它是否与使用BufReader的事实有关。

0 个答案:

没有答案