Rust 有没有办法从实现读取特性的Vec创建一个框?

Rust 有没有办法从实现读取特性的Vec创建一个框?,rust,ownership,Rust,Ownership,我正在写一段代码,使用lopdf板条箱在Vec上写PDF。然后我想通过IPP板条箱打印PDF,但它需要一个我无法创建的框 我尝试过很多事情,但我总是遇到所有权问题。 我尝试的另一件事是从原始指针创建框,但它确实使程序崩溃 让mut doc=Document::loadfile.pdf.unwrap; 让mut doc_bytes=Vec::new; //编辑 单据保存到&mut单据字节; 让doc_slice=doc_bytes.as_slice; 让buffer=std::io::BufRea

我正在写一段代码,使用lopdf板条箱在Vec上写PDF。然后我想通过IPP板条箱打印PDF,但它需要一个我无法创建的框

我尝试过很多事情,但我总是遇到所有权问题。 我尝试的另一件事是从原始指针创建框,但它确实使程序崩溃

让mut doc=Document::loadfile.pdf.unwrap; 让mut doc_bytes=Vec::new; //编辑 单据保存到&mut单据字节; 让doc_slice=doc_bytes.as_slice; 让buffer=std::io::BufReader::newdoc\u slice.to\u vec.as\u slice; 让doc_box=box::frombuffer; 让client=ipp::IppClient::newprinter URL; 让print\u job=ipp::operation::PrintJob::newdoc\u框,&用户名,无 client.sendprint_job.unwrap; 它应该在打印机上打印文件。那样说是不可能的

temporary value dropped while borrowed

creates a temporary which is freed while still in use rustc(E0716)
main.rs(6, 70): creates a temporary which is freed while still in use
main.rs(6, 100): temporary value is freed at the end of this statement
main.rs(9, 79): cast requires that borrow lasts for `'static`
在doc_slice.to_vec上

基本上,我正在尝试这样做,但不从文件中读取

最小、完整且可验证的示例 货舱

使用此代码,我会遇到两个错误:

   Compiling example v0.1.0 (/path)
error[E0277]: the trait bound `[u8]: std::io::Read` is not satisfied
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `[u8]`
  |
  = help: the following implementations were found:
            <&'a [u8] as std::io::Read>
  = note: required for the cast to the object type `dyn std::io::Read`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required for the cast to the object type `dyn std::io::Read`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `pettorine`.

To learn more, run the command again with --verbose.
我注意到我又犯了一个错误

提前感谢您的支持

好的,找到答案了


并且Read已经实现了Impl@turbulencetoo编译器表示不满足特性绑定的“[u8]:std::io::Read”。特性'std::io::Read'不是为“[u8]”实现的,而是为&[u8]实现的,一个框应该被撤销到&[u8]您能为A提供相应的错误消息吗?包含有关如何创建此项的提示。我猜你的问题与你想解决的实际问题无关。
   Compiling example v0.1.0 (/path)
error[E0277]: the trait bound `[u8]: std::io::Read` is not satisfied
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `[u8]`
  |
  = help: the following implementations were found:
            <&'a [u8] as std::io::Read>
  = note: required for the cast to the object type `dyn std::io::Read`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:5:49
  |
5 |   let print_job = ipp::operation::PrintJob::new(doc_bytes.into_boxed_slice(), &"username", None);
  |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required for the cast to the object type `dyn std::io::Read`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `pettorine`.

To learn more, run the command again with --verbose.
let mut doc = Document::load("pettorina.pdf").unwrap();
let mut doc_bytes = Vec::new();
let mut num = " ".repeat(((4.0 -(pettorina.numero as f32).log10().floor())/2.0) as usize);
num.push_str(&pettorina.numero.to_string());
doc.version = "1.4".to_string();
doc.replace_text(1, "num", &num);
doc.replace_text(1, "NOME", &pettorina.nome.to_ascii_uppercase());
doc.save_to(&mut doc_bytes);
let client = ipp::IppClient::new("printer_url");
let print_job = ipp::operation::PrintJob::new(Box::new(std::io::Cursor::new(doc_bytes)), &"pettorine", None);
client.send(print_job).unwrap();