我想得到一个第一个角色std::str.该方法char_at()目前是不稳定的,如slice_chars在std::string::String.
我目前唯一提出的选择如下.
let text = "hello world!";
let char_vec:Vec<char> = text.chars().collect();
let ch = char_vec[0];
Run Code Online (Sandbox Code Playgroud)
但这对于获得单个字符似乎过分,而不是使用向量的其余部分.
我有一个程序打印出应该打印成看起来像表的格式的数据.但是,当数字长于2时,表会中断.我知道width参数std::fmt,但是我无法理解它.
当前输出:
---------------------------------------
| total | blanks: | comments: | code: |
---------------------------------------
| 0 | 0 | 0 | 0 |
| 77 | 0 | 3 | 74 |
| 112 | 0 | 6 | 106 |
| 178 | 0 | 6 | 172 |
| 218 | 0 | 7 | 211 |
| 289 | 0 | 8 | 281 |
| 380 | 0 | 9 | 371 | …Run Code Online (Sandbox Code Playgroud) 我一直在使用VS Code,我想知道如何构建一个task.json具有这些命令的文件.cargo build,cargo run [ARGS] cargo run --release -- [ARGS]
我试着做一个与文件上task.json.我一直在No such subcommand犯错.
样品:
{
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "cargo",
// The command is a shell script
"isBuildCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
"tasks": [{
"taskName": "run test",
"version": "0.1.0",
"command": "run -- --exclude-dir=node_modules …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,它挂钩到一个发回JSON的Web服务.
当某个属性不存在时,它提供一个空对象,其所有字段为空字符串,而不是排除该值.当属性存在时,一些属性是u64.我怎么能拥有它以便Serde处理这个案子?
#[derive(Clone, Debug, Deserialize)]
struct WebResponse {
foo: Vec<Foo>,
}
#[derive(Clone, Debug, Deserialize)]
struct Foo {
points: Points,
}
#[derive(Clone, Debug, Deserialize)]
struct Points {
x: u64,
y: u64,
name: String,
}
Run Code Online (Sandbox Code Playgroud)
{
"foo":[
{
"points":{
"x":"",
"y":"",
"name":""
}
},
{
"points":{
"x":78,
"y":92,
"name":"bar"
}
}
]
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个应该能够同时获取文件(*.*)和目录(./,..)的路径的程序.我希望能够检查提供的路径是文件还是目录.
扩展我之前的问题,你如何处理包含混合的数组structs?我试过看serde_json::Value源头.但是它没有处理两种不同的情况structs.
我不能简单地合并它们,并且在它们的属性上使用Options,因为这会使单个struct变得难以处理,并且它们必须是不同的.
#[derive(Clone, Debug, Deserialize)]
struct WebResponse {
foo: Vec<Structs>,
}
enum Structs {
Foo(Foo),
Bar(Bar),
}
#[derive(Clone, Debug, Deserialize)]
struct Foo {
name: String,
baz: Vec<String>,
}
#[derive(Clone, Debug, Deserialize)]
struct Bar {
quux: u64
}
Run Code Online (Sandbox Code Playgroud)
{
"foo": [
{
"name": "John",
"baz": ["Lorem", "Ipsum"]
},
{
"quux": 17
}
]
}
Run Code Online (Sandbox Code Playgroud) 我有一个用Swift编写的函数,我想从Rust调用。我尝试通过Objective-C公开它,但是由于ld说它找不到而继续出错_foo。通过将Rust项目编译为staticlib,Rust项目链接到Swift项目。
#pragma once
#include <stdint.h>
uint8_t foo_bridge(uint8_t);
Run Code Online (Sandbox Code Playgroud)
#import <Foundation/Foundation.h>
#import <Foo-Swift.h>
uint8_t foo_bridge(uint8_t byte) {
return foo(byte);
}
Run Code Online (Sandbox Code Playgroud)
public func foo(byte: UInt8) -> UInt8 {
return byte * 2
}
Run Code Online (Sandbox Code Playgroud)
public func foo(byte: UInt8) -> UInt8 {
return byte * 2
}
Run Code Online (Sandbox Code Playgroud)
extern "C" {
pub fn foo_bridge(byte: u8) -> u8;
}
Run Code Online (Sandbox Code Playgroud)
[package]
name = "foo"
version = "0.1.0"
[lib]
name = "foo"
crate-type = ["staticlib"]
Run Code Online (Sandbox Code Playgroud) 程序宏派生是否可以将来自其他 crate 的派生添加到它派生的结构中?
#[derive(Combined)]
struct Foo;
Run Code Online (Sandbox Code Playgroud)
#[macro_use] extern crate quote;
extern crate proc_macro2;
extern crate proc_macro;
extern crate syn;
use proc_macro::TokenStream;
#[proc_macro_derive(Combined)]
pub fn my_macro(input: TokenStream) -> TokenStream {
let input: DeriveInput = syn::parse(input).unwrap();
let ident = input.ident;
let expanded = quote! {
#[derive(Clone, Debug)]
struct #ident;
};
expanded.into()
}
Run Code Online (Sandbox Code Playgroud) 我目前正在实现fmt::Display一个结构,以便它将打印到控制台.然而,struct有一个字段,它是Vec它的类型.
pub struct Node<'a> {
pub start_tag: &'a str,
pub end_tag: &'a str,
pub content: String,
pub children: Vec<Node<'a>>,
}
Run Code Online (Sandbox Code Playgroud)
impl<'a> fmt::Display for Node<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "START TAG: {:?}", self.start_tag);
write!(f, "CONTENT: {:?}", self.content);
for node in self.children {
write!(f, "CHILDREN:\n\t {:?}", node);
}
write!(f, "END TAG: {:?}", self.end_tag);
}
}
Run Code Online (Sandbox Code Playgroud)
START TAG: "Hello"
CONTENT: ""
CHILDREN:
PRINTS CHILDREN WITH INDENT
END TAG: "World"
Run Code Online (Sandbox Code Playgroud) 我将如何运行可在exit命令或 shell 关闭上运行的 powershell 脚本。我希望这个脚本在每个关闭的 shell 上运行,而不仅仅是一个 shell。
我有两个TableViews,当我设计原件时,我使用情节提要中的原型单元来设计它,为了使其可重复使用,我尝试将其拉出.xib并加载它。cellID.xib然而,当它被加载时,它在运行时失去了所有的约束,一切都在彼此之上。
let cellIdentifier = "cellID"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
}
Run Code Online (Sandbox Code Playgroud)
在下面的函数中,我匹配a的第一个完整字符&str,如果它是a *,-或者 _是否是返回字符的那个字符,并且_我想要检查该字符是否为空格,'a'否则返回.
fn print_character(text: &str) {
let character: char = match text.chars().nth(0).unwrap() {
ch @ '*' | ch @ '-' | ch @ '_' => ch,
ch @ _ => {
if !ch.is_whitespace() {
return 'a';
}
' '
}
};
println!("{}", character);
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到以下错误:
error[E0308]: mismatched types
--> src/main.rs:6:24
|
6 | return 'a';
| ^^^ expected (), found char
|
= note: expected type `()`
found type …Run Code Online (Sandbox Code Playgroud)