我想在Rust中将一串字符(SHA256哈希)转换为十六进制:
extern crate crypto;
extern crate rustc_serialize;
use rustc_serialize::hex::ToHex;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
fn gen_sha256(hashme: &str) -> String {
let mut sh = Sha256::new();
sh.input_str(hashme);
sh.result_str()
}
fn main() {
let hash = gen_sha256("example");
hash.to_hex()
}
Run Code Online (Sandbox Code Playgroud)
编译器说:
error[E0599]: no method named `to_hex` found for type `std::string::String` in the current scope
--> src/main.rs:18:10
|
18 | hash.to_hex()
| ^^^^^^
Run Code Online (Sandbox Code Playgroud)
我可以看到这是真的; 看起来它只是实现了[u8]
.
我是什么做的?在Rust中没有实现从字符串转换为十六进制的方法吗?
我的Cargo.toml依赖项:
[dependencies]
rust-crypto = "0.2.36"
rustc-serialize = "0.3.24"
Run Code Online (Sandbox Code Playgroud)
编辑我刚刚从rust-crypto库中发现字符串已经是十六进制格式.D'哦.
我经常发现自己得到这样的错误:
mismatched types: expected `collections::vec::Vec<u8>`, found `&[u8]` (expected struct collections::vec::Vec, found &-ptr)
Run Code Online (Sandbox Code Playgroud)
据我所知,一个是可变的,一个不是,但我不知道如何在类型之间走,即取&[u8]
,并使其成为Vec<u8>
反之亦然.
他们之间有什么不同?是不是一样String
和&str
?
我一直在使用和修改这个库https://github.com/sile/patricia_tree
有点困扰的一件事是 node.rs 中使用了多少不安全,特别是,它被定义为只是指向某个堆位置的指针。在执行自述页面上列出的第一个基准测试(维基百科输入)时,PatriciaSet 使用了 ~700mb(PatriciaSet 只是在它的根目录下持有一个节点)
pub struct Node<V> {
// layout:
// all these fields accessed with ptr.offset
// - flags: u8
// - label_len: u8
// - label: [u8; label_len]
// - value: Option<V>
// - child: Option<Node<V>>
// - sibling: Option<Node<V>>
ptr: *mut u8,
_value: PhantomData<V>,
}
Run Code Online (Sandbox Code Playgroud)
并malloc
用于分配:
let ptr = unsafe { libc::malloc(block_size) } as *mut u8;
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个内存没有正确对齐,所以我尝试添加新的 alloc api 并使用 Layout/alloc,这也仍然没有正确对齐,只是似乎“工作”。完全公关
let layout = Layout::array::<u8>(block_size).expect("Failed to get layout");
let ptr …
Run Code Online (Sandbox Code Playgroud) 语言中有什么东西可以将二进制字符串转换为int吗?
我的二进制文件现在作为字符串存在,我希望格式化!它与我将int格式化为二进制文件的方式相同:format!
我有一个更大的二进制字符串,我正在循环中取出切片,所以让我们假设我的一个切片是:
let bin_idx: &str = "01110011001";
Run Code Online (Sandbox Code Playgroud)
我想将该二进制格式化为整数:
format!("{:i}", bin_idx);
Run Code Online (Sandbox Code Playgroud)
这给出了编译器错误:
error: unknown format trait `i`
--> src/main.rs:3:21
|
3 | format!("{:i}", bin_idx);
| ^^^^^^^
Run Code Online (Sandbox Code Playgroud)
我也试过'd'和'你'(http://www.contrib.andrew.cmu.edu/~acrichto/doc/std/fmt/index.html)并得到了同样的错误.
我正在关注一个haskell教程:http://www.seas.upenn.edu/~cis194/lectures/01-intro.html
我正在测试ghci中的函数,我得到了这个部分:
hailstone :: Integer -> Integer
hailstone n
| n `mod` 2 == 0 = n `div` 2
| otherwise = 3*n + 1
Run Code Online (Sandbox Code Playgroud)
我在.hs文件中有这个功能,我在同一个目录中启动ghci并执行:l hailstone.hs
输出是
Syntax error on 'mod'
Perhaps you intended to use TemplateHaskell
In the Template Haskell quotation 'mod'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
做了一些谷歌搜索,试图加载这个'templatehaskell',最后得到一组不同的错误(http://brandon.si/code/working-with-template-haskell-in-ghci/)
针对较新的 nightlys 重新编译一些较旧的代码,我收到有关使用as_slice()
有利于var[]
语法的语法的警告。
但是,当我按照RFC 中as_slice()
所示替换为时,我收到编译器错误:[]
expected `&str`,
found `str`
(expected &-ptr,
found str) [E0308]
src/main.rs:38 print_usage(program[], opts);
Run Code Online (Sandbox Code Playgroud)
与我原来的相比
print_usage(program.as_slice(), opts);
Run Code Online (Sandbox Code Playgroud)
语法是否as_slice()
完全消失,或者只是将其写为更惯用vec[]
?当我按照编译器要求我做的事情时遇到的错误是怎么回事?
我可以看到我必须像这样导入:
use std::io::IoResult;
use std::num::{Int, ToPrimitive};
use std::rand::{OsRng, Rng};
Run Code Online (Sandbox Code Playgroud)
然后创建一个新的OsRng实例,并尝试从中生成一个新的u32 int
fn main() {
let mut rng = OsRng::new();
let num:u32 = rng.next_u32();
println!("{}",num);
}
Run Code Online (Sandbox Code Playgroud)
但是我得到的错误类型core::result::Result<std::rand::os::imp::OsRng, std::io::IoError>
没有实现命名范围内的任何方法next_u32
但是铁锈文件清楚地说有一个功能next_u32
?http://doc.rust-lang.org/std/rand/struct.OsRng.html
我错过了什么?
我有一份整体清单清单 [[1,2,3,4],[1,2,3,4]]
我想转发它 [[1,1],[2,2],[3,3]...]
我有:
transpose : List (List a) -> List (List a)
transpose ll = case ll of
((x::xs)::xss) -> (x :: (List.map List.head xss)) :: transpose (xs :: (List.map List.tail xss))
otherwise -> []
Run Code Online (Sandbox Code Playgroud)
但问题是编译器不喜欢head和tail操作,并希望返回Maybe类型.
如何在榆树中正确转置列表?
fn recursive_binary_search<T: Ord>(list: &mut [T], target: T) -> bool {
if list.len() < 1 {
return false;
}
let guess = list.len() / 2;
if target == list[guess] {
return true;
} else if list[guess] > target {
return recursive_binary_search(&mut list[0..guess], target);
} else if list[guess] < target {
return recursive_binary_search(&mut list[guess..list.len()], target);
}
}
Run Code Online (Sandbox Code Playgroud)
编译器在if target == list[guess]
说出时抛出错误
src/main.rs:33:5: 39:6 error: mismatched types [E0308]
src/main.rs:33 if target == list[guess] {
^
src/main.rs:33:5: 39:6 help: run `rustc --explain …
Run Code Online (Sandbox Code Playgroud) 我正在编写我的第一个 proc 宏,尽管尝试通读此错误、structopt 和derive_more 的源代码,但我似乎无法准确找到我正在寻找的内容。我想改变这个:
#[derive(Attach)]
#[attach(foo(SomeType, OtherType))]
#[attach(bar(OtherType))]
struct Plugin {}
Run Code Online (Sandbox Code Playgroud)
进入这个:
impl Attach for Plugin {
fn attach(self, srv: &mut Server) {
let this = Arc::new(self);
srv.foo_order(this.clone(), &[TypeId::of::<SomeType>(), TypeId::of::<OtherType>()]);
srv.bar_order(this, &[TypeId::of::<OtherType>()]);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经开始编写一个 proc 宏,但在尝试解析属性时遇到了麻烦:
extern crate proc_macro;
use proc_macro::{Span, TokenStream};
use quote::quote;
use std::any::TypeId;
use syn::{
parse::ParseStream, parse_macro_input, Attribute, AttributeArgs, DeriveInput, Ident, Result,
};
#[proc_macro_derive(Attach, attributes(attach))]
pub fn register_macro(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
impl_register(input)
}
fn impl_register(input: DeriveInput) -> TokenStream …
Run Code Online (Sandbox Code Playgroud) 将我的代码更新到新的 nightlies 似乎他们已经摆脱了 std::Vec 的 to_string()
src/rust_mnemonic.rs:100:39: 100:50 error: type `collections::vec::Vec<&str>` does not implement any method in scope named `to_string`
rc/rust_mnemonic.rs:100 println!("mnemonic: {}", mnemonic.to_string());
Run Code Online (Sandbox Code Playgroud)