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.
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?