【问题标题】:Can't use code example from dlang.org无法使用来自 dlang.org 的代码示例
【发布时间】:2013-12-10 21:29:53
【问题描述】:

我有无法编译的代码

import std.string;
import std.net.curl;

int main(string[] argv)
{
    string a = get("http://google.com");
    return 0;
}

Error: cannot implicitly convert expression (get("http://google.com", AutoProtocol())) of type char[] to string

http://dlang.org/phobos/std_net_curl.html 中有代码

import std.net.curl, std.stdio;

// Return a string containing the content specified by an URL
string content = get("dlang.org");

为什么我不能编译相同的代码?

【问题讨论】:

    标签: d


    【解决方案1】:

    示例错误 - get 返回 char[] 而不是 string。区别在于字符串是不可变的,而 char 不是。

    两种修复方法:

    char[] a = get("http://google.com"); // or you could do auto a = ... instead
    

    string a = get("http://google.com").idup;
    

    第二个生成数据的不可变副本。第一个使用适当的类型。

    【讨论】:

    • 我会推荐通用的 std.conv.to!string 而不是 .idup。我想是个人喜好。
    猜你喜欢
    • 2021-02-06
    • 2011-09-25
    • 1970-01-01
    • 2013-11-21
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多