小编Her*_*tar的帖子

localStorage事件侦听器不会在Chrome中为本地文件触发

我需要在localStorage更改时收到通知.此代码在Firefox 24中运行良好,但在Chrome 29(或30)或IE 10中不起作用.它也可以在实时服务器上运行,但在我使用本地文件(file:///)进行测试时却无效.

这是代码:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#submit').click(function() {
                console.log('Clicked');
                if($('#username').val() != "")
                    localStorage.setItem('someItem', 'someValue');
            });
            $(window).bind('storage', function(e) {
                alert('change');
            });


        });
    </script>
</head>
<body>
<input type="text" id="username" name="username" value="" />
<a href="#" id="submit">Click me</a>
<p id="result"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这在Chrome中有什么问题?我根据需要打开两个标签.

javascript jquery html5 google-chrome local-storage

10
推荐指数
1
解决办法
6892
查看次数

如何按 2 个或多个字段对结构体 Vec 进行排序?

example

struct MyStruct{
    row: u8,
    column: u8
}

let my_vector = a Vec<MyStruct> with like 100 items in it
Run Code Online (Sandbox Code Playgroud)

可以说我有一个像这样的简单设置^。我想my_vector按行然后按列对 100 个项目的列表进行排序,这样我的向量就会看起来像sample 1而不是sample 2.

sample 1

my_vector = vec![
MyStruct { row: 10, column: 1 },
MyStruct { row: 10, column: 2 },
MyStruct { row: 10, column: 3 }, ]
Run Code Online (Sandbox Code Playgroud)

sample 2

