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 但它没有任何价值可以借鉴_Rust - Fatal编程技术网

Rust 但它没有任何价值可以借鉴

Rust 但它没有任何价值可以借鉴,rust,Rust,我的代码简化版本如下所示: fn main() { method1(true); } fn method1(cond: bool) -> (&[u8], String) { let ret1 = if cond { let a = "a string in base64".as_bytes().from_base64(); a.unwrap().as_slice() } else { [] // how can I return an empt

我的代码简化版本如下所示:

fn main() {
  method1(true);
}

fn method1(cond: bool) -> (&[u8], String) {
  let ret1 = if cond {
    let a = "a string in base64".as_bytes().from_base64();
    a.unwrap().as_slice()
  } else {
    [] // how can I return an empty array? Should I return vector instead?
  };

  (ret1, "aaa".to_string())
}
错误:

error: missing lifetime specifier [E0106]
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
test1.rs:7:28: 7:33 help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
test1.rs:7:28: 7:33 help: consider giving it a 'static lifetime
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
error: aborting due to previous error
底线是,我想从一个方法返回一个数组的元组和一个字符串(string或&str),我该怎么做呢

您可以这样做:

fn main() {
  method1(true);
}

fn method1(cond: bool) -> (&'static [u8], String) {
  // ' <-- un-confuse SO syntax highlighting...
  let ret1 = if cond {
    b"some hard-coded data"
  } else {
    b""
  };

  (ret1, "aaa".to_string())
}
fn main(){
方法1(真);
}
fn方法1(条件:bool)->(&'static[u8],字符串){

//“我不能使用像
这样的东西吗?为什么它应该是
(&'static[u8],String)
,而不是说
&([u8],String)
(&[u8],&str)
或其他东西?有没有办法在没有
b
的情况下创建
静态[u8]
静态内容:&'static[u8]=[0,1,2]
我建议你读一篇我写的文章,这篇文章相当体面地涵盖了这一领域: