Closure may outlive the function while iterating over 2D array - rust

I have a Vec<Vec<u32> and want to iterate over every item and see if it has a bigger number on the right side. Here's my code fragment:
matrix
.iter()
.enumerate()
.flat_map(|(y, row)| {
row.iter()
.enumerate()
.filter(|(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
})
.collect::<Vec<_>>();
Unfotunately, this code returns the following error:
error[E0373]: closure may outlive the current function, but it borrows `y`, which is owned by the current function
--> src/main.rs:16:25
|
16 | .filter(|(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| ^^^^^^^^^^^ may outlive borrowed value `y` - `y` is borrowed here
|
note: closure is returned here
--> src/main.rs:14:13
|
14 | / row.iter()
15 | | .enumerate()
16 | | .filter(|(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| |_______________________________________________________________________________________________^
help: to force the closure to take ownership of `y` (and any other referenced variables), use the `move` keyword
|
16 | .filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| ++++
If I use move here, then it changes to a different error:
error[E0507]: cannot move out of `matrix`, a captured variable in an `FnMut` closure
--> src/main.rs:16:25
|
9 | let matrix = prepare_matrix();
| ------ captured outer variable
...
13 | .flat_map(|(y, row)| {
| ___________________-
14 | | row.iter()
15 | | .enumerate()
16 | | .filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| | ^^^^^^^^^^^^^^^^ ------
| | | |
| | | variable moved due to use in closure
| | | move occurs because `matrix` has type `Vec<Vec<u32>>`, which does not implement the `Copy` trait
| | move out of `matrix` occurs here
17 | | })
| |_________- captured by this `FnMut` closure
I have tried to clone matrix and work on its clone for comparing, but it doesn't work either. How do I fix this?

The problem is that you return filter()'s closure from flat_map(), but it captures y, which is owned by flat_map(), and it captures it by reference because it is Copy.
As always, when we want to force a closure to capture something by copy/move, we add move to it:
.flat_map(|(y, row)| {
row.iter()
.enumerate()
.filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
})
But now we have another problem:
error[E0507]: cannot move out of `matrix`, a captured variable in an `FnMut` closure
--> src/main.rs:10:25
|
2 | let matrix: Vec<Vec<u32>> = Vec::new();
| ------ captured outer variable
...
6 | .flat_map(|(y, row)| {
| ---------- captured by this `FnMut` closure
...
10 | .filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| ^^^^^^^^^^^^^^^^ ------
| | |
| | variable moved due to use in closure
| | move occurs because `matrix` has type `Vec<Vec<u32>>`, which does not implement the `Copy` trait
| move out of `matrix` occurs here
error[E0505]: cannot move out of `matrix` because it is borrowed
--> src/main.rs:6:19
|
3 | / matrix
4 | | .iter()
| |_______________- borrow of `matrix` occurs here
5 | .enumerate()
6 | .flat_map(|(y, row)| {
| -------- ^^^^^^^^^^ move out of `matrix` occurs here
| |
| borrow later used by call
...
10 | .filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
| ------ move occurs due to use in closure
Because a move closure captures everything by move, it includes matrix. But matrix isn't Copy so we can't do that.
The way to force a move closure to capture something specific by reference is to add a variable containing a reference and capture it:
.flat_map(|(y, row)| {
let matrix = &matrix;
row.iter()
.enumerate()
.filter(move |(x, cell)| (*x..row.len()).find(|x| &matrix[y][*x] > *cell).is_none())
})

Related

Rust derive default on enum to remove need for impl block

~/Workspace/microscaler/docker-api-rs on fix/linting-issues cargo clippy --all-targets --all-features -- -D clippy::all -Z macro-backtrace ✔ at 20:14:11
Checking docker-api v0.12.2 (/Users/casibbald/Workspace/microscaler/docker-api-rs)
error: this `impl` can be derived
--> src/opts/container.rs:50:1
|
50 | / impl Default for Isolation {
51 | | fn default() -> Self {
52 | | Isolation::Default
53 | | }
54 | | }
| |_^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
= note: `-D clippy::derivable-impls` implied by `-D clippy::all`
= help: remove the manual implementation...
help: ...and instead derive it...
|
43 | #[derive(Default)]
|
help: ...and mark the default variant
|
45 ~ #[default]
46 ~ Default,
|
error: could not compile `docker-api` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `docker-api` due to previous error
The following help page is referred to:
https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
However, adding #[derive(Default)] does not seem so straightforward when multiple items exist.
Source code here:
https://github.com/microscaler/docker-api-rs/blob/d85deec98a10dcc2f6a1ed7c324010e28d437941/src/opts/container.rs#L50
The following does not work:
Checking docker-api v0.12.2 (/Users/casibbald/Workspace/microscaler/docker-api-rs)
error: no default declared
--> src/opts/container.rs:41:10
|
41 | #[derive(Default, Clone, Debug, Serialize, Deserialize)]
| ^^^^^^^ in this derive macro expansion
|
::: /Users/casibbald/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/default.rs:186:1
|
186 | pub macro Default($item:item) {
| ----------------- in this expansion of `#[derive(Default)]`
|
= help: make a unit variant default by placing `#[default]` above it
Read the compiler/linter messages, they tell you exactly what to do:
= help: remove the manual implementation...
help: ...and instead derive it...
|
43 | #[derive(Default)]
|
help: ...and mark the default variant
|
45 ~ #[default]
46 ~ Default,
|
= help: make a unit variant default by placing `#[default]` above it
Like this:
#[derive(Default, Clone, Debug)]
pub enum Isolation {
#[default]
Default,
Process,
HyperV,
}

Substrate - Pallet-Nicks Tutorial - error: unexpected token #10993

added dependencies & features in the Cargo.toml
then: $ cargo build --release
how can I fix this?
A lot of errors are occurring:
Compiling node-template-runtime v4.0.0-dev (/home/robert/substrate-node-template/runtime)
Compiling pallet-nicks v4.0.0-dev (https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.17#22d40c76)
error: failed to run custom build command for `node-template-runtime v4.0.0-dev (/home/robert/substrate-node-template/runtime)`
Caused by:
process didn't exit successfully: `/home/robert/substrate-node-template/target/release/build/node-template-runtime-6e84ec0a3716120c/build-script-build` (exit status: 1)
--- stdout
Information that should be included in a bug report.
Executing build command: "rustup" "run" "nightly" "cargo" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path=/home/robert/substrate-node-template/target/release/wbuild/node-template-runtime/Cargo.toml" "--color=always" "--profile" "release"
Using rustc version: rustc 1.61.0-nightly (38a0b81b1 2022-03-06)
--- stderr
Compiling pallet-nicks v4.0.0-dev (https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.17#22d40c76)
Compiling node-template-runtime v4.0.0-dev (/home/robert/substrate-node-template/runtime)
error: unexpected token
--> /home/robert/substrate-node-template/runtime/src/lib.rs:326:2
|
326 | / {
327 | | /* --snip-- */
328 | | Balances: pallet_balances,
329 | |
330 | | /*** Add This Line ***/
331 | | Nicks: pallet_nicks,
332 | | }
| |_____^
error[E0433]: failed to resolve: use of undeclared type `Runtime`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:396:24
|
396 | OpaqueMetadata::new(Runtime::metadata().into())
| ^^^^^^^ use of undeclared type `Runtime`
error[E0433]: failed to resolve: use of undeclared type `Aura`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:439:49
|
439 | sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())
| ^^^^ use of undeclared type `Aura`
error[E0433]: failed to resolve: use of undeclared type `Aura`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:443:4
|
443 | Aura::authorities().into_inner()
| ^^^^ use of undeclared type `Aura`
error[E0433]: failed to resolve: use of undeclared type `Grandpa`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:461:4
|
461 | Grandpa::grandpa_authorities()
| ^^^^^^^ use of undeclared type `Grandpa`
error[E0433]: failed to resolve: use of undeclared type `Grandpa`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:465:4
|
465 | Grandpa::current_set_id()
| ^^^^^^^ use of undeclared type `Grandpa`
error[E0433]: failed to resolve: use of undeclared type `System`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:491:4
|
491 | System::account_nonce(account)
| ^^^^^^ use of undeclared type `System`
error[E0433]: failed to resolve: use of undeclared type `TransactionPayment`
--> /home/robert/substrate-node-template/runtime/src/lib.rs:500:4
|
500 | TransactionPayment::query_info(uxt, len)
| ^^^^^^^^^^^^^^^^^^ use of undeclared type `TransactionPayment`
and so on
When you get this type of chained errors, it is likely that you have copied something wrong, accidentally deleting some important element, for example, a comma, a semicolon, etc.
Make sure you didn't delete anything important and try again.
If this is not the case, then check this line, I think it is the key:
error: unexpected token --> /home/robert/substrate-node-template/runtime/src/lib.rs:326:2
| 326 | / { 327 | | /* --snip-- / 328 | | Balances: pallet_balances, 329
|
| 330 | | /** Add This Line ***/ 331 | | Nicks: pallet_nicks, 332 | | }
| |_____^

What to do about "match is not allowed in a const fn" error?

When running the command:
cargo install cargo-generate --features vendored-openssl
I am getting the following error:
error[E0658]: `match` is not allowed in a `const fn`
--> /home/pam/.cargo/registry/src/github.com-1ecc6299db9ec823/socket2-0.4.0/src/lib.rs:156:9
|
156 | / match address {
157 | | SocketAddr::V4(_) => Domain::IPV4,
158 | | SocketAddr::V6(_) => Domain::IPV6,
159 | | }
| |_________^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
error[E0599]: no associated item named `MAX` found for type `usize` in the current scope
--> /home/pam/.cargo/registry/src/github.com-1ecc6299db9ec823/socket2-0.4.0/src/sys/unix.rs:680:46
|
680 | msg.msg_iovlen = min(bufs.len(), IovLen::MAX as usize) as IovLen;
| ^^^ associated item not found in `usize`
error[E0599]: no associated item named `MAX` found for type `usize` in the current scope
--> /home/pam/.cargo/registry/src/github.com-1ecc6299db9ec823/socket2-0.4.0/src/sys/unix.rs:740:46
|
740 | msg.msg_iovlen = min(bufs.len(), IovLen::MAX as usize) as IovLen;
| ^^^ associated item not found in `usize`
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0599, E0658.
For more information about an error, try `rustc --explain E0599`.
error: could not compile `socket2`.
warning: build failed, waiting for other jobs to finish...
error: failed to compile `cargo-generate v0.6.1`, intermediate artifacts can be found at `/tmp/cargo-install7B7MDG`
Caused by:
build failed
Using Rust version 1.41.0 on Ubuntu 20.04.2 LTS. What am I missing here?
Updating Rust worked for me:
curl https://sh.rustup.rs -sSf | sh
New version 1.52.0

multiple applicable items in scope

I'm using Ubuntu 20.04.2.0-desktop-amd64 , Substrate 3.0.0
Version:
gh#ubuntu:~$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home: /home/gh/.rustup
installed toolchains
--------------------
stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu
active toolchain
----------------
stable-x86_64-unknown-linux-gnu (default)
rustc 1.50.0 (cb75ad5db 2021-02-10)
And I'm using "cargo install --force subkey --git https://github.com/paritytech/substrate"
to download
But I can't download Subkey:
Compiling idna v0.1.5
error[E0034]: multiple applicable items in scope
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/field.rs:801:12
|
801 | if M::BITS > T::Mem::BITS {
| ^^^^ multiple `BITS` found
|
note: candidate #1 is defined in the trait `BitMemory`
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/mem.rs:44:2
|
44 | const BITS: u8 = mem::size_of::<Self>() as u8 * 8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in the trait `IsNumber`
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/funty-1.2.0/src/lib.rs:144:2
|
144 | const BITS: u32;
| ^^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #1
|
801 | if BitMemory::BITS > T::Mem::BITS {
| ^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #2
|
801 | if IsNumber::BITS > T::Mem::BITS {
| ^^^^^^^^^^^^^^
error[E0308]: mismatched types
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/field.rs:801:19
|
765 | fn load_le<M>(&self) -> M
| - this type parameter
...
801 | if M::BITS > T::Mem::BITS {
| ^^^^^^^^^^^^ expected type parameter `M`, found `u8`
|
= note: expected type parameter `M`
found type `u8`
error[E0034]: multiple applicable items in scope
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/field.rs:809:12
|
809 | if M::BITS > T::Mem::BITS - shamt {
| ^^^^ multiple `BITS` found
|
note: candidate #1 is defined in the trait `BitMemory`
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/mem.rs:44:2
|
44 | const BITS: u8 = mem::size_of::<Self>() as u8 * 8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in the trait `IsNumber`
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/funty-1.2.0/src/lib.rs:144:2
|
144 | const BITS: u32;
| ^^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #1
|
809 | if BitMemory::BITS > T::Mem::BITS - shamt {
| ^^^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #2
|
809 | if IsNumber::BITS > T::Mem::BITS - shamt {
| ^^^^^^^^^^^^^^
error[E0308]: mismatched types
--> /home/gh/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.20.1/src/field.rs:809:19
|
765 | fn load_le<M>(&self) -> M
| - this type parameter
...
809 | if M::BITS > T::Mem::BITS - shamt {
| ^^^^^^^^^^^^^^^^^^^^ expected type parameter `M`, found `u8`
|
= note: expected type parameter `M`
found type `u8`
...(The same error as above)
error: aborting due to 60 previous errors
Some errors have detailed explanations: E0034, E0308.
For more information about an error, try `rustc --explain E0034`.
error: could not compile `bitvec`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: failed to compile `subkey v2.0.0 (https://github.com/paritytech/substrate#f5d2faf1)`, intermediate artifacts can be found at `/tmp/cargo-installVIcyI0`
Caused by:
build failed
At the beginning of the download,the following warning appears
warning: profile package spec `cranelift-codegen` in profile `dev` did not match any packages
warning: profile package spec `cranelift-wasm` in profile `dev` did not match any packages
warning: profile package spec `libm` in profile `dev` did not match any packages
Did you mean `libc`?
warning: profile package spec `librocksdb-sys` in profile `dev` did not match any packages
warning: profile package spec `nalgebra` in profile `dev` did not match any packages
I tried again and again, but it didn't work
Is it possible to do cargo install with the repo cargo.lock with:
cargo install --locked --force subkey --git https://github.com/paritytech/substrate
Temporary issue from an upstream care. Just do:
cargo update -p funty --precise "1.1.0"

build failed in substrate.dev Ink! tutorial. sp-aritmetic lack of type? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My build has failed.
Everything looked good up until I tested my contract with the command:
cargo +nightly test
I got error [E0282]
Here is the whole error report:
error[E0282]: type annotations needed
--> /home/mal/.cargo/registry/src/github.com-1ecc6299db9ec823/sp-arithmetic-2.0.0/src/fixed_point.rs:541:9
|
541 | let accuracy = P::ACCURACY.saturated_into();
| ^^^^^^^^ consider giving `accuracy` a type
...
1595 | / implement_fixed!(
1596 | | FixedI64,
1597 | | test_fixed_i64,
1598 | | i64,
... |
1601 | | "_Fixed Point 64 bits signed, range = [-9223372036.854775808, 9223372036.854775807]_",
1602 | | );
| |__- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> /home/mal/.cargo/registry/src/github.com-1ecc6299db9ec823/sp-arithmetic-2.0.0/src/fixed_point.rs:541:9
|
541 | let accuracy = P::ACCURACY.saturated_into();
| ^^^^^^^^ consider giving `accuracy` a type
...
1604 | / implement_fixed!(
1605 | | FixedI128,
1606 | | test_fixed_i128,
1607 | | i128,
... |
1611 | | [-170141183460469231731.687303715884105728, 170141183460469231731.687303715884105727]_",
1612 | | );
| |__- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> /home/mal/.cargo/registry/src/github.com-1ecc6299db9ec823/sp-arithmetic-2.0.0/src/fixed_point.rs:541:9
|
541 | let accuracy = P::ACCURACY.saturated_into();
| ^^^^^^^^ consider giving `accuracy` a type
...
1614 | / implement_fixed!(
1615 | | FixedU128,
1616 | | test_fixed_u128,
1617 | | u128,
... |
1621 | | [0.000000000000000000, 340282366920938463463.374607431768211455]_",
1622 | | );
| |__- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0282`.
error: could not compile `sp-arithmetic`
This is part of a wider tutorial.
I believe rust is updated.
The problem seems to be the macro sp-aritmetic, however I am not sure how to give it a "type". My rust is non-existent...
Really eager to get into Ink! so any help is appreciated.
The cause of that error is a regression in the rust nightly compiler starting with version 2020-10-06. You need to rustup toolchain install 2020-10-05 and use that one for building your runtime until this issue is resolved. Usually, the latest nightly is automatically used to build your runtime. You can override this behaviour by setting the WASM_BUILD_TOOLCHAIN environment variable. In this case:
export WASM_BUILD_TOOLCHAIN='nightly-2020-10-05'
In case of building a contract with cargo contract you should use:
cargo +nightly-2020-10-05 contract build

Resources