my_vector = vec![
MyStruct { row: 10, column: 3 },
MyStruct { row: 10, column: 1 },
MyStruct { row: 10, …
Run Code Online (Sandbox Code Playgroud)

rust

10
推荐指数
3
解决办法
6273
查看次数

#[should_panic] 不接受预期的常量恐慌消息

运行 rust 单元测试时,使用属性宏 #[should_panic(expect = )] 来断言测试因正确的错误消息而发生恐慌(这意味着它在您希望它发生恐慌的行处发生恐慌,而不是因为不同的错误。

但是,在使用在errors.rs 文件中定义的标准化错误消息的应用程序中,不可能将错误消息的常量作为expect 的参数传递。

这:

pub const ERR_001: &str = "ERR_001 message";

#[test]
#[should_panic(expected = ERR_001)]
fn test_function() {

        //test code here

}

Run Code Online (Sandbox Code Playgroud)

产生以下错误:

error: expected unsuffixed literal or identifier, found `ERR_001`
  --> staking/src/storage.rs:74:31
   |
74 |     #[should_panic(expected = ERR_001)]
   |                    
Run Code Online (Sandbox Code Playgroud)

有没有办法将常量或变量传递给 #[should_panic(expected = )] 宏?或者是否有必要总是写消息而不是?

testing unit-testing rust rust-macros

10
推荐指数
1
解决办法
546
查看次数

我应该如何理解&amp;**self in Box

boxed.rs 中的代码

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
    type Target = T;

    fn deref(&self) -> &T {
        &**self
    }
}
Run Code Online (Sandbox Code Playgroud)

以我的理解:

  • self-> &Box,
  • *self-> Box
  • **self-> *Box,将调用:*(Box.deref())? 这不会导致递归吗?

我创建一个测试代码:

impl<T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &**self
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,Rust 返回给我:fatal runtime error: stack overflow

那么&**self …

rust

9
推荐指数
1
解决办法
380
查看次数

Fn 闭包类型的静态生命周期是什么意思?

如果我按照 rustc 的指示操作,并将绑定更改为,以下错误就会消失

where F: Fn() -> () + 'static

pub struct Struct(Box<dyn Fn() -> ()>);
pub fn example<F>(f: F)
where
    F: Fn() -> ()
{
    Struct(Box::new(|| ())); // ok
    Struct(Box::new(f)); // error: The parameter type `F` may not live long eneough
                         // help: consider adding an explicit lifetime bound: `F: 'static`
}
Run Code Online (Sandbox Code Playgroud)

但是,我只是不明白“静态”在这里意味着什么。这似乎并不意味着封闭本身会永远存在。

至少,关闭似乎不会永远存在。如果我让闭包拥有一些数据,那么该数据会在某个时刻被删除,这让我怀疑闭包已被删除:

pub struct Struct(Box<dyn Fn() -> ()>);

#[derive(Debug)]
struct DropMe;
impl Drop for DropMe {
    fn drop(&mut self) {
        println!("dropped");
    }
}

/// prints: …
Run Code Online (Sandbox Code Playgroud)

closures lifetime rust

8
推荐指数
2
解决办法
2448
查看次数

处理多个函数参数类型的惯用方法

我对 Rust 相当陌生,我需要一些关于如何处理 Rust 中一个参数的多种类型的指导。我什至不知道这是否可能。

我有一个函数可以进行大量计算,并且其一些指令可能会根据参数的类型而有所不同。

在 Python 中,它会这样写:

def foo(bar):
   # Do a bunch of computations
   if isinstance(bar, TYPE_A):
       # Do this
   elif isinstance(bar, TYPE_B):
       # Do that
Run Code Online (Sandbox Code Playgroud)

我什至不知道这在 Rust 中是否可行,甚至不推荐。函数体相当大,并且使用match函数体内的语句来处理这种基于类型的条件似乎比使用两个支持两种不同类型的非常相似的函数更干净。

我不是在这里寻找泛型。实际上,就我而言,Type_A是一个 Rustndarray实例,并且TYPE_B是一个自定义struct.

rust

8
推荐指数
1
解决办法
3906
查看次数

带范围的飞镖开关

飞镖有没有办法在范围之间切换。除了我什么也找不到

例如:

switch(response.statusCode) {
  case 200..300: return "OK";
  case 400..500: return "Error";
  default: break;
}
Run Code Online (Sandbox Code Playgroud)

dart

7
推荐指数
2
解决办法
1025
查看次数

如何在字符串中包含变量?

我认为这应该很简单,但我的 Google 能力很弱。我正在尝试使用 u32 变量在 Rust 中构建一个字符串。在 C 中,我会使用 snprintf,如下所示:

使用 C 中的变量(如 printf 中的变量)创建 char 数组

但我找不到任何关于如何在 Rust 中做到这一点的信息。

rust

7
推荐指数
3
解决办法
2万
查看次数

交叉编译错误`ld:未知选项:--as-needed`

在 Apple M1 中,当我为 Linux X86_64 编译 Rust 代码时,出现错误ld: unknown option: --as-needed

\n
~/WORKDIR/rust/helloworld \xe2\x8c\x9a 20:24:21\n$ cargo build --target x86_64-unknown-linux-gnu\n   Compiling helloworld v0.1.0 (/Users/fudenglong/WORKDIR/rust/helloworld)\nerror: linking with `cc` failed: exit status: 1\n  |\n  = note: "cc" "-m64" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.19lk9s4zgix5szcj.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.1m3icqtseu62rr0o.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.2fy0lykh93934k3s.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.31qjfvz3m1ka31b5.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.3vrcx702oe0jhs7q.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.53gv3v3m2hy2kzin.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.cc5ryu0ilsy4zts.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.zedrxxva8ppoktz.rcgu.o" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps/helloworld-42c0bc1b0d56de10.2l2aexzp1dl763ew.rcgu.o" "-Wl,--as-needed" "-L" "/Users/fudenglong/WORKDIR/rust/helloworld/target/x86_64-unknown-linux-gnu/debug/deps" "-L" "/Users/fudenglong/WORKDIR/rust/helloworld/target/debug/deps" "-L" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-1b64d5fe7a3c3d7f.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-e7f86684b9679284.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-10a87791239bd676.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-0e6cad6dd623d38e.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-9d05dd05a4d51b33.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-a60ff138fe1adfad.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0e9344458c09713e.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-ef3b808d96f639bf.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-e3ce420901586b53.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-546bece8dd1bea42.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9b358af16d7d7aa1.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-554cd499fe1a3b52.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-d105877dbe329cd1.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-8fec8a74c706e4b4.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-e869a247063ed69a.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-4896c4057dc9553a.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-2a6a2797f7a73818.rlib" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-0e3656b1fda5fd7b.rlib" "-Wl,--end-group" "/Users/fudenglong/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-1f103368fa522bc0.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" …
Run Code Online (Sandbox Code Playgroud)

cross-compiling rust apple-m1

7
推荐指数
2
解决办法
3602
查看次数

使用未申报的板条箱或模块

当我serde从文件中读取 json 时,出现以下错误:

Failed to resolve: use of undeclared crate or module serde_json

这是代码:

use serde::Deserialize;


fn main() {
  let file = fs::File::open("./feed.json")
      .expect("file should open read only");
  let reader = BufReader::new(file);
  let json = serde_json::from_reader(reader)
      .expect("file should have FirstName key");
  let feed_url = json.get("2.0")
      .expect("file should have FirstName key");
  println!("{}", reedFeed(feed_url));
}
Run Code Online (Sandbox Code Playgroud)

是有关此功能的文档。我在 ubuntu 上并使用 intellij 作为我的 ide。我在这里缺少什么?

rust

7
推荐指数
1
解决办法
2万
查看次数