let mut kn;
let mut ukn;
let mut compute_batch_idx;
let mut known_batch_idx;
such like
let a,b,c,d;
let mut kn;
let mut ukn;
let mut compute_batch_idx;
let mut known_batch_idx;
such like
let a,b,c,d;
I don't understand this question. Could you describe what the problem is? Perhaps this post is incomplete?
I believe that this was a query about declaring multiple variables in a single let
statement. To me the question implies that the author does not understand that let
pattern-matches, rather than being a simple declarator as in the Basic programming language. C.f. Listing 18-3 in chapter 18 of TRPL.
Yup! You can't declare multiple names in one line and not ever give them a value. However, you can do so if you give them a value eventually:
let (x, y, z);
x = 1;
y = String::from("abc");
z = SomeStruct { contents };
However! This is just pattern matching in disguise, since let statements wouldn't work with this:
let x, y, z;
You need to make the LHS place value (I think that's the proper term?) a single value, hence we package it up in an inconspicuous tuple (...)
.
You can extend the declaration to include types:
let (x, y, z): (usize, String, SomeStruct);
(And then that would compile all on its own)
Note however, that unlike certain other languages such as C/C++/C#, you can't do something like this inside a structure declaration. IE, the following is not allowed:
struct Foo {
(x, y, z): (usize, usize, usize),
}
Since pattern matching doesn't apply in struct
declarations. It does however, in struct
deconstructions:
struct Foo {
vals: (usize, usize),
}
let Foo { vals: (x, y) } = Foo { vals: (1, 2) };
Perhaps it is also worth pointing out that the mut
modifier can be interspersed in the pattern as desired.
let (mut a, b, mut c) = (123, 456, "foo");
a = 42; // This is fine
b = 13; // ERROR! `b` is immutable
c = "bar"; // This is fine
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.