Crates.io | rhs_first_assign |
lib.rs | rhs_first_assign |
version | 0.1.0 |
source | src |
created_at | 2020-02-17 09:34:43.745949 |
updated_at | 2020-02-17 09:34:43.745949 |
description | An attribute macro to hack compound assignment. |
homepage | |
repository | https://github.com/qryxip/rhs_first_assign |
max_upload_size | |
id | 209988 |
size | 23,020 |
An attribute macro to hack compound assignment.
use std::num::Wrapping;
fn main() {
let mut xs = vec![Wrapping(1), Wrapping(2)];
// OK
xs[1] = xs[0] + xs[1];
// Error
xs[1] += xs[0];
}
error[E0502]: cannot borrow `xs` as immutable because it is also borrowed as mutable
--> src/main.rs:10:14
|
10 | xs[1] += xs[0];
| ---------^^---
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
| mutable borrow later used here
use rhs_first_assign::rhs_first_assign;
use std::num::Wrapping;
#[rhs_first_assign]
fn main() {
let mut xs = vec![Wrapping(1), Wrapping(2)];
xs[1] = xs[0] + xs[1];
xs[1] += xs[0];
}
↓
use std::num::Wrapping;
fn main() {
let mut xs = vec![Wrapping(1), Wrapping(2)];
xs[1] = xs[0] + xs[1];
{
let __rhs_first_assign_rhs_l11_c10 = xs[0];
xs[1] += __rhs_first_assign_rhs_l11_c10;
};
}
Licensed under MIT OR Apache-2.0
.