Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 锈蚀及;str转换为&';静态和动态;str_Rust_Type Conversion - Fatal编程技术网

Rust 锈蚀及;str转换为&';静态和动态;str

Rust 锈蚀及;str转换为&';静态和动态;str,rust,type-conversion,Rust,Type Conversion,我正在从应用程序的args接收一个&str,但我需要这个值作为&'static&str。 我如何转换它 fn-fun(变量:&'static&str){} fn main(){ 让var:&str=“temp”; 乐趣(var); } 错误[E0308]:类型不匹配 -->src/main.rs:102:9 | 102 |匹配(“静态文件”).unwrap()的.value_, |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我正在从应用程序的args接收一个
&str
,但我需要这个值作为
&'static&str
。 我如何转换它

fn-fun(变量:&'static&str){}
fn main(){
让var:&str=“temp”;
乐趣(var);
}
错误[E0308]:类型不匹配
-->src/main.rs:102:9
|
102 |匹配(“静态文件”).unwrap()的.value_,
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
=注意:应为类型`&'static&str`
找到类型“%str”`
错误:由于上一个错误而中止

@更多信息
//文件:main.rs
外部板条箱;
使用clap::{App,Arg};
mod服务器;
fn main(){
让app=app::新建(“我的应用”)
阿格先生(
Arg::带有_名称(“静态文件”)
.help(“设置客户端静态文件的绝对路径”)
.默认值(“/var/www/client”),
)
let matches=app.get_matches();
让s=server::start(
匹配(“静态文件”).unwrap()的.value_,
);
//文件:server.rs
使用actix_文件作为fs;
使用actix_web:{guard,web,App,HttpResponse,HttpServer};
酒吧fn开始(
静态文件路径:&'static&str,
)->io::结果{
让sys=actix\u rt::System::new(“myappsvr”);
HttpServer::新建(移动| |{
设sfp=move | | static_files_path;
App::new()
.service(fs::Files::new(“/”,sfp()).index_文件(“index.html”))
})
(...)
.start();
sys.run();
}
main.rs
I中启动http服务器(actix)。带有静态文件的目录路径必须作为参数传递


App::new().service(fs::Files::new(“/”,sfp()).index_file(“index.html”)
需要
&'static&str
来代替
spf()

当编译器说您需要
&'static
时,这并不是真的意思。它试图告诉您这里不允许临时引用

&'static str
是一种特殊情况,基本上是内存泄漏。在99.99%的情况下,泄漏内存是个坏主意,因此编译器的建议是愚蠢的。不要尝试使用
&'static str
。而是使用拥有的
字符串

请记住,
&str
本身不是一个字符串,而是存储在别处的某些字符串的只读视图

所以真正的问题是您使用了一个临时的
&str
,而您应该使用一个永久存储的
字符串


但是,对于0.01%的可能泄漏字符串内存的情况,下面介绍了如何:

let leaked:&'static str=Box::leak(“hi”.to_string()。to_boxed_str());

我希望您可以用相反的方法来实现这一点,但不能真正将“动态”值转换为静态可用的值。听起来这可能是一个XY问题。为什么您需要一个
&'static&str
?问题代码可以通过做一个小改动来解决:
让var=&“temp”
但是我认为这对您的实际代码不起作用,这就是我们需要更多信息的原因。@ÖmerErden
“temp”
是一个常量值,这意味着它受限制。你可以根据需要获取任意多个
&
引用,只要你不强制缩短它们,它们都将具有
'静态
生存期。显然看起来像是X/Y问题。不要求它的参数是
'静态
;这会很奇怪。
.
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── main.rs
│   └── server.rs
// file: server.rs
use actix_files as fs;
use actix_web::{guard, web, App, HttpResponse, HttpServer};

pub fn start(
    static_files_path: &'static &str,
) -> io::Result<()> {

    let sys = actix_rt::System::new("myappsvr");

    HttpServer::new(move || {
        let sfp = move || static_files_path;
        App::new()
            .service(fs::Files::new("/", sfp()).index_file("index.html"))
    })

    (...)

    .start();

    sys.run();
}