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

“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?
Rust borrow lasts beyond the scope it's in?
How can I include an arbitrary set of Protobuf-built files without knowing their names?
Specialization of method in inherent impl
Getting reference to object behind Rc
Similar implementations of one trait for many structs
Unable to find unsafe trait implementation
Injecting a Diesel connection into an Iron middleware

Categories

HOME
rust
visual-studio
artificial-intelligence
android-espresso
matrix
adfs3.0
systemd
operating-system
embedded-resource
out-of-memory
azure-data-lake
azure-mobile-services
capistrano
mithril.js
reportviewer
stack-overflow
fasm
parsley.js
fbloginview
xbap
live-streaming
cuba-platform
squid
texas-instruments
bnf
pygobject
google-sites
checkout
gmp
android-custom-view
gitkraken
akka-persistence
weex
sqlplus
function-points
python-imageio
basex
docx4j
winsock
argv
osx-lion
microsoft-certifications
spring-rabbitmq
unification
text-decorations
linq.js
viewcontroller
nuclio
gcal
md5-file
karabiner
node-apn
sigsegv
htmlspecialchars
case-when
kendo-editor
alertify
http4s
qtextedit
streamwriter
nclam
xmlbeans
eclemma
meld
mercurial-extension
flipclock
pack
android-mapview
html.actionlink
apache-commons-digester
bootstrap-wysiwyg
android-audiomanager
fasterxml
datagridcomboboxcolumn
qt5.4
lumia-imaging-sdk
zend-search-lucene
xulrunner
code39
compiler-flags
virtual-channel
tnt4j
quickgraph
hints
objective-c-runtime
pacman
blobstorage
eager-loading
itunes-sdk
image-formats
n-queens
qtextbrowser
glassfish-esb
kext
tablet-pc
directshow.net
airprint
suphp
formal-semantics
pinax
fgetc
fxruby
inversion
tso
suggestbox

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