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

How do I implement Clone/Copy for an enum that contains a String?
Modeling embedded hardware in Rust and how to have multiple mutable references cleanly?
Should I borrow or copy my small data types?
Using pointer casting to change the “type” of data in memory [duplicate]
Export function only to module test?
Take slice of certain length known at compile time
Is it possible to deactivate file locking in cargo?
“does not live long enough” error in same function
What ways exist to create containers of several types? [duplicate]
How can I optimize reading a UTF-8 string from a file with a known offset and size?
Create vector of objects implementing a trait in Rust
Using loop variable from “..” loop causes type conversion?
What is RFC 401's coerce_inner useful for?
Rust Borrow checker only complains about borrowing as mutable multiple times when a function that returns a reference with the same lifetime assigned
How can I remove `let _ : () = …`?
Does if-let with a pair short-circuit?

Categories

HOME
lambda
softlayer
postgresql-9.4
add-on
tcsh
dry
filesize
apk
gentelella
visualforce
azure-logic-apps
yahoo
amazon-emr
fbloginview
xbap
sslhandshakeexception
jpa-2.0
synthesis
boomi
gmp
nullreferenceexception
opam
angular2-highcharts
workday
exchange-server-2010
mod-wsgi
morris.js
python-appium
oracleforms
php-5.3
code-climate
bonita
dnsmasq
docx4j
source-insight
toad
commonsware-cwac
memory-fences
actframework
express-session
nsuserdefaults
iscroll
oauth2
slideshow
flipkart
coverage.py
dagger
spooler
git-tfs
docker-ucp
adblock
spongycastle
dredd
.net-micro-framework
photoswipe
simple-schema
gtk#
pen
pecl
svnserve
raw-sockets
mercurial-extension
godeps
perceptron
html.actionlink
extend
wso2ml
domino-designer-eclipse
jcheckbox
n-tier-architecture
osascript
treeline
sql-function
xulrunner
nodeload
nosql-aggregation
typesafe-stack
spring-security-acl
revmob
disjoint-union
.net-remoting
chartfx
office-app
server-name
trialware
flymake
nsfont
infopath-2007
3des
solandra
sscli
iphone-sdk-3.1.3
datagridcolumn
objectinstantiation
watchpoint
webclient.uploaddata

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