Answers for "armanriazi•rust•error•E0502•cannot borrow `s` as mutable because it is also borrowed as immutable"

0

armanriazi•rust•error•E0502•cannot borrow `s` as mutable because it is also borrowed as immutable

// If In unsafe code error happend you will need unsafe like follow block:
use std::slice;

fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
    let len = values.len();
    let ptr = values.as_mut_ptr();

    assert!(mid <= len);

    unsafe {
        (
            slice::from_raw_parts_mut(ptr, mid),
            slice::from_raw_parts_mut(ptr.add(mid), len - mid),
        )
    }
}

fn main() {
    let mut vector = vec![1, 2, 3, 4, 5, 6];
    let (left, right) = split_at_mut(&mut vector, 3);
}

//Resolve add line  let mut x=&s; or using trait patterns for convret immut to mut
let mut s = String::from("hello");
    
    let r1 = &s; // no problem
    let r2 = &s; // no problem
    let r3 = &mut s; // BIG PROBLEM
Posted by: Guest on May-05-2022

Code answers related to "armanriazi•rust•error•E0502•cannot borrow `s` as mutable because it is also borrowed as immutable"

Browse Popular Code Answers by Language