我有这样的代码:
// This class cannot be changed
class VendorApi {
static void func1(char x) {}
static void func1(int x) {}
static void func1(float x) {}
static void func1(double x) {}
}
class Main {
static <T> void my_func(T arg) {
// much of code, which uses T
// ...
VendorApi.<T>func1(arg);
}
public static void main(String args[]) {
// call my_func for each type (char, int, float, double)
// ...
int i = 1;
my_func(i);
char c = 1;
my_func(c);
} …Run Code Online (Sandbox Code Playgroud) Struct S实际上可能是一些大数据,例如大数据Vec.如果我有一个线程并且在创建线程后不使用数据,我可以将数据移动到它,但是有两个线程(或在主线程中使用相同的数据),这是不可能的.
struct S {
i : i32,
}
fn thr(s : &S)
{
}
fn main()
{
let s1 = S { i:1 };
thr(&s1);
let t1 = std::thread::spawn(|| thr(&s1)); // does not work
let t2 = std::thread::spawn(|| thr(&s1)); // does not work
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)