无法使用dlang.org中的代码示例

Yar*_*lka 1 d

我有无法编译的代码

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
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*ppe 9

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

有两种方法可以解决它:

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

要么

string a = get("http://google.com").idup;
Run Code Online (Sandbox Code Playgroud)

第二个是数据的不可变副本.第一个使用适当的类型.