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

How do I provide type annotations inline when calling a non-generic function?
How to represent shared mutable state?
Convert String to SocketAddr
Why is a Cell used to create unmovable objects?
“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?

Categories

HOME
hpoo
firebase
yii
jms
haproxy
google-play-services
tabs
moodle-api
linker
siesta
xades4j
business-intelligence
mailmerge
grocery-crud
max-msp-jitter
mahout-recommender
sonicwall
user-defined-types
coroutine
renderscript
stackexchange.redis
amazon-sns
cellular-network
window-managers
uri
ninject
materialize
tightvnc
premake
rust-cargo
ms-media-foundation
markov-chains
twilio-php
mv
strapi
widestring
robust
redisson
android-webservice
sequence-diagram
lvalue
zend-server
front-camera
streamwriter
jsonix
svnserve
nunit-console
zynq
nsdata
node.js-connect
yii2-model
maximize
pack
nastran
sqlbase
angular-amd
web-api
html-escape-characters
createfile
microsoft-expression-web
ivalueconverter
roxy-fileman
strdup
jmenubar
progress-db
notin
asp.net-mvc-3-areas
smartfox
workflow-manager-1.x
taskkill
opends
fitch-proofs
memory-pool
startupscript
rtd
active-record-query
activestate
urchin
resharper-5.0
webclient.uploaddata

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