0

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.

1
  • static does not take a pattern Commented May 14, 2017 at 23:10

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help again, I use mpsc locally, but I wanted to do some tests and I found the previous "error", but now it becomes clear to me what can be static

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.