最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Zig expected type '[]usize', found 'type' - Stack Overflow

programmeradmin3浏览0评论

I'm trying to learn Zig using the website /. I was solving this exercise, which requires us to find the nth prime number.

My idea was to generate all of them during comptime and return as required; this is my current my code:

const std = @import("std");
const mem = std.mem;

fn sieve(buffer: []usize, comptime limit: usize) []usize {
    if (limit < 2)
        return buffer[0..0];
    const sqrt_limit = std.math.sqrt(limit);
    var bitset = std.StaticBitSet(limit + 1).initFull();
    var n: usize = 3;
    var i: usize = 1;
    
    
    buffer[0] = 2;
    while(n <= sqrt_limit): (n += 2) {
        if (!bitset.isSet(n))
            continue;

        buffer[i] = n;
        i += 1;
        var j = 2 * n;
        while (j <= limit): (j += i) {
            bitset.unset(j);
        }
    }
    while (n <= limit) : (n += 2) {
        if (!bitset.isSet(i))
            continue;

        buffer[i] = n;
        i += 1;
    }
    return buffer[0..i];
}

const buffer_size = std.math.maxInt(usize);
const primes_buffer = [buffer_size]usize;
const primes = sieve(primes_buffer, buffer_size - 1);

pub fn prime(allocator: mem.Allocator, number: usize) !usize {
    _ = allocator;
    return primes[number-1];
}

But when I try to compile this, I'm getting the following error:

nth_prime.zig:37:22: error: expected type '[]usize', found 'type'
nth_prime.zig:4:18: note: parameter type declared here

I don't understand why primes_buffer is of type type here; shouldn't it be a []usize?

I'm trying to learn Zig using the website https://exercism.org/. I was solving this exercise, which requires us to find the nth prime number.

My idea was to generate all of them during comptime and return as required; this is my current my code:

const std = @import("std");
const mem = std.mem;

fn sieve(buffer: []usize, comptime limit: usize) []usize {
    if (limit < 2)
        return buffer[0..0];
    const sqrt_limit = std.math.sqrt(limit);
    var bitset = std.StaticBitSet(limit + 1).initFull();
    var n: usize = 3;
    var i: usize = 1;
    
    
    buffer[0] = 2;
    while(n <= sqrt_limit): (n += 2) {
        if (!bitset.isSet(n))
            continue;

        buffer[i] = n;
        i += 1;
        var j = 2 * n;
        while (j <= limit): (j += i) {
            bitset.unset(j);
        }
    }
    while (n <= limit) : (n += 2) {
        if (!bitset.isSet(i))
            continue;

        buffer[i] = n;
        i += 1;
    }
    return buffer[0..i];
}

const buffer_size = std.math.maxInt(usize);
const primes_buffer = [buffer_size]usize;
const primes = sieve(primes_buffer, buffer_size - 1);

pub fn prime(allocator: mem.Allocator, number: usize) !usize {
    _ = allocator;
    return primes[number-1];
}

But when I try to compile this, I'm getting the following error:

nth_prime.zig:37:22: error: expected type '[]usize', found 'type'
nth_prime.zig:4:18: note: parameter type declared here

I don't understand why primes_buffer is of type type here; shouldn't it be a []usize?

Share Improve this question asked Feb 7 at 12:15 João AreiasJoão Areias 1,44818 silver badges45 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
const primes_buffer = [buffer_size]usize;

[buffer_size]usize is a type. You're assigning it to a variable. It's only logical that primes_buffer would remain a type.

You probably wanted to do

const primes_buffer: [buffer_size]usize = undefined;
发布评论

评论列表(0)

  1. 暂无评论