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