切片过滤时的生命周期问题

时间:2017-01-05 09:03:24

标签: rust

我想选择过滤切片而不更改类型。以下方法失败:

Class<?> actionClass = Class.forName(callbackClass,true,ClassLoader.getSystemClassLoader());

有错误

     java.lang.ClassNotFoundException: demos.callbacks.GetDayOfAWeekCallback
   at java.lang.Class.classForName(Native Method)
   at java.lang.Class.forName(Class.java:324)
   at com.example.tomek.test1.MainActivity.buttonOnClick(MainActivity.java:38)
   at java.lang.reflect.Method.invoke(Native Method)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:5198)
   at android.view.View$PerformClick.run(View.java:21147)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "demos.callbacks.GetDayOfAWeekCallback" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    ... 14 more
 Suppressed: java.lang.ClassNotFoundException: demos.callbacks.GetDayOfAWeekCallback
   at java.lang.Class.classForName(Native Method)
   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  ... 15 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

- playground

我知道use std::collections::HashSet; fn main() { let include: Option<HashSet<i8>> = Some(HashSet::new()); let y: &[i8] = &[1, 2, 3]; let z: &[i8] = match include { Some(set) => { let filtered: Vec<i8> = y.iter().filter(|&i| set.contains(i)).map(|&i| i).collect(); filtered.as_slice() }, None => y }; println!("{:?}", z); } 的寿命不够长,因为切片只是对它的引用。我试图复制或克隆或以其他方式生成结果error: `filtered` does not live long enough --> <anon>:10:9 | 9 | filtered.as_slice() | -------- borrow occurs here 10 | }, | ^ `filtered` dropped here while still borrowed ... 14 | } | - borrowed value needs to live until here 的静态版本,但没有编译。

filtered定义之外声明filtered.as_slice()无法正常工作,因为它需要filtered才能进行过滤。

我希望这种转换为迭代器,然后转换为矢量,然后转换为切片的舞蹈可能不是惯用的。所以我有兴趣知道更好的方法。

1 个答案:

答案 0 :(得分:2)

  

在z定义之外声明过滤将无效,因为它需要设置进行过滤。

我不明白为什么不。在filtered.as_slice()语句中,您从filtered借用,因此您必须确保filtered的寿命超过您为其分配的变量(z):

use std::collections::HashSet;

fn main() {
    let mut h = HashSet::new();
    h.insert(2);
    let exclude: Option<HashSet<i8>> = Some(h);
    let y: &[i8] = &[1, 2, 3];

    let filtered: Vec<i8>;
    let z: &[i8] = match exclude {
        Some(set) => {
            filtered = y.iter().filter(|&i| !set.contains(i)).map(|&i| i).collect();
            filtered.as_slice()
        },
        None => y
    };
    println!("{:?}", z);  // outputs [1, 3]
}

注意我颠倒了filter中的逻辑。