E0022Assignment through
Error
Assignment through ?.
ErrorAn assignment cannot go through ?., because the object may be None and then there is nothing to assign to.
Why
?. stands for "if there is an object". Unwrap the optional with if let, so the assignment sits in the branch where the object exists.
Broken
class Node {
mut value: i64
}
fn main() {
let node: Node? = Node(value: 1)
node?.value = 2
}
What sisu says
error[E0022]: cannot assign through `?.` --> E0022.sisu:7:5 | 7 | node?.value = 2 | ^^^^^^^^^^^ | = help: unwrap it with `if let` sisu: 1 error sisu: for more on a code, run `sisu explain <code>`: E0022
Fixed
class Node {
mut value: i64
}
fn main() {
let node: Node? = Node(value: 1)
if let n = node {
n.value = 2
print(n.value)
}
}
Sisu