Borrow a section of a borrowed array as a borrowed array
NickName:goo Ask DateTime:2016-11-08T21:53:30

Borrow a section of a borrowed array as a borrowed array

As the title reads, how would I go about doing this?

fn foo(array: &[u32; 10]) -> &[u32; 5] {
    &array[0..5]
}

Compiler error

error[E0308]: mismatched types
 --> src/main.rs:2:5
  |
2 |     &array[0..5]
  |     ^^^^^^^^^^^^ expected array of 5 elements, found slice
  |
  = note: expected type `&[u32; 5]`
  = note:    found type `&[u32]`

Copyright Notice:Content Author:「goo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40488690/borrow-a-section-of-a-borrowed-array-as-a-borrowed-array

Answers
bluss 2016-11-08T21:14:48

arrayref implements a safe interface for doing this operation, using macros (and compile-time constant slicing bounds, of course).\n\nTheir readme explains\n\n\n The goal of arrayref is to enable the effective use of APIs that involve array references rather than slices, for situations where parameters must have a given size. \n\n\nand\n\nlet addr: &[u8; 16] = ...;\nlet mut segments = [0u16; 8];\n// array-based API with arrayref\nfor i in 0 .. 8 {\n segments[i] = read_u16_array(array_ref![addr,2*i,2]);\n}\n\n\n\n Here the array_ref![addr,2*i,2] macro allows us to take an array reference to a slice consisting of two bytes starting at 2*i. Apart from the syntax (less nice than slicing), it is essentially the same as the slice approach. However, this code makes explicit the need for precisely two bytes both in the caller, and in the function signature.\n",


More about “Borrow a section of a borrowed array as a borrowed array” related questions

flexlm borrowed licenses LM_BORROW_STAT struct

I need to fix some legacy code that wraps flexlm 11. the code apparently worked with earlier flexlm versions, and something must have changed in the LM_BORROW_STAT in version 11. The code retrieve...

Show Detail

Cannot borrow as immutable because it is also borrowed as mutable (Rust)

Goal Summary: I want to get rid of this error, and have my code to compile. error[E0502]: cannot borrow `selected` as immutable because it is also borrowed as mutable --> src/main.rs:17:26 ...

Show Detail

Cannot borrow immutable borrowed content as mutable

I'm trying to develop a message routing app. I've read the official Rust docs and some articles and thought that I got how pointers, owning, and borrowing stuff works but realized that I didn't. u...

Show Detail

cannot borrow as immutable because it is also borrowed as mutable

I'm using the structs Foo and Bar from a library and I'm getting a compilation error in the client code. I simplified the code to this: use std::marker::PhantomData; struct Foo { some_str: &a...

Show Detail

Borrow a section of a borrowed array as a borrowed array

As the title reads, how would I go about doing this? fn foo(array: &[u32; 10]) -> &[u32; 5] { &array[0..5] } Compiler error error[E0308]: mismatched types --> src/main.rs:..

Show Detail

Cannot borrow as immutable because it also borrowed as mutable

I have the following minimal example to reproduce this issue: fn main() { let mut vec = Vec::from(["Hello"]); let mut some_struct = SomeStruct { field: "World" };

Show Detail

Borrow as immutable inside the loop after borrowed as mutable to the iterator

I want to get a returning value from a method inside a loop. But iterator is also borrowed as a mutable. And the method want an immutable reference. This is a small reproducible code (playground l...

Show Detail

error[E0502]: cannot borrow `char_array` as mutable because it is also borrowed as immutable

I am writing a small program to identify the first recurring character in a string: use std::io; fn main() { let mut main_string = String::new(); println!("Please enter the string : "); ...

Show Detail

Cannot borrow HashMap as mutable because it is also borrowed as immutable

This question was asked zillion times, but it always has kind of unique solution. I understand why it's happening and how to fix it, but my fix is ugly and I'm probably missing something. use std::

Show Detail

Who borrowed a variable?

I'm fighting with the borrow checker. I have two similar pieces of code, one working as I expect, and the other not. The one that works as I expect: mod case1 { struct Foo {} struct Bar1...

Show Detail