rust


Getting the error “error: the trait `core::marker::Sized` is not implemented” when trying to return the value from a vector


I am trying to return the values of a vector with the following code and. I am getting the error message.
fn merge<'a>(left: &'a [i32], right: &'a [i32]) -> [i32] {
let mut merged: Vec<i32> = Vec::new();
// push elements to merged
*merged
}
test.rs:19:52: 19:57 error: the trait core::marker::Sized is not implemented for the type [i32]
test.rs:19 fn merge<'a>(left: &'a [i32], right: &'a [i32]) -> [i32] {
And I can't for the life of me find out how I fix this
The compiler is telling you that it is impossible to return a [T].
Rust has owned vectors (Vec<T>), slices (&[T]) and fixed-size arrays ([T; n], where n is a non-negative integer like 6).
A slice is composed of a pointer to data and a length. This is what your left and right values are. However, what isn't specified in a slice is who ultimately owns the data. Slices just borrow data from something else. You can treat the & as a signal that the data is borrowed.
A Vec is one thing that owns data and can let other things borrow it via a slice. For your problem, you need to allocate some memory to store the values, and Vec does that for you. You can then return the entire Vec, transferring ownership to the caller.
The specific error message means that the compiler doesn't know how much space to allocate for the type [i32], because it's never meant to be allocated directly. You'll see this error for other things in Rust, usually when you try to dereference a trait object, but that's distinctly different from the case here.
Here's the most likely fix you want:
fn merge(left: &[i32], right: &[i32]) -> Vec<i32> {
let mut merged = Vec::new();
// push elements to merged
merged
}
Additionally, you don't need to specify lifetimes here, and I removed the redundant type annotation on your merged declaration.

Related Links

Calling an impl method from another impl method
compilation of openssl-sys fails with `openssl/hmac.h: No such file or directory`
Indexing a String
Importing mio::tcp::TcpStream but get std::net::tcp::TcpStream
It is possible to always have Cargo show warnings?
Copy files to the target directory after build
Is it ok to return in main?
Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime
Cannot move out of `req` because it is borrowed
remove duplicates from vector of custom struct
error with % operator inside closure
How can I open a file with the standard text editor?
How to use multiple variables in routes with Nickel?
sdl2-sys won't compile - could not exec the linker: No such file or directory
Cannot borrow as mutable more than once at a time in one code - but can in another very similar
Forcing a move for an implemented `Copy` type

Categories

HOME
amazon-web-services
mpdf
long-integer
concurrency
postgresql-9.4
plpgsql
google-webmaster-tools
openflow
implicit-conversion
cdi
save
hspi
twitter-oauth
phpstorm-2017.1
clish
freertos
mailgun
xbox360
asp.net-core-1.0
vertica
raml
neo4j.rb
eclipse-emf
react-dnd
offline-caching
android-cardview
adapter
spring-annotations
tilemill
opnet
sca
cloudera-manager
cmis
landscape
love2d
mars-simulator
jacoco-maven-plugin
latexml
git-tfs
asihttprequest
liteide
ssi
scala-breeze
uiautomatorviewer
jgrapht
karabiner
redisson
embedded-v8
deedle
1010
oid
conan
openseadragon
jtag
perforce-integrate
deferred-rendering
maximize
django-redis
seadragon
vim-plugin
wif4.5
certificate-authority
wepay
diawi
outlook.com
srv-record
knife
jnlp
hmisc
uitest
modern.ie
swagger-maven-plugin
darwin
fuseesb
nsmutabledata
thoughtworks-go
lifetime
refit
boolean-algebra
nosql-aggregation
visual-c++-2005
hints
insertion
statechart
android-authenticator
examine
contextswitchdeadlock
handshake
email-verification
workflow-manager-1.x
image-scanner
dropshadow
wiktionary
yuidoc
memory-pool
delphi-prism
dip
cassini
zend-test
source-code-protection
autobench
twitter-feed

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App