我如何计算地图中元素的出现次数?
例
val myMap = Map("word1" -> "foo", "word3" -> "word4", "word5" -> "foo")
myMap contains "foo" count //???
// returns 2
Run Code Online (Sandbox Code Playgroud) 我想在C++类中包装一个Rust结构.
锈:
#[repr(C)]
pub struct RustStruct {
num: i32,
// other members..
}
pub extern "C" fn update(rust_struct: *mut RustStruct) {
(*rust_struct).num = 1i32;
}
extern "C" {
void update(void*);
}
Run Code Online (Sandbox Code Playgroud)
C++:
class Wrapper {
public:
Wrapper();
// ..
private:
void* rustStruct;
// ..
};
Wrapper::Wrapper() {
update(rustStruct); // crash
}
int main() {
std::cout << "Testing..";
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么这不起作用.我的问题是:我怎样才能实现我基本上要做的事情(在c ++类中包装一个rust结构)?
我正在跟随JavaScript教程的调用.我没有使用节点,而是直接从网络浏览器调用:
程序hello_world/index.html的
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<script src="hello_world.js"></script>
<script type='text/javascript'>
var hello_world = cwrap('hello_world', 'number', []);
var result = hello_world();
console.log("result: ", result);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
程序hello_world/SRC/main.rs
#![feature(link_args)]
#[link_args = "-s EXPORTED_FUNCTIONS=['_hello_world']"]
extern {}
#[no_mangle]
pub fn hello_world() -> isize {
println!("Hello World!");
41 + 1
}
fn main() {
// Intentionally left blank
}
Run Code Online (Sandbox Code Playgroud)
命令:
cargo build --target asmjs-unknown-emscripten
Run Code Online (Sandbox Code Playgroud)
Safari中的错误消息:
TypeError:Module ["dynCall_ii"]不是函数.(在'Module'中dynCall_ii"','Module ["dynCall_ii"]'未定义)
Firefox每晚的错误消息:
TypeError:Module.dynCall_ii不是函数了解更多
如果我只是打印功能:
var hello_world = cwrap('hello_world', 'number', []); …Run Code Online (Sandbox Code Playgroud) 我问过类似的早期问题已让我明白是怎么回事引擎盖下,但我仍然不能得到锈做我想做的事情,当谈到泛型编程.这是一些代码:
struct Foo<B: Bar> { bars: Vec<Box<B>> }
struct Foo2;
trait Bar {}
impl Bar for Foo2 {}
impl<B: Bar> Foo<B> {
fn do_something() -> Foo<B> {
let foo2:Box<Bar> = box Foo2;
let mut foo = Foo { bars: vec!(box Foo2) };
foo.bars.push(box Foo2);
foo // compiler: *ERROR*
}
}
Run Code Online (Sandbox Code Playgroud)
错误: expected 'Foo<B>', found 'Foo<Foo2>'
foo(Foo)implements Bar(B: Bar)?版: 0.12.0-nightly (4d69696ff 2014-09-24 20:35:52 +0000)
我在@Levans的解决方案中看到的问题:
struct …Run Code Online (Sandbox Code Playgroud) 我现在设置了水平滚动我希望它能够一次滚动/捕捉一个单元格,同时滚动到一边.
有人能解开一些光吗?这是我到目前为止的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
Run Code Online (Sandbox Code Playgroud) 我一直在玩Scala并且想知道是否可以嵌套调用(可能是一种描述它的坏方法).
我正在做的事情:
val nested:MyNestType =
foo("hi") {
foo("bye") {
foo("done")
}
}
Run Code Online (Sandbox Code Playgroud)
这将循环并打印出来:
"done" inside "bye" inside "hi" // or the other way around..
Run Code Online (Sandbox Code Playgroud)
怎么可以用Scala完成?
示例代码:
fn main() {
use std::thread::spawn;
spawn(|| { loop { println!("a") } });
// `a` is never printed
}
Run Code Online (Sandbox Code Playgroud)
fn main() {
use std::thread::spawn;
spawn(|| { loop { println!("a") } });
loop { }
// `a` is printed repeatedly
}
Run Code Online (Sandbox Code Playgroud)
a在第二种情况下打印到标准输出,但在第一种情况下不一样.这是为什么?a在第一种情况下不应重复打印?
基本上,我有点困在这里。我正在使用Rails3.xx。我现在使用的代码如下
@email_reminder = ToDo.where(:reminder => true, :deadline_date =< Date.today)
Run Code Online (Sandbox Code Playgroud)
上面的代码显然不起作用。我到处搜索,但是几乎所有解决方案都是Rails 2.xx解决方案。如果今天的日期已过或截止日期是最后的日期,查找所有“电子邮件提醒”的最有效方法是什么?
(不必与上面使用的方法类似,但是如果您提供使用类似方法的方法,则将有所帮助)
提前致谢
我是新来的Scala和每个人都告诉它的不好用类似if myFoo.get is null then时,myFoo被包装在一个Option(如Option[myFoo]Scala中)
我一直在看,getOrElse但所有引用getOrElse的答案都让我更加困惑,尽管听起来很简单("getOrElse").
这就是我想要做的.如果myFoo为null则执行,something但如果不是则执行somethingElse.
有人可以用最简单的方式为我解释一下吗?不一定是一个很长的答案,但我会感谢我能得到的所有帮助.
我熟悉这个错误信息,但在这种情况下我不太确定:
class Foo extends A {
// getting error here
}
trait A extends B {
def something(num:Int):Boolean = {
num == 1
}
}
trait B {
def something[S](num:S):Boolean
}
Run Code Online (Sandbox Code Playgroud)
但这编译很好:
class Foo extends A ...
trait A extends B[Int] {
def something(num:Int):Boolean = {
num == 1
}
}
trait B[S] {
def something(num:S):Boolean
}
Run Code Online (Sandbox Code Playgroud)
完整错误: class Foo needs to be abstract, since method something in trait A of type [S](num: S)Boolean is not defined
不应该先编译吗?我在这里做错了什么,我该如何解决?
我有一个看起来像这样的字符串 "./path1/path2/path3/path4/etc/file"
我想删除前两个路径并返回一个如下所示的字符串:
"path3/path4/etc/file"
我怎么能用Scala做到这一点?
请考虑以下代码:
struct Person {
mate: Option<*const Person>,
siblings: Vec<i32>,
}
impl Person {
fn meets(&mut self, mate: &mut Person) {
self.mate = Some(mate);
mate.mate = Some(self);
}
}
struct Bar {
person: Person,
}
impl Bar {
fn addr(&self) {
unsafe {
println!("\n`bob.siblings.len()`: {}, `bob.susan.siblings.len()`: {}",
self.person.siblings.len(), (*self.person.mate.unwrap()).siblings.len());
println!("\n but wait -> `bob.susan.bob.siblings.len()`: {} ??",
(*(*self.person.mate.unwrap()).mate.unwrap()).siblings.len());
println!("\nthis is `bob.susan.bob's address`: {:p} => same address inside bar ???",
(*self.person.mate.unwrap()).mate.unwrap());
}
}
}
fn main() {
let mut bob = Person { …Run Code Online (Sandbox Code Playgroud)