rust


Error when creating a static variable: 'expected identifier, found `(` '


I am trying to create global variables but I got multiple compilation errors in the process.
First I tried this:
static mut (tx, rx): (mpsc::Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel();
error: expected identifier, found `(`
|
109 | static mut (tx, rx): (mpsc::Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel();
^
|
Then I have tried some other forms but it seems that they always give me a similar error:
thread_local!(static mut (tx, rx): (mpsc::Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel());
error: no rules expected the token `(`
|
109 | thread_local!(static mut (tx, rx): (mpsc::Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel());
^
|
Finally, and in case this helps others to respond, it also happens with this:
static (x, y, z) = (1, 2, 3);
error: expected identifier, found `(`
|
109 | static (x, y, z) = (1, 2, 3);
| ^
Maybe it is some error when creating the tuples from a static declaration, but I'm new to Rust, so I do not know if this is true.
As you well found out in your last attempt, the same issue can be reproduced more easily:
static (A, B): (i32, i32) = (1, 2);
According to the Rust reference, the grammar for a static binding is defined as thus:
static_item : "static" ident ':' type '=' expr ';' ;
Mutable statics, although not included, is most likely defined to include
mut after static:
mut_static_item : "static" "mut" ident ':' type '=' expr ';' ;
The compiler fails to parse your statement because it expects an identifier, not a pattern. This contrasts with let binding declarations, which accept a pattern after the keyword let:
let_decl : "let" pat [':' type ] ? [ init ] ? ';' ;
init : [ '=' ] expr ;
On this end, you have no choice but not to use patterns to declare static or const variables.
In your former case of mpsc channels, even this limitation would not solve your problem, since static bindings contain many other restrictions: consider for instance the declaration of a static Vec:
static moo: Vec<i32> = Vec::with_capacity(10);
This would yield the following error:
error[E0015]: calls in statics are limited to struct and enum constructors
--> src/main.rs:1:24
|
1 | static moo: Vec<i32> = Vec::with_capacity(10);
| ^^^^^^^^^^^^^^^^^^^^^^
Channels are meant to be created locally and their endpoints sent to other threads from there. The documentation on the mpsc module provides some examples.

Related Links

Return first item of vector
What's the purpose of SizeHint in Iterator::unzip?
Repetitive trait bounds on generic structs [duplicate]
Choose to borrow or pass ownership without duplicating code in Rust
How to get the name of current program without the directory part?
Changing key of HashMap from child method
Cannot build a delete function for a binary search tree in Rust because the borrowed value does not live long enough
Why does AtomicUsize not implement Send?
How can I define a list of functions to call? [duplicate]
How can I tell if a Rust library is deprecated?
error: non-scalar cast: `core::option::Option<i32>` as `usize`
Lifetime of variable in map/flat_map in Rust
Can associated constants be used to initialize fixed size arrays?
Why do Arc and Mutex allow me to change the value of an immutable variable?
Unable to download a package via cargo — timeout
“borrowed value does not live long enough” seems to blame the wrong thing

Categories

HOME
xpath
office365
flask
extjs
artificial-intelligence
svn
long-integer
concurrency
postgresql-9.4
antd
theano
greasemonkey
tibco
jint
drag-and-drop
minecraft
sympy
clickable-image
match
slide
getopenfilename
erd
outlook-restapi
smartgwt
spring-jpa
twisted
bnf
rpmbuild
google-sites
office-ui-fabric
varnish-vcl
salt-cloud
sharedpreferences
maven-surefire-plugin
pdf.js
unordered-multimap
password-generator
sap-lumira
ninject
basic-authentication
unmarshalling
amazon-iam
docx4j
matlab-app-designer
quick-nimble
mongoengine
custom-keyboard
pdfminer
textmate2
cloudera-quickstart-vm
microsoft-ui-automation
google-now
netapp
classnotfoundexception
contactless-smartcard
julius-speech
directwrite
android-mediarecorder
gmaps.js
emf-compare
business-rules
google-shopping-api
maatwebsite-excel
gapi
researchkit
ftp-client
seccomp
groovyfx
rails-engines
adjacency-list
sharepoint-apps
django-redis
vimperator
ioctl
voronoi
webgrind
directx-9
srv-record
search-form
clarion
deque
subview
kognitio-wx2
ildasm
global-scope
visual-c++-2012
bittorrent-sync
flipboard
jchartfx
assetic
meteorite
code39
facebook-game-groups
stty
axacropdf
proc-open
examine
onconfigurationchanged
clearinterval
chronometer
monocross
opends
acpi
zend-server-ce
sql-parametrized-query
mediacontroller
nsconnection
turbine
aptitude
android-2.1-eclair
odac
browser-based
inversion
motif
remote-working
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