Let X = &X.clone(); ^ value borrowed here after move

Hi,

How do I get out of this horrible situation? I need to remove a col for a matrix until the last one. This should be very very easy. In rust and this library its not. How do I resolve it?

move occurs because X has type na::Matrix<f64, na::Dynamic, na::Dynamic, na::VecStorage<f64, na::Dynamic, na::Dynamic>>, which does not implement the Copy trait

Why is this happening with this library? I Tried the place Copy on every derive. Why? Is rather unreal.

Thanks

let X = &X.clone();
| ^ value borrowed here after move

           let mut X = DMatrix::from_iterator(
            rows,
            cols,
            temp
            .iter()
            .cloned(),
            );


         let mut remaining_cols:usize = 0;
        let mut done = false; // mut done: bool
        while !done {

            let X = &X.clone();
            X.remove_column(0);
            remaining_cols = X.ncols();

            //done=true;
            if remaining_cols == 1{
                done=true;
            }
        }

Hi!

It appears you have not fully grasped ownership and borrowing rules in Rust. You may be interested in the Rust book about ownership and borrowing.

As can be seen in the method signature, .remove_column method takes the matrix argument self by-move and returns a new matrix (with one less column). This means that by doing X.remove_column(0) the compiler will attempt to move out of the matrix pointed by X. And because X is a reference to a matrix (&X.clone()), it will only be possible if the matrix type (here DMatrix) implements the Copy trait. However DMatrix cannot implement the Copy trait because it contains heap-allocated memory. This is why the compiler gives you this Copy-related error.

The solution to your problem is to do:

while !done {
            X = X.remove_column(0);
            remaining_cols = X.ncols();

            //done=true;
            if remaining_cols == 1{
                done=true;
            }
        }

Because X.remove_column(0) returns the new matrix with one less column, you simply have to assign to X the new matrix.

Why do you need to remove one column at a time like this by the way? Depending on your use-case, there may be more efficient ways to do this. For example, you can remove several columns at once, or simply extract the last column with X.column(X.ncols() - 1) if you want.