小编Gir*_*rpa的帖子

如何在 Rust 中向上或向下舍入一个数字?

如何设置地板或天花板号码?我试图使用round板条箱,但要么不起作用,要么我用错了。

use round::round_down;

fn main() {
  println!("{}", round_down(5.5f64, 0));  
}
Run Code Online (Sandbox Code Playgroud)

这会打印5.5但应该打印5

我的Cargo.toml文件包含这个:

[dependencies]
round = "0.1.0"
Run Code Online (Sandbox Code Playgroud)

rounding rust

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

如何使用eventListener获取按钮的值?

我在 HTML 中创建了具有不同值的按钮。我试图在单击时输出这些值。我正在使用querySelectorAlleventListeners 但它一直输出未定义。

let buttons = document.querySelectorAll('button');
function showNumber() {
  if (buttons.value != 5) {
    document.getElementById('screen').innerHTML = buttons.value;
  } else {
    document.getElementById('screen').innerHTML = 5;
  }
}
buttons.forEach(buttons => {
  buttons.addEventListener("click", () => {
    showNumber()
  });
}); 
Run Code Online (Sandbox Code Playgroud)

javascript addeventlistener

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

为什么相同的 32 位浮点数在 JavaScript 和 Rust 中不同?

在 JavaScript 中,38_579_240_960转换为 32 位浮点数时不会改变:

console.log(new Float32Array([38_579_240_960])[0]); // 38579240960
Run Code Online (Sandbox Code Playgroud)

在 Rust 中,它被四舍五入为38579240000. 怎么来的?

fn main() {
     println!("{}", 38_579_240_960_f32);` // 38579240000
}
Run Code Online (Sandbox Code Playgroud)

javascript floating-point precision rust

4
推荐指数
2
解决办法
183
查看次数

如何撤消 GitHub 上的最后一次提交?

我想撤消在存储库的 master 分支上推送到 GitHub 的最后一次提交,并使其看起来好像该提交从未存在过并且不会出现在提交历史记录中。

\n

我该怎么办?

\n

请注意那些投票结束 \xe2\x80\x94 的人,所提出的替代问题不必要地复杂,答案居高临下,并且散布着难以筛选的巨大文本墙。

\n

因此,为了我和子孙后代的利益,这个简单的问题和一个简单的答案。

\n

commit undo github git-revert

4
推荐指数
1
解决办法
5083
查看次数

元素的名称属性对 querySelectorAll 不可见,并且不会出现在其 HTML 中

我想选择所有名称为 的列表项'viewer'

这将返回一个空的NodeList

document.querySelectorAll('[name="viewer"]');
Run Code Online (Sandbox Code Playgroud)

但这会返回一个包含正确项目的数组:

[...document.querySelectorAll('li')].filter(({ name }) => name == 'viewer');
Run Code Online (Sandbox Code Playgroud)

列表项是这样创建的:

const listItem = document.createElement('li');
listItem.name = 'viewer';
listItem.id = 'viewer-1337';
Run Code Online (Sandbox Code Playgroud)

当我在控制台中检查它们时,我id在它们的 HTML 中看到它们,但在它们的name. 例如

<li id="viewer-1337">foo</li>
Run Code Online (Sandbox Code Playgroud)

似乎他们确实name设置了属性,但无法通过document.querySelectorAll其 HTML访问或可见。

为什么不?

const names = ['foo', 'bar'];
names.forEach(name => {
  const li = document.createElement('li');
  li.id = 'viewer-' + name;
  li.name = 'viewer';
  li.innerHTML = name;
  const ul = document.querySelector('ul');
  ul.appendChild(li);
});

const nodeList = document.querySelectorAll('[name="viewer"]');
console.log([...nodeList]);

const array …
Run Code Online (Sandbox Code Playgroud)

html javascript dom selectors-api

3
推荐指数
1
解决办法
191
查看次数

How to generate array of n equidistant points along a line segment of length x?

I ported this function from a python answer to a similar question on this site. However, although the points are equidistant from each other, they are not centered on the line.

The first point is at 0 while the last is at 83 (the end of the line segment is 100).

How do I center this row of points?

Specifically, I want the first point to be the same distance from 0 that the last point is from 100 …

javascript algorithm point line coordinates

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

如果跟随其组中的内容,为什么负前瞻不会阻止匹配?

我有以下RegExp(?<= )L.*(?!MH$)

这个想法是:

  • (?<= ) 匹配前必须有 4 个空格,但不能包含在匹配中。
  • L.*匹配必须以L字符串的其余部分开始并包括...
  • (?!MH$) 除非 MH在字符串的末尾。

最后一个条件被忽略了。以MH仍然匹配的字符串结尾。为什么?

const regExpStr = String.raw`(?<=    )L.*(?!MH$)`;
const regExp = new RegExp(regExpStr);
const str = '2.    L2 B4 RHODA 1 ADDN,SOMECITY,OK,74999 - 81 HILLCREST MH';
const match = str.match(regExp)?.[0];

console.log(match);
Run Code Online (Sandbox Code Playgroud)

javascript regex regex-lookarounds

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

如何使用表达式作为匹配条件?

如何在匹配臂中使用表达式?100我想处理等于 的情况75 + 25

fn main() {
    match 100 {
        75 + 25 => println!("100"),
//         ^ expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`
        _ => unreachable!()
    };
}
Run Code Online (Sandbox Code Playgroud)

match rust

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

“gettimeofday”不能用作函数

我在这里错过了什么,这是我的主程序,我还有一个 makefile,一切正常,错误就在这里的某个地方。

#include <iostream>
#include <observer.h>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) {
    std::fstream in;
    int gettimeofday;
    //CPUtypeandmodel
    struct timeval now;
    gettimeofday(&now, NULL);
    cout << "Status report as of : " << ctime((time_t*)&now.tv_sec) << endl;
    // Print machine name
    in.open("/proc/sys/kernel/hostname");
    string s;
    in >> s;
    cout << "Machine name: " << s << endl;
    in.close();

    return 1;
}  //end main
Run Code Online (Sandbox Code Playgroud)

当我尝试制作文件时,会发生这种情况

observer.cpp: In function ‘int main(int, char**)’:
observer.cpp:13:26: error: ‘gettimeofday’ cannot be used as a function …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
116
查看次数

如何更改向量元素的索引?

在 JavaScript 中我会这样做:

function move(arr, old_index, new_index) {
  while (old_index < 0) {
    old_index += arr.length;
  }
  while (new_index < 0) {
    new_index += arr.length;
  }
  if (new_index >= arr.length) {
    var k = new_index - arr.length;
    while ((k--) + 1) {
      arr.push(undefined);
    }
  }
  arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
  return arr;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 Rust 中完成同样的事情?

我不想使用insertandremove因为我的向量是 astd::vec::Vec<std::string::String>并且我想将它们从字面上移动到向量中的不同位置,而不是删除它们然后插入副本。

我不想交换 2 个元素。我想将一个元素的索引更改为任意其他索引,就像一个人切到队列中的任意其他位置一样。

vector rust

-2
推荐指数
1
解决办法
1321
查看次数