rust


Does dereference equal to a function call?


What happens if I borrow a dereferenced pointer?
let a = some object;
let b = &a;
let c = &*b;
What object has c borrowed? Does the dereferencing op create a temporary object like a function's return value? How does it obey the borrowing rules.
I'm also confused about Box's mutable semantic.
let mut a = Box::new(8us)
*a = 1;
This code works just fine without something like Box::new_mut(). But
let mut a = &8us;
*a = 1;
An error occurs.
Quick answer: It depends.
Long answer: keep reading...
What happens if I borrow a dereferenced pointer?
If you check out the LLVM-IR that rust generates, you can see everything in fine detail:
let a = 8us;
let b = &a;
let c = &*b;
gets expanded to
let a;
// %a = alloca i64
let b;
// %b = alloca i64*
let c;
// %c = alloca i64*
a = 8us;
// store i64 8, i64* %a
b = &a;
// store i64* %a, i64** %b
let tmp = *b;
// %0 = load i64** %b
c = tmp;
// store i64* %0, i64** %c
now, llvm can easily optimize this stuff out. It gets more complicated once you implement traits like Deref on your own types. Then obviously a function call is involved, but most likely optimized out again, since you shouldn't be doing complicated stuff in the deref function.
What object has c borrowed?
c borrows a
Does the dereferencing op create a temporary object like a function's return value?
Nope, see above
How does it obey the borrowing rules.
*b behaves as if it were a. If you take a reference to it you get a reference to a.
To answer your second issue:
A Box owns the object it points to. Since you declared your Box to be mutable, you can take eithe a mutable reference or any number of non-mutable references to your object. This means when you dereference the Box rust decides, depending the situation, whether to automatically create a mutable box. In the case of the assignment, your deref'd box is on the left side of the assignment operator, therefor rust tries to get a mutable reference to the object.
If you give your variables types, this becomes more obvious:
let mut x = Box::new(8us);
{
let y : &usize = &*x;
}
{
let y : &mut usize = &mut *x;
*x = 99; // cannot assign to `*x` because it is borrowed
}

Related Links

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
When do I need to specify explicit lifetimes in Rust?
cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable
Getting sequence of bytes (u8) from a char
How does the lifetime work on constant strings?
Conditionally compile only one module at a time
“macro undefined” error in the macro's own doc test

Categories

HOME
entity-framework
gaming
freeradius
shinyapps
exchange-server
concurrency
activemq
slider
websocket
amp
tfs2015
swift2
graphql-js
eddystone
swarm
getopenfilename
rangy
pm2
amazon-elb
corona
bnf
maven-plugin
string-formatting
cloudsim
http-authentication
bazaar
raml
tableview
android-permissions
fido-u2f
sybase-ase
backendless
multi-dimensional-scaling
basex
bonita
splunk-query
objectmapper
seafile-server
mongoengine
cloveretl
grails-2.5
djcelery
android-alarms
hidden-markov-models
selenide
swfupload
uipickerview
fiware-wirecloud
1wire
virtual-memory
apache-toree
spongycastle
solr-query-syntax
punctuation
maven-jaxb2-plugin
sequence-diagram
word-2007
deedle
http4s
adobe-reader
email-injection
streamwriter
tablespace
livereload
tsqlt
strpos
jeditable
swiffy
vim-plugin
certificate-authority
openshift-cartridge
wso2ml
supercomputers
wlanapi
org-babel
com0com
mathematical-expressions
adhoc-polymorphism
utf8-decode
visual-c++-2012
webmethod
senchatouch-2.4
forio-contour
flipboard
code39
insertonsubmit
coalesce
virtual-channel
dynamic-rdlc-generation
user-forums
system-requirements
gitx
opends
nsfont
quartz-core
sigar
haiku
datacontracts
net-use
css-friendly
airprint
ninject-interception
pinax
fxruby
acceleration
gui-designer

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