将`~str`编码为Rust中的base64
我想将将`~str`编码为Rust中的base64,base64,rust,Base64,Rust,我想将~str编码到Rust中的base64,以实现HTTP基本身份验证 我找到了extra::base64,但我不知道该如何使用它。ToBase64特性似乎有一个和[u8]的实现,但编译器找不到它。以下测试程序: extern mod extra; fn main(){ 使用extra::base64::MIME; 让mut config=MIME; config.line_length=无; 让foo=::std::os::args()[0]; 打印(foo.as_bytes()到_base
~str
编码到Rust中的base64,以实现HTTP基本身份验证
我找到了extra::base64
,但我不知道该如何使用它。ToBase64
特性似乎有一个和[u8]
的实现,但编译器找不到它。以下测试程序:
extern mod extra;
fn main(){
使用extra::base64::MIME;
让mut config=MIME;
config.line_length=无;
让foo=::std::os::args()[0];
打印(foo.as_bytes()到_base64(config));
}
在Rust 0.9上失败,并出现以下错误:
rustc -o test test.rs
test.rs:9:11: 9:44 error: type `&[u8]` does not implement any method in scope named `to_base64`
test.rs:9 print(foo.as_bytes().to_base64(config));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我错过了什么
ToBase64特性似乎有一个&[u8]的实现,但它是
编译器找不到
事实上,编译器在代码中找不到它,因为您没有导入它。为了使用trait实现,您必须导入trait本身:
extern mod extra;
fn main(){
使用extra::base64::{ToBase64,MIME};
让mut config=MIME;
config.line_length=无;
让foo=::std::os::args()[1];
打印(foo.as_bytes()到_base64(config));
}
(我已将
args()[0]
更改为args()[1]
,因为编码命令行参数而不仅仅是可执行文件名更有趣:)您必须添加使用extra::base64代码>@Arjan,它不起作用了。导入模块不会自动导入其特征。请注意,几天前,extra::base64
已移动到serialize::base64
。您必须将代码中的extra
替换为serialize
。@barjak如我所说,我使用的是rust 0.9。当然,但此信息对本网站的未来访问者很有用。