rust


Borrow checker and function arguments in Rust, correct or over zealous?


When a mutable argument is passed as a function argument, the borrow checker doesn't allow this to be used to construct other arguments, even when those arguments clone values without holding a reference.
While assigning variables outside the function is always an option1, logically this seems over zealous and something the borrow checker could take into account.
Is this working as intended or something that should be resolved?
#[derive(Debug)]
struct SomeTest {
pub some_value: f64,
pub some_other: i64,
}
fn some_fn(var: &mut SomeTest, other: i64) {
println!("{:?}, {}", var, other);
}
fn main() {
let mut v = SomeTest { some_value: 1.0, some_other: 2 };
some_fn(&mut v, v.some_other + 1);
// However this works!
/*
{
let x = v.some_other + 1;
some_fn(&mut v, x);
}
*/
}
Gives this error:
--> src/main.rs:14:21
|
14 | some_fn(&mut v, v.some_other + 1);
| - ^^^^^^^^^^^^ use of borrowed `v`
| |
| borrow of `v` occurs here
See: playpen.
[1]: Even though one-off assignments do sometimes improve readability, being forced to use them for arguments encourages use of scopes to avoid single use variables polluting the name-space, causing function calls that would otherwise be one line - being enclosed in braces and defining variables... I'd like to avoid this if possible especially when the requirement seems like something the borrow checker could support.
This is an artifact of the current implementation of the borrow checker. It is a well known limitation, dating back to at least 2013, and no one is overjoyed by it.
Is this working as intended
Yes.
something that should be resolved?
Yes.
The magic keywords are "non-lexical lifetimes". Right now, lifetimes are lexical - they correspond to the blocks of source that we type. Ideally, foo.method(foo.mutable_method()) would see that the borrow ends "inside the parenthesis", but for a myriad of reasons, it is tied to the entire statement.
For tons of further information see RFC issue 811 and everything linked from there.

Related Links

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
How to return an Iterator from a Map [duplicate]
Do all primitive types implement the Copy trait?
Comparison of two floats in Rust to arbitrary level of precision
Is there a convenient way to represent x86 instructions in a struct or other language feature?
Why isn't `regex!` a wrapper for `Regex::new` to offer the same regex matching speed?
Lifetime annotation for closure argument

Categories

HOME
hpoo
rust
code-formatting
mithril.js
joomla3.2
yahoo
fireloop
cqrs
colors
multi-step
live-streaming
max-msp-jitter
synthesis
android-contacts
gollum-wiki
sipp
pyopencl
introspection
prompt
django-import-export
sendmessage
unmarshalling
envoy
publishing
argv
transform
cups
xvfb
macports
mv
django-smart-selects
hidden-markov-models
tidal-scheduler
netapp
plane
data-uri
oauth2-playground
classnotfoundexception
swift2.3
android-5.0-lollipop
supertest
uiautomatorviewer
maven-jaxb2-plugin
clrs
predicates
nitrousio
pbx
haraka
rational-performance-test
xcode6.4
nclam
dmarc
carmen
android-4.2-jelly-bean
frame-grab
android-async-http
seadragon
in-app-billing
nastran
knife
alwayson
com0com
flow-js
kotlin-android-extensions
html-escape-characters
cakephp-2.2
berkeley-db-xml
loginview
otl
jini
meteorite
indesign-server
void-pointers
semicolon
oembed
xmlwriter
simplecov
getproperty
word-frequency
firebird1.5
hardcode
zend-server-ce
boost-foreach
space-partitioning
mediacontroller
iphone-sdk-4.3
.net-services
html-components
external-sorting

Resources

Encrypt Message