rust


Compare a borrowed integer to a literal integer


I want to get the elements in an array where a condition is true. For example I would like all indices where the array elements are 0.
fn main () {
let lim = 10;
let mut sieve = vec![0; lim+1];
sieve[1] = 1;
println!("{:?}", sieve.iter()
.enumerate()
.filter(|&(_, c)| c != 0)
.map(|(i, _)| i )
.collect::<Vec<usize>>());
}
But this is a compile error: the trait bound '&{integer}: std::cmp::PartialEq<{integer}>' is not satisfied. When I use c.clone() != 0 it works.
If I understand the error message correctly, Rust complains that it can't compare a borrow to an integer with an integer. I don't see why it shouldn't be possible.
You interpret the error correctly, and the reason is that it simply isn't implemented. If the standard library writers wanted to make this work, they'd have to implement PartialEq for &i32 == i32, i32 == &i32, &mut i32 == i32, i32 == &mut i32, &i32 == &mut i32 and &mut i32 == &i32. And then they'd have to do that for all other primitive types (i8, i16, u8, u16, u32, i64, u64, f32, f64, and char).
That's a lot of PartialEq implementations.
Or instead they can just ask the users of the language to write *c != 0.
(If you're coming from C++, the key thing to understand is that syntactically, borrows are more like pointers than references. Only method call syntax has the auto-deref feature.)

Related Links

Rust Trait object conversion
Struct where one member references another [duplicate]
LLDB does not display source for Rust program on FreeBSD 12
How to convert a Option<Result<T, Error>> to an Option<T> without unwrapping it?
Borrow problems with compiled SQL statements
Conditionally derive based on feature flag
Possible to pass a '&str' or '&String' to function in Rust using 'Into'?
Why does a mutable reference to a dropped object still count as a mutable reference?
How to use 'on_send' method of Nickel response?
How to build an executable that depends on curl for x86_64-unknown-linux-musl
How can I create a struct with a HashMap where the keys the same type as the container
Issues constraining implementation lifetimes on type without lifetime parameter
How to use recursive associated types in functions?
Right way to share a reference between closures
Borrow checker and function arguments in Rust, correct or over zealous?
Most idiomatic way to create a default struct

Categories

HOME
windows-10
devexpress
cypher
header
swift2
material-components
apache-kafka-connect
ag-grid
responsive-design
azure-graph-api
solution
heuristics
migrate
match
amazon-emr
game-maker
android-externalstorage
max-msp-jitter
fresco
bnf
phonegap-build
string-formatting
significance
psql
spring-shell
yii2-basic-app
acl
eclipse-emf
pdf.js
web-push
managed-c++
annotation-processing
pyspark-sql
laravel-4.2
docx4j
devtools
express-session
rails-postgresql
testlink
jslint
djcelery
mv
hidden-markov-models
system-on-chip
coverage.py
minikube
reverse-dns
nuclio
spongycastle
dredd
webalizer
non-linear-regression
node-apn
winmerge
nivo-slider
patternlab.io
swiperefreshlayout
zend-server
maatwebsite-excel
email-injection
nitrousio
gawk
portal
streamwriter
s-function
deferred-rendering
textblock
wif4.5
ajax4jsf
bayesglm
algebraixlib
unidata
mathematical-expressions
symja
rebar
visual-c++-2012
full-text-indexing
otl
bittorrent-sync
accountmanager
spring-social-linkedin
smips
eclim
grails-2.3
stty
mongo-jackson-mapper
cgrectmake
story
chronometer
getopt-long
pcspim
fitch-proofs
kolite
filelock
representation
botnet
photo-management
nsconnection
cassini
explicit
icicles
android-input-method
iphone-sdk-4.3
mysqli-multi-query
bucket
rtti
gtktextview
xap
sqlsitemapprovider
code-camp
word-automation
synthesizer

Resources

Encrypt Message