小编наб*_*эли的帖子

是`iter().map().sum()`和`iter().fold()`一样快?

编译器是否为iter().map().sum()和生成相同的代码iter().fold()?最终他们实现了相同的目标,但第一个代码将迭代两次,一次为map一次,一次为sum.

这是一个例子.哪个版本更快total

pub fn square(s: u32) -> u64 {
    match s {
        s @ 1...64 => 2u64.pow(s - 1),
        _ => panic!("Square must be between 1 and 64")
    }
}

pub fn total() -> u64 {
    // A fold
    (0..64).fold(0u64, |r, s| r + square(s + 1))
    // or a map
    (1..64).map(square).sum()
}
Run Code Online (Sandbox Code Playgroud)

什么是看待装配或基准测试的好工具?

rust

9
推荐指数
2
解决办法
2033
查看次数

允许Rust的格式!()系列中未使用的命名参数

鉴于:

format!("{red}{}{reset}", "text", red = "RED", blue = "BLUE", reset = "RESET");
Run Code Online (Sandbox Code Playgroud)

编译器退出时出错:

error: named argument never used
  --> example.rs:1:47
   |
 1 |         format!("{red}{}{reset}", "text", red = "RED", blue = "BLUE", reset = "RESET");
   |                                                        ^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

通常,这不应该是一个问题,因为blue应该删除,但我的usecase是一个包装宏(简化):

macro_rules! log {
    ($fmt:expr, $($arg:tt)*) => {
        println!($fmt, $($arg)*, blue = "BLUE", red = "RED", reset = "RESET");
    };
}
Run Code Online (Sandbox Code Playgroud)

有时,它被这样使用(简化),但有时候使用不同的颜色,你得到了要点:

log!("{red}{}{reset}", "text");
Run Code Online (Sandbox Code Playgroud)

编译器退出时出现类似错误:

error: named argument never used
  --> example.rs:3:26
   |
3  |         println!($fmt, $($arg)*, blue = "BLUE", red = …
Run Code Online (Sandbox Code Playgroud)

rust

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

为什么要打印"制作:没有什么可以为'所有'做的."?

这是一个"Hello.c"模块和"Makefile".make从woking目录执行后,我收到以下消息:

make:没有什么可以为'all'做的.

这是"Hello.c"文件:

#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lakshmanan");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void) {
  printk(KERN_INFO "Hello world!\n");
  return 0;    // Non-zero return means that the module couldn't be
}

static void __exit hello_cleanup(void) {
    printk(KERN_INFO "Cleaning up module.\n");
}   

module_init(hello_init); 
module_exit(hello_cleanup);
Run Code Online (Sandbox Code Playgroud)

和"Makefile":

obj-m += hello.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean: …
Run Code Online (Sandbox Code Playgroud)

c makefile gnu-make linux-device-driver linux-kernel

8
推荐指数
3
解决办法
5万
查看次数

为什么我的析构函数被调用,我该如何修复它

嘿伙计,所以我在我的c ++程序中遇到了我的析构函数问题.当我运行程序并接受用户输入时,它突然调用析构函数,然后cout甚至可以在语句中打印.假设用户输入将是一个,因为我设计了这部分代码只接受输入1.我认为析构函数在你离开范围时被调用所以我认为析构函数应该在cout之后被调用至少我将在下面评论的if语句,让你们更容易阅读.如果有人可以解释我的错误并纠正它会很棒!在我的头文件中我有

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

class creature{
public:
    creature();//default constructor
    creature(int a);
    ~creature();//desconstructor 
    string getName();//accessor for the name
    static int getNumObjects();
private:
    string name;
    int happy_level;
    static int count;
};
Run Code Online (Sandbox Code Playgroud)

在我的实现文件中,我有

#include "creature.h"

int creature::count=0;//initialize static member variable

creature::creature(){//default constructor
    name="bob";
    ++numberobject;

    cout<<"The default constructor is being called"<<endl;
}

creature::creature(int a)
{
    if(a==1)
    {
        name="billybob";

    }


    else if(a==2)
    {
        name="bobbilly";

    }

    else if(a==3)
    {
        name="bobbertyo";
        happy_level=1;
    }
}

creature::~creature()
{
    cout<<"The destructor …
Run Code Online (Sandbox Code Playgroud)

c++ constructor

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

什么时候对enable_if_t表达式进行评估?

这是我的代码:

template<class T, std::enable_if_t<T::value == 1> * = nullptr>
void foo(T) {
    std::cout<<"test"<<std::endl;
} // #1

template<class T, std::enable_if_t<T::value != 1> * = nullptr>
void foo(T) {} // #2

class test{
  public:
    constexpr static int value = 1;

    test() {}
};


int main() {
    test p;
    foo(p);
}
Run Code Online (Sandbox Code Playgroud)

因为std::enable_if_t<T::value != 1>要求我value是静态的constexpr,我假设它在编译期间被评估(我需要确认).但由于它是一个模板,它将取决于T,但我也在主要上有这个,它们不是constexpr:

int main() {
    test p;
    foo(p);
}
Run Code Online (Sandbox Code Playgroud)

输出:

test
Run Code Online (Sandbox Code Playgroud)

那么此时如何评估事物(包括函数的初始化顺序)呢?由于编译器需要决定创建哪个foo版本.

c++

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