Skip to content

Rustc help tips create a loop with borrowing #72742

Closed
@NotWearingPants

Description

@NotWearingPants

I tried compiling this code:

fn main() {
    let x: [u8] = vec!(1, 2, 3)[..];
}

which resulted in the following output of rustc:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:9
  |
2 |     let x: [u8] = vec!(1, 2, 3)[..];
  |         ^         ----------------- help: consider borrowing here: `&vec!(1, 2, 3)[..]`
  |         |
  |         doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

So the compiler says help: consider borrowing here: &vec!(1, 2, 3)[..]
So I tried adding a borrow and recompiling the code:

fn main() {
    let x: [u8] = &vec!(1, 2, 3)[..];
}

which resulted in:

error[E0308]: mismatched types
 --> src/main.rs:2:19
  |
2 |     let x: [u8] = &vec!(1, 2, 3)[..];
  |            ----   ^^^^^^^^^^^^^^^^^^
  |            |      |
  |            |      expected slice `[u8]`, found `&[{integer}]`
  |            |      help: consider removing the borrow: `vec!(1, 2, 3)[..]`
  |            expected due to this

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:9
  |
2 |     let x: [u8] = &vec!(1, 2, 3)[..];
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.

so the compiler says help: consider removing the borrow: vec!(1, 2, 3)[..]

and so I'm stuck in a loop. The compiler can't decide whether I should borrow or not.

As a newbie to Rust, this is very frustrating and the opposite of helpful, it just confuses me further.
Please fix this indecisiveness :)

Meta

rustc --version --verbose:

rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-pc-windows-msvc
release: 1.43.1
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`F-unsized_locals`#![feature(unsized_locals)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions