我有一个if
语句需要检查嵌套中是否存在值Option
。该声明目前看起来像
if my_vec.get(0).is_some() && my_vec.get(0).unwrap().is_some() {
// My True Case
} else {
// My Else Case
}
Run Code Online (Sandbox Code Playgroud)
我觉得这是检查这个潜在嵌套值是否存在的业余方式。我希望在Option
从数组中获取数组时保持安全,因为它可能存在也可能不存在,并且在解开数组Option
本身时也是如此。我尝试使用and_then
类似的运算符,但没有任何运气。
如果您去年一直活跃在编程社区中,那么您肯定听到过对 Rust 执行速度和性能以及Result
Rust 中出色类型的赞扬。
我可能应该提一下,我不是 Rust 开发人员。尽管如此,或者甚至可能正因为如此,我想知道如果 Rust 使用这个 Result 类型,它怎么会如此高效,因为就我而言,这个类型被实现为所谓的union
在C . 它在联合中包含一个错误和一个返回值,其中在给定时间只有一个有效。该类型还包含一个标志,指示结果是否包含错误或值。
如果我计数正确,并且假设错误存储为指针或引用(例如,在 64 位系统上占用内存中的 8 个字节),则联合最少 8 个字节 + 标志一个字节,使得9字节内存。
现在,通过填充,我假设在大多数系统上,这将被重新对齐以占用 12 个字节。相比之下,返回 int(32) 仅分配 4 个字节。因此,使用 Result 分配的内存应该是使用 int 的三倍。
这不是极大地浪费内存吗?我想象在循环中运行它,这会增加很多。
我不太明白为什么有人会声称 Rust 性能超级好,而 Result 却占用了那么多内存?
我知道有一些优化技巧可以减少内存使用量,例如使用带有选项的 NotZeroInt 使编译器可以使用零作为标志,从而避免为标志提供额外的字节。但对于大多数类型来说这并不适用,不是吗?
如果有人有进一步的见解,我很想听听。请注意,我不是 Rust 开发人员,出于好奇而提出这个问题,正如我在尝试移植此功能的库中观察到的那样,内存使用量急剧增加。
当然,RustResult<T, E>
和Option<T>
类型比某些移植库进行了更好的优化,但我无法想象这如何不会影响程序性能。
我正在努力完成一项任务.打印4个完整数字,介于1到10000之间.
在数论中,一个完整数是一个正整数,等于其正确的除数之和,即除数除数之外的正除数之和.
这是我的代码:
public class PerfectNumbers
{
public static void main(String[] args)
{
// Perfect numbers!
for (int number = 1; number < 10000; number++)
{
int sum = 0;
int i = 1;
while (i < number)
{
if (number % i == 0)
{
sum += i;
i++;
}
else
{
i++;
continue;
}
if (sum == number)
{
System.out.println(number);
}
else
{
continue;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
6
24 <--- This one is …
Run Code Online (Sandbox Code Playgroud) base.css
base.html
<link rel="stylesheet" type="text/css" href="/base.css" />
C:\Users\user\S\St\static\base.css
C:\Users\user\S\St\Templates\base.html
当我运行我的网页时,只显示html.当我将浏览器直接指向css文件时,我得到的是文件中写的文字,如下所示:
body{background-color:blue}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么这不起作用.
我使用Debian.我想检查我的PHP版本.我键入但是错误
php
Run Code Online (Sandbox Code Playgroud)
代替
php - v
Run Code Online (Sandbox Code Playgroud)
.然后"php命令行"将不再接受任何命令.没有错误没有反应.
我可以使用kill命令停止这个过程,但我想知道,没有选项的'php'是什么.如何停止它,没有杀死命令.
我正在使用c ++在unix中做一些工作.我试图在我的两个程序之间创建一个命名管道,并在它们之间来回发送一些文本.一切编译都很好,但当我调用我的系统运行server.cpp时,我收到此错误消息.
./server.cpp: line 8: syntax error near unexpected token '('
./server.cpp: line 8: 'void test()'
Run Code Online (Sandbox Code Playgroud)
导致此错误的原因是什么?我对unix或命名管道没有多少经验,所以我有点难过.
这是我的代码
client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int fd;
mkfifo("home/damil/myPipe", 0666);
fd=open("home/damil/myPipe", O_WRONLY);
write(fd,"test", sizeof("test")+1);
system("./server.cpp");
close(fd);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void test()
{
int fd;
char * comm;
fd = open("home/damil/myPipe", O_RDONLY);
read(fd, comm, 1024);
printf(comm);
close(fd);
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
#include <ctype.h>
void trinanguloNumeros(){
char caracter = 'y';
int iTamano;
while (caracter == 'Y' || caracter == 'y') {
printf("Realizar triangulo:\n");
printf("De que tamaño desea su triangulo? [1-20]");
scanf("%i", &iTamano);
int i,j;
for(i=1; i<= iTamano; i++ ){
for(j=1; j <= i; j++){
printf("%i", j);
}
printf("\n");
}
printf("Desea Intentar nuevamente? [y/n]\n");
scanf("%c", &caracter);
printf("%c", caracter);
}
printf("Termina!!");
}
int main(void){
trinanguloNumeros();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
scanf
var 的第二个caracter
不起作用并打破 while
循环.我不知道为什么这会打破循环并且不再设置变量.
我试图让一个程序来查找和此刻的我试图改变一个字符串替换一些文本"hello how are you"
,以"hello bow are you"
作为测试.
所以首先我发现"how"
使用char *substring = strstr(mystring, newstr);
哪个返回指针"(this position)how are you"
现在我不知道如何更改接下来的3个字母.我可以strlen(newstr)
为我替换的字符串的长度,"how"
但我找不到从指针newstr开始更改mystring的方法.
打印出代码时,它会运行,但似乎不运行该coinflip()
功能.目前只试图打印出第一匹马串,随机前进.
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
string h0 = "0................";
string h1 = "1................";
string h2 = "2................";
string h3 = "3................";
string h4 = "4................";
int position0 = 0;
string coinflip0(string h0);
int main(){
cout << "Press Enter to begin! " <<endl;
cin.ignore();
std::cout << h0 << endl; //print string
cout << h1 << endl;
cout << h2 << endl;
cout << h3 << endl;
cout << h4 << endl;
// …
Run Code Online (Sandbox Code Playgroud) 我想在给定的字符串列表中查找常见字符而不使用集合库。有人可以帮忙吗?
输入:
strings = ["apple", "app", "ape"]
Run Code Online (Sandbox Code Playgroud)
输出:
result - ap
Run Code Online (Sandbox Code Playgroud) c ×3
c++ ×2
rust ×2
css ×1
html ×1
if-statement ×1
java ×1
linux ×1
memory ×1
named-pipes ×1
performance ×1
php ×1
python ×1
python-3.x ×1
scanf ×1
union ×1
unix ×1
web ×1