简体   繁体   English

试图对 Vec() 的成员进行可变借用,但 rustc 认为我正在尝试可变借用 Vec()

[英]Trying to do a mutable borrow of members of a Vec() but rustc thinks I'm trying to mutable borrow the Vec()

In the assembler I'm working on, identifiers can be of two types - labels or symbols.在我正在研究的汇编器中,标识符可以有两种类型——标签或符号。 When being defined, the tokenize_line() helper function can automatically set the identifier type argument of the Identifier variant of the TokenInfo enum to Some(IdentifierType::Label) or Some(IdentifierType::Symbol).在定义 tokenize_line() 助手 function 时,可以自动将 TokenInfo 枚举的 Identifier 变体的标识符类型参数设置为 Some(IdentifierType::Label) 或 Some(IdentifierType::Symbol)。 However, when an identifier is being used the identifier type is not immediately obvious as it is when defining identifiers.但是,当使用标识符时,标识符类型并不像定义标识符时那样立即明显。

As such, this needs to be done in the main lex() function.因此,这需要在主 lex() function 中完成。 However, in order to do this, I need to grab mutable borrows of all the identifier tokens because unless the identifier type is already Some(whatever) I will have to modify them.但是,为了做到这一点,我需要获取所有标识符标记的可变借用,因为除非标识符类型已经是 Some(whatever),否则我将不得不修改它们。 Part of this was doing a linear search through my vector of vectors of tokens (list of list of tokens) and grabbing the positions of all identifier tokens and the second part is actually making mutable borrows of all those identifier tokens and putting those into a vector/list.其中一部分是通过我的标记向量向量(标记列表列表)进行线性搜索并获取所有标识符标记的位置,第二部分实际上是对所有这些标识符标记进行可变借用并将它们放入向量中/列表。

However, doing this through a for loop creates a problem: the rust compiler thinks I'm repeatedly borrowing the vector/list, and not the individual tokens.但是,通过 for 循环执行此操作会产生一个问题:rust 编译器认为我重复借用了向量/列表,而不是单个标记。 I have no idea if this can be worked around, or if I'll just have to use the position list and the actual list of lists of tokens.我不知道这是否可以解决,或者我是否只需要使用 position 列表和令牌列表的实际列表。 Here's my code:这是我的代码:

let mut i = 0; // i = Line counter
let mut j = 0; // j = Token counter
let mut positions: Vec<(usize, usize)> = vec!();
for line in &tokens {
    for token in line {
        match &token.info {
            TokenInfo::Identifier(t) => {
                positions.push((i, j));
            }
            _ => {}
        }
        j = j + 1;
    }
    i = i + 1;
}

let mut ident_tokens: Vec<&mut Token> = vec!();

for position in positions {
    ident_tokens.push(&mut tokens[position.0][position.1]);
}

The reference returned by the [] syntax is considered to be derived from the reference to the container, so you can't have two such references at the same time. []语法返回的引用被认为是从对容器的引用派生的,因此您不能同时拥有两个这样的引用。 Therefore, instead of因此,而不是

fn vec_of_ref_mut(vec: &mut Vec<i32>) -> Vec<&mut i32> {
    let mut result = vec![];
    
    for i in 0..vec.len() {
        result.push(&mut vec[i]);
    }
    
    result
}

you would need to use iter_mut :你需要使用iter_mut

fn vec_of_ref_mut(vec: &mut Vec<i32>) -> Vec<&mut i32> {
    vec.iter_mut().collect()
}

In a nested case like yours, you would need to use flat_map .在像您这样的嵌套案例中,您需要使用flat_map For example:例如:

fn vec_of_ref_mut(vec: &mut Vec<Vec<i32>>) -> Vec<&mut i32> {
    vec.iter_mut().flat_map(|sub_vec| sub_vec.iter_mut()).collect()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM