繁体   English   中英

Rust 模式匹配不允许我更改值

[英]Rust Pattern Matching does not allow me to change values

所以我目前正在研究一种新的编程语言tr-lang

我目前正在编写语言的解析器

这段代码是错误所在

BlockToken::İse(bip) => {
    let ise = &mut parsed[bip]; // Access method 1
    match ise.typ {
        TokenType::İse ( mut yoksa ) => {
            yoksa = Some(ip);
        },
        TokenType::Yoksa ( mut tp ) => {
            tp = Some(ip);
        },
        _ => unreachable!(),
    }
    ip + 1
},
BlockToken::İken(bip) => {
    let iken = parsed.get_mut(bip).unwrap(); // Trying other access methods
        match iken.typ {
            TokenType::İken ( mut yoksa ) => {
                yoksa = Some(ip + 1);
        },
        _ => unreachable!(),
    }
    bip
    },
    _ => unimplemented!(),
};

这是解析并生成可执行程序的代码的一部分

它给出了一些警告,但我认为问题在于这些:

warning: variable `yoksa` is assigned to, but never used
   --> src/parser/parser.rs:121:54
    |
121 | ...                   TokenType::İse ( mut yoksa ) => {
    |                                            ^^^^^
    |
    = note: consider using `_yoksa` instead

warning: value assigned to `yoksa` is never read
   --> src/parser/parser.rs:122:37
    |
122 | ...                   yoksa = Some(ip);
    |                       ^^^^^
    |
    = help: maybe it is overwritten before being read?

warning: variable `tp` is assigned to, but never used
   --> src/parser/parser.rs:124:56
    |
124 | ...                   TokenType::Yoksa ( mut tp ) => {
    |                                              ^^
    |
    = note: consider using `_tp` instead

warning: value assigned to `tp` is never read
   --> src/parser/parser.rs:125:37
    |
125 | ...                   tp = Some(ip);
    |                       ^^
    |
    = help: maybe it is overwritten before being read?

warning: variable `yoksa` is assigned to, but never used
   --> src/parser/parser.rs:134:55
    |
134 | ...                   TokenType::İken ( mut yoksa ) => {
    |                                             ^^^^^
    |
    = note: consider using `_yoksa` instead

warning: value assigned to `yoksa` is never read
   --> src/parser/parser.rs:135:37
    |
135 | ...                   yoksa = Some(ip + 1);
    |                       ^^^^^
    |
    = help: maybe it is overwritten before being read?

正如您所看到的,由于某种原因,即使我像往常一样模式匹配,当我尝试将值设置为其他值时,它也会将变量视为不同的

而不是改变 yoksa/tp 的值,最终结果不会改变任何东西

我尝试改变我访问 ise/iken 的方式但是它没有改变任何我也尝试使用if let而不是match

它不会改变 ise.typ.yoksa 或 ise.typ.tp 的值

额外信息 BlockToken 是这个枚举

enum BlockToken {
    İse(usize),
    İken(usize),
    İkiNoktaNokta(usize),
}

令牌是这个结构

struct Token {
    pub typ:  TokenType,
    pub line: usize,
    pub col:  usize,
}

我想要的是能够更改枚举结构 İse、İken 和 Yoksa 的内容

尽管首选安全方法,但它可能不安全

我认为您需要在匹配时匹配一个可变引用,否则您只是在创建一个局部变量并对其进行变异。

例如这段代码:

#[derive(Debug)]      
enum Blah {      
    A(u64),      
}      
      
mod test {      
    use crate::refmatch::Blah;      
      
    #[test]      
    fn test_match() {      
        let mut a = Blah::A(23);      
        println!("{:?}", a);                                   
                                                               
        match a {                                              
            Blah::A(ref mut x) => *x = 5,                      
        }                                                      
        println!("{:?}", a);                                   
    }                                                          
}

将输出:

running 1 test
A(23)
A(5)

如果你用cargo test -- --nocapture运行它。

使用Option::replace将新值放入可变选项中:

yoksa.replace(ip + 1);

你也可能想要一个可变引用

TokenType::İken(ref mut yoksa)

暂无
暂无

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

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