rust


Is it possible to destructure the `self` argument of a method?


I am trying to find a way to destructure the self argument of a method. According to a GitHub comment:
Per today's meeting, we have a different plan to make self arguments destructurable. With universal function-call syntax (UFCS #11938) there will not be any distinction between static methods and instance methods - they will both be 'associated functions'. At that point any function who's first argument is the self type will be callable with method syntax, and self, &self, and &mut self are just sugar for i.e. self: &Self, and destructuring on the self argument can be done as normal by not using the self-sugar.
I wrote the following code, but it does not work as I expected in that all three print functions can be used as a method.
struct Vector {
x: i32,
y: i32,
z: i32,
}
impl Vector {
fn print1(self: &Self) {
println!("{} {} {}", self.x, self.y, self.z);
}
// destructure self argument
fn print2(&Vector{x, y, z}: &Self) {
println!("{} {} {}", x, y, z);
}
// use another name for the first argument
fn print3(this: &Self) {
println!("{} {} {}", this.x, this.y, this.z);
}
}
fn main() {
let v = Vector{x: 1, y: 2, z: 3};
Vector::print1(&v); // work
v.print1(); // work
Vector::print2(&v); // work
v.print2(); // not work
Vector::print3(&v); // work
v.print3(); // not work
}
print3() was just used to test if it is possible to use a name other than self for the first argument of a method.
It gives this compile error:
error: no method named `print2` found for type `Vector` in the current scope
--> 1.rs:27:7
|
27 | v.print2(); // not work
| ^^^^^^
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in an impl for the type `Vector`
--> 1.rs:12:5
|
12 | fn print2(&Vector{x, y, z}: &Self) {
| _____^ starting here...
13 | | println!("{} {} {}", x, y, z);
14 | | }
| |_____^ ...ending here
error: no method named `print3` found for type `Vector` in the current scope
--> 1.rs:29:7
|
29 | v.print3(); // not work
| ^^^^^^
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in an impl for the type `Vector`
--> 1.rs:16:5
|
16 | fn print3(this: &Self) {
| _____^ starting here...
17 | | println!("{} {} {}", this.x, this.y, this.z);
18 | | }
| |_____^ ...ending here
It seems that print2() and print3() are not identified as methods of Vector.
How to destructure the self argument of a method?
According to the comment, the name self is just sugar. Does it mean that a name other than self can be used for the first argument of a method?
This was originally intended to be possible with the Universal Function Calls, but is backwards incompatible, because it would mean that fn foo(bar: &Self) would suddenly be equivalent to fn foo(self: &Self), which can break method calls due to new methods appearing suddenly.
Full rationale in this github issue comment
You can destructure the explicit self parameter with a let binding in the function body:
let &Vector { x, y, z } = self;

Related Links

“entry point could not be located” when running program on Windows
Does dereference equal to a function call?
Should I use Rust 1.0-alpha or the nightly version?
How to do a binary search on a Vec of floats?
Having a child modify its parent
How do I access the data sent over UDP?
How to idiomatically copy a slice?
Recursive types and arrays
Can I determine the zero value of generic types?
How to slice a large Vec<i32> as &[u8]?
How to parse i64 from a string? [duplicate]
Why is variable scope dependent on the definition order?
Warning on “unstable” directive
How can I pass a socket as an argument to a function being called within a thread?
error: cannot move out of borrowed content on &mut self
Why does a reference not live long enough in case of “as_slice”?

Categories

HOME
extjs
list
shinyapps
seedstack
xcode8.1
kendo-dropdown
antd
dry
embedded-resource
website
whmcs
iverilog
nsis
iptables
ontology
implicit-conversion
drag-and-drop
ui-automation
heuristics
fasm
getopenfilename
live-streaming
google-awareness
web-deployment
android-contacts
sqlplus
pst
mime-types
quantitative-finance
device
perfino
managed-c++
unrar
hdmi
sap-lumira
running-object-table
network-analysis
npoi
iwebbrowser2
mdns
amazon-iam
argv
wampsharp
caesar-cipher
counting
apptentive
autoencoder
xerces-c
jfxtras
spinner
djcelery
variable-variables
stat
httphandler
git-tfs
qvtkwidget
photoswipe
nonlinear-functions
password-hash
node-apn
watir-webdriver
aurelia-fetch-client
mura
algebra
mobile-angular-ui
email-forwarding
emf-compare
d3v4
scheduledexecutorservice
controllers
bigdecimal
jsonix
livereload
fuzzer
dmarc
oci
m4
diawi
mousehover
nette
sequence-sql
ocmockito
kotlin-android-extensions
scala-swing
nanomsg
juttle
comctl32
image-rotation
loginview
angular-ui-select
android-2.3-gingerbread
.htpasswd
reference-counting
radius-protocol
property-injection
roxy-fileman
spring-social-linkedin
iosched
armv6
mp4parser
django-facebook
disjoint-union
tnt4j
hints
delphi-xe4
html-frames
struts2-json-plugin
struts2-s2hibernate
platform-independent
wcf-callbacks
yuidoc
canonicalization
kernel32
mongrel2
pyjamas
git-log
suphp
ios-4.2
icicles
bucket
fxruby
regioninfo
code-camp
account-management

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