我知道C++ 中的"未定义行为"几乎可以让编译器做任何想做的事情.但是,我遇到了让我感到惊讶的崩溃,因为我认为代码足够安全.
在这种情况下,真正的问题仅发生在使用特定编译器的特定平台上,并且仅在启用了优化时才发生.
我尝试了几件事来重现问题并将其简化到最大程度.这是一个名为的函数的摘录Serialize,它将获取bool参数,并将字符串true或复制false到现有的目标缓冲区.
如果bool参数是未初始化的值,那么这个函数是否会在代码审查中,没有办法告诉它实际上可能会崩溃?
// Zero-filled global buffer of 16 characters
char destBuffer[16];
void Serialize(bool boolValue) {
// Determine which string to print based on boolValue
const char* whichString = boolValue ? "true" : "false";
// Compute the length of the string we selected
const size_t len = strlen(whichString);
// Copy string into destination buffer, which is zero-filled (thus already null-terminated)
memcpy(destBuffer, whichString, len);
}
Run Code Online (Sandbox Code Playgroud)
如果使用clang 5.0.0 +优化执行此代码,它将/可能崩溃.
boolValue ? "true" …
我正在学习/试验Rust,在我用这种语言找到的所有优雅中,有一个让我感到困惑并且看起来完全不合适的特点.
在进行方法调用时,Rust会自动取消引用指针.我做了一些测试来确定确切的行为:
struct X { val: i32 }
impl std::ops::Deref for X {
type Target = i32;
fn deref(&self) -> &i32 { &self.val }
}
trait M { fn m(self); }
impl M for i32 { fn m(self) { println!("i32::m()"); } }
impl M for X { fn m(self) { println!("X::m()"); } }
impl M for &X { fn m(self) { println!("&X::m()"); } }
impl M for &&X { fn m(self) { println!("&&X::m()"); } }
impl M for &&&X { …Run Code Online (Sandbox Code Playgroud) 注意:此问题包含已弃用的1.0之前的代码!答案是正确的.
要将一个转换str为一个int在Rust中,我可以这样做:
let my_int = from_str::<int>(my_str);
Run Code Online (Sandbox Code Playgroud)
我知道如何将一个转换的唯一方法String到int是得到它的一个切片,然后用from_str它像这样:
let my_int = from_str::<int>(my_string.as_slice());
Run Code Online (Sandbox Code Playgroud)
有没有办法直接将a转换String为int?
按照本指南,我创建了一个货运项目
SRC/main.rs
fn main() {
hello::print_hello();
}
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
我运行使用
cargo build && cargo run
Run Code Online (Sandbox Code Playgroud)
它编译没有错误.现在我正在尝试将主模块分成两部分,但无法弄清楚如何从另一个文件中包含一个模块.
我的项目树看起来像这样
??? src
??? hello.rs
??? main.rs
Run Code Online (Sandbox Code Playgroud)
和文件的内容:
SRC/main.rs
use hello;
fn main() {
hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)
SRC/hello.rs
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译它src/main.rs,我得到
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
Run Code Online (Sandbox Code Playgroud)
我试图遵循编译器的建议并修改main.rs
#![feature(globs)]
extern crate hello; …Run Code Online (Sandbox Code Playgroud) 我希望有一个包含多个结构的模块,每个模块都在自己的文件中.以Math模块为例:
Math/
Vector.rs
Matrix.rs
Complex.rs
Run Code Online (Sandbox Code Playgroud)
我希望每个结构都在同一个模块中,我将从我的主文件中使用它,如下所示:
use Math::Vector;
fn main() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,Rust的模块系统(开始时有点混乱)并没有提供一种明显的方法来实现这一点.它似乎只允许您将整个模块放在一个文件中.这不是质朴的吗?如果没有,我该怎么做?
我已经在多个上下文中阅读过“胖指针”一词,但是我不确定它的确切含义以及何时在Rust中使用它。该指针似乎是普通指针的两倍,但我不明白为什么。它也似乎与特征对象有关。
我有一个字符串,我需要扫描每次出现的"foo"并读取其后的所有文本,直到一秒钟".由于Rust没有,我需要通过字符扫描来迭代它.我该怎么做?contains字符串函数
编辑:Rust &str有一个contains()和find()方法.
一个使用它将输出发送到stdout:
println!("some output")
Run Code Online (Sandbox Code Playgroud)
我认为没有相应的宏可以为stderr做同样的事情.
在Rust中,抽象的主要工具是traits。在C ++中,有两种用于抽象的工具:抽象类和模板。为了摆脱一些使用模板的缺点(例如,硬盘读取错误消息),C ++引入的概念,这是“命名集的要求”。
这两个功能似乎非常相似:
但据我了解,也存在显着差异。例如,C ++的概念似乎定义了一组必须有效的表达式,而不是列出函数签名。但是那里有很多不同且令人困惑的信息(也许是因为概念仅出现在C ++ 20中?)。这就是为什么我想知道:C ++概念和Rust的特性之间的区别和相似之处到底是什么?
是否存在仅概念或特征提供的功能?例如,Rust的关联类型和const如何?还是用多个特征/概念来界定一个类型?
rust ×9
c++ ×2
llvm-codegen ×2
abi ×1
c++-concepts ×1
dereference ×1
inline ×1
iterator ×1
llvm ×1
module ×1
pointers ×1
reference ×1
stdio ×1
string ×1
traits ×1