鉴于下面的代码足够明确,我想为什么 Rust 要求类型注释?
pub struct Score {
pub id: Option<String>,
}
fn main() {
let rows = vec![
Score{
id: None,
},
Score{
id: Some("test".to_string()),
},
];
let existing_scores = rows
.iter()
.map(|o| o.id.unwrap_or_default())
.collect();
dbg!(existing_scores);
}
Run Code Online (Sandbox Code Playgroud) 我想使用循环计算 1 到 n 的总和,这是我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
long sum = 0;
int n = atoi(argv[1]);
for(int i = 1; i<= n; i++)
sum += i;
printf("Sum of serial from 1 to %d = %ld\n", n, sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我用小 n 运行程序时,结果是正确的,但当我用大数字运行时,结果是错误的。这是我运行此命令时的结果:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
long sum = 0;
int n = atoi(argv[1]);
for(int i = 1; i<= n; i++)
sum += i;
printf("Sum of …Run Code Online (Sandbox Code Playgroud) 我学习了两种将元素插入到vector.
由于我的工作有时间限制,我一直想知道哪种方式更快。
方法一:
int n;
cin>>n;
vector<int> v(n);
for(int i = 0;i<n;i++){
cin>>v[i];
}
Run Code Online (Sandbox Code Playgroud)
方法二:
int n;
cin>>n;
vector<int> v;
for(int i = 0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}
Run Code Online (Sandbox Code Playgroud)
如果您有更好的方法推荐,将不胜感激!
我试图用 C++ 测试非常基本的面向对象,但遇到了问题。我正在写以下课程:
演员等级:
#pragma once
#include <string>
class Actor
{
private:
void setName(std::string inputName);
public:
std::string name{};
Actor(std::string inputName = "Actor");
std::string getName();
};
Run Code Online (Sandbox Code Playgroud)
#include "Actor.h"
Actor::Actor(std::string inputName)
{
setName(inputName);
}
void Actor::setName(std::string inputName)
{
name = inputName;
}
std::string Actor::getName()
{
return name;
}
Run Code Online (Sandbox Code Playgroud)
字符类
#pragma once
#include "Actor.h"
class Character : public Actor
{
public:
int maxHealth{};
int currentHealth{};
Character(std::string inputName = "Character", int inputHealth = 100);
int getCurrentHealth();
};
Run Code Online (Sandbox Code Playgroud)
#include "Character.h"
Character::Character(std::string inputName, int inputHealth) : …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
let routes = match env::var("ENV") {
Ok(el) => {
if el == "PROD" {
routes![upload_image]
} else {
routes![get_token, callback, form, upload_image, refresh]
}
},
_ => routes![get_token, callback, form, upload_image, refresh],
};
Run Code Online (Sandbox Code Playgroud)
该函数env::var返回一个Result<String, VarError>. 我想知道我上面的代码是否可以简化如下:
let routes = match env::var("ENV") { // E: mismatched types: this expression has type `Result<std::string::String, VarError>
Ok("PROD") => { // E: mismatched types: expected `String`, found `&str`
routes![upload_image]
},
_ => routes![get_token, callback, form, upload_image, refresh],
};
Run Code Online (Sandbox Code Playgroud)
String但是,我收到有关“不匹配类型:预期,发现”的错误 …
我需要创建 10,000 个随机整数的文件进行测试。我将在 Python 和 C 中使用该文件,因此我不能将数据表示为字符串,因为我不希望在 C 中产生整数转换的额外开销。
在 Python 中,我可以使用该方法struct.unpack将文件转换为整数,但无法使用该write()方法将其写入文件以在 C 中使用。
Python 有没有办法只将整数而不是整数作为字符串写入文件?我使用过print(val, file=f)and f.write(str(val)),但在这两种情况下它都会写入一个字符串。
这是我现在所在的位置:
file_root = "[ file root ]"
file_name = file_root + "Random_int64"
if os.path.exists(file_name):
f = open(file_name, "wb")
f.seek(0)
for _ in range(10000):
val = random.randint(0, 10000)
f.write(bytes(val))
f.close()
f = open(file_name, "rb")
wholefile = f.read()
struct.unpack(wholefile, I)
Run Code Online (Sandbox Code Playgroud)
我的unpack格式字符串错误,所以我现在正在处理。我对此不太熟悉struct.unpack。
我做了一个简单的项目,试图了解如何ParameterizedTest工作ValueSource。
从下图中它找到了导入路径,但是当我尝试运行代码时它会抛出错误:
还有 gradle 文件:
这是整个项目的链接。
我有这个代码:
x = [1,2,3,4,5, 'hole']
try:
if type(x) == list:
print("all ok")
else:
raise Exception('Variable is not a list')
except Exception as error:
print('Caught an error: ' + repr(error))
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在检查变量file是否实际上是一个列表。如果不是,则将引发异常。确切地说,它是 aValueError但我只是将其概括为Exception.
但是,我需要更具体。我还希望我的 setter 检查file列表中的元素是否都是整数。
任何人都可以帮助我如何解决这个问题并引发另一个异常:“列表中的所有元素都不是整数”?
我有这段代码可以创建5个字母的所有可能组合,并将它们分配给具有相同名称的变量:
import string
table = string.ascii_letters
table = list(table)
for i in table:
exec("%s = '%s'" % (i,i))
for t in table:
exec("%s = '%s'" % (i+t,i+t))
for k in table:
exec("%s = '%s'" % (i+t+k,i+t+k))
for m in table:
exec("%s = '%s'" % (i+t+k+m,i+t+k+m))
for h in table:
exec("%s = '%s'" % (i+t+k+m+h,i+t+k+m+h))
Run Code Online (Sandbox Code Playgroud)
但是它很大,而且不容易阅读。如何使它更紧凑?
我刚刚从 xcode 10.1 升级到 xcode 11.2.1。现在,每当我在 swift 3.0 中构建的 xcode 11.2.1 中打开现有项目时,它都会显示“不受支持的 Swift 版本”。
请任何人都可以给我一个简短的描述